-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
62 lines (53 loc) · 1.98 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import asyncio
from datetime import datetime
from lnbits.core.models import Payment
from lnbits.core.services import pay_invoice
from lnbits.tasks import register_invoice_listener
from .crud import (
get_satspot,
update_satspot,
)
from .helpers import calculate_winner, get_pr
async def wait_for_paid_invoices():
invoice_queue = asyncio.Queue()
register_invoice_listener(invoice_queue, "ext_satspot")
while True:
payment = await invoice_queue.get()
await on_invoice_paid(payment)
async def on_invoice_paid(payment: Payment) -> None:
if payment.extra.get("tag") == "satspot":
ln_address = payment.extra["ln_address"]
satspot_id = payment.extra["satspot_id"]
# fetch details
satspot = await get_satspot(satspot_id)
if not satspot:
return
# Check they are not trying to scam the system.
if (payment.amount / 1000) != satspot.buy_in:
return
await calculate_winner(satspot)
# If player joins late send a refund
if datetime.now().timestamp() > satspot.closing_date.timestamp():
satspot.completed = True
await update_satspot(satspot)
# Calculate the haircut amount
haircut_amount = satspot.buy_in * (satspot.haircut / 100)
# Calculate the refund amount
max_sat = int(satspot.buy_in - haircut_amount)
pr = await get_pr(ln_address, max_sat)
if not pr:
return
await pay_invoice(
wallet_id=satspot.wallet,
payment_request=pr,
max_sat=max_sat,
description="Refund. Satspot game was full.",
)
await calculate_winner(satspot)
return
# Add the player to the game.
if satspot.players == "":
satspot.players = ln_address
else:
satspot.players = f"{satspot.players},{ln_address}"
await update_satspot(satspot)