-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.py
68 lines (52 loc) · 2.14 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
63
64
65
66
67
68
import asyncio
from lnbits.core.models import Payment
from lnbits.core.services import create_invoice, pay_invoice
from lnbits.tasks import register_invoice_listener
from loguru import logger
from .crud import get_livestream_by_track, get_producer, get_track
async def wait_for_paid_invoices():
invoice_queue = asyncio.Queue()
register_invoice_listener(invoice_queue, "ext_livestream")
while True:
payment = await invoice_queue.get()
await on_invoice_paid(payment)
async def on_invoice_paid(payment: Payment) -> None:
if not payment.extra or payment.extra.get("tag") != "livestream":
# not a livestream invoice
return
track = await get_track(payment.extra.get("track", -1))
if not track:
logger.error("this should never happen", payment)
return
if payment.extra.get("shared_with"):
logger.error("payment was shared already", payment)
return
producer = await get_producer(track.producer)
assert producer, f"track {track.id} is not associated with a producer"
ls = await get_livestream_by_track(track.id)
assert ls, f"track {track.id} is not associated with a livestream"
amount = int(payment.amount * (100 - ls.fee_pct) / 100)
payment = await create_invoice(
wallet_id=producer.wallet,
amount=int(amount / 1000),
internal=True,
memo=f"Revenue from '{track.name}'.",
)
logger.debug(
f"livestream: producer invoice created: {payment.payment_hash}, {amount} msats"
)
payment = await pay_invoice(
payment_request=payment.bolt11,
wallet_id=payment.wallet_id,
extra={
**payment.extra,
"shared_with": f"Producer ID: {producer.id}",
"received": payment.amount,
},
)
logger.debug(f"livestream: producer invoice paid: {payment.checking_id}")
# so the flow is the following:
# - we receive, say, 1000 satoshis
# - if the fee_pct is, say, 30%, the amount we will send is 700
# - we change the amount of receiving payment on the database from 1000 to 300
# - we create a new payment on the producer's wallet with amount 700