Merge pull request #1089 from lnbits/fix/paymentChain_splitPayment

Fix workflow for chaining extension
This commit is contained in:
calle 2023-01-06 17:10:57 +01:00 committed by GitHub
commit 79eec2e751
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 45 deletions

View file

@ -86,12 +86,19 @@ async def lnurl_callback(
ls = await get_livestream_by_track(track_id)
extra_amount = amount_received - int(amount_received * (100 - ls.fee_pct) / 100)
payment_hash, payment_request = await create_invoice(
wallet_id=ls.wallet,
amount=int(amount_received / 1000),
memo=await track.fullname(),
unhashed_description=(await track.lnurlpay_metadata()).encode(),
extra={"tag": "livestream", "track": track.id, "comment": comment},
extra={
"tag": "livestream",
"track": track.id,
"comment": comment,
"amount": int(extra_amount / 1000),
},
)
if amount_received < track.price_msat:

View file

@ -22,6 +22,7 @@ async def wait_for_paid_invoices():
async def on_invoice_paid(payment: Payment) -> None:
if payment.extra.get("tag") != "livestream":
# not a livestream invoice
return
@ -41,21 +42,26 @@ async def on_invoice_paid(payment: Payment) -> None:
ls = await get_livestream_by_track(track.id)
assert ls, f"track {track.id} is not associated with a livestream"
# now we make a special kind of internal transfer
amount = int(payment.amount * (100 - ls.fee_pct) / 100)
payment_hash, payment_request = await create_invoice(
wallet_id=tpos.tip_wallet,
amount=amount, # sats
wallet_id=producer.wallet,
amount=int(amount / 1000),
internal=True,
memo=f"Revenue from '{track.name}'.",
)
logger.debug(f"livestream: producer invoice created: {payment_hash}")
logger.debug(
f"livestream: producer invoice created: {payment_hash}, {amount} msats"
)
checking_id = await pay_invoice(
payment_request=payment_request,
wallet_id=payment.wallet_id,
extra={"tag": "livestream"},
extra={
**payment.extra,
"shared_with": f"Producer ID: {producer.id}",
"received": payment.amount,
},
)
logger.debug(f"livestream: producer invoice paid: {checking_id}")

View file

@ -20,60 +20,57 @@ async def wait_for_paid_invoices():
async def on_invoice_paid(payment: Payment) -> None:
if payment.extra.get("tag") == "splitpayments":
if payment.extra.get("tag") == "splitpayments" or payment.extra.get("splitted"):
# already a splitted payment, ignore
return
targets = await get_targets(payment.wallet_id)
logger.debug(targets)
if not targets:
return
# validate target percentages
total_percent = sum([target.percent for target in targets])
if total_percent > 100:
logger.error("splitpayment failure: total percent adds up to more than 100%")
logger.error("splitpayment: total percent adds up to more than 100%")
return
logger.trace(f"splitpayments: performing split payments to {len(targets)} targets")
if payment.extra.get("amount"):
amount_to_split = (payment.extra.get("amount") or 0) * 1000
else:
amount_to_split = payment.amount
if not amount_to_split:
logger.error("splitpayments: no amount to split")
return
logger.debug(f"checking if tagged for {len(targets)} targets")
tagged = False
for target in targets:
if target.tag in payment.extra:
tagged = True
tagged = target.tag in payment.extra
if tagged or target.percent > 0:
if tagged:
memo = f"Pushed tagged payment to {target.alias}"
amount_msat = int(amount_to_split)
else:
amount_msat = int(amount_to_split * target.percent / 100)
memo = f"Split payment: {target.percent}% for {target.alias or target.wallet}"
payment_hash, payment_request = await create_invoice(
wallet_id=target.wallet,
amount=int(payment.amount / 1000), # sats
amount=int(amount_msat / 1000),
internal=True,
memo=f"Pushed tagged payment to {target.alias}",
extra={"tag": "splitpayments"},
memo=memo,
)
logger.debug(f"created split invoice: {payment_hash}")
checking_id = await pay_invoice(
extra = {**payment.extra, "tag": "splitpayments", "splitted": True}
await pay_invoice(
payment_request=payment_request,
wallet_id=payment.wallet_id,
extra={"tag": "splitpayments"},
extra=extra,
)
logger.debug(f"paid split invoice: {checking_id}")
logger.debug(f"performing split to {len(targets)} targets")
if tagged == False:
for target in targets:
if target.percent > 0:
amount = int(payment.amount * target.percent / 100) # msats
payment_hash, payment_request = await create_invoice(
wallet_id=target.wallet,
amount=int(amount / 1000), # sats
internal=True,
memo=f"split payment: {target.percent}% for {target.alias or target.wallet}",
extra={"tag": "splitpayments"},
)
logger.debug(f"created split invoice: {payment_hash}")
checking_id = await pay_invoice(
payment_request=payment_request,
wallet_id=payment.wallet_id,
extra={"tag": "splitpayments"},
)
logger.debug(f"paid split invoice: {checking_id}")

View file

@ -20,6 +20,9 @@ async def wait_for_paid_invoices():
async def on_invoice_paid(payment: Payment) -> None:
if not payment.extra:
return
if payment.extra.get("tag") != "tpos":
return
@ -50,7 +53,7 @@ async def on_invoice_paid(payment: Payment) -> None:
payment_hash, payment_request = await create_invoice(
wallet_id=wallet_id,
amount=int(tipAmount), # sats
amount=int(tipAmount),
internal=True,
memo=f"tpos tip",
)
@ -59,6 +62,6 @@ async def on_invoice_paid(payment: Payment) -> None:
checking_id = await pay_invoice(
payment_request=payment_request,
wallet_id=payment.wallet_id,
extra={"tag": "tpos"},
extra={**payment.extra, "tipSplitted": True},
)
logger.debug(f"tpos: tip invoice paid: {checking_id}")

View file

@ -76,7 +76,12 @@ async def api_tpos_create_invoice(
wallet_id=tpos.wallet,
amount=amount,
memo=f"{tpos.name}",
extra={"tag": "tpos", "tipAmount": tipAmount, "tposId": tpos_id},
extra={
"tag": "tpos",
"tipAmount": tipAmount,
"tposId": tpos_id,
"amount": amount - tipAmount if tipAmount else False,
},
)
except Exception as e:
raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e))