Merge remote-tracking branch 'origin/fix/paymentChain_splitPayment' into fix/paymentChain_splitPayment
This commit is contained in:
commit
89266f9aa2
1 changed files with 31 additions and 27 deletions
|
|
@ -26,51 +26,55 @@ async def on_invoice_paid(payment: Payment) -> None:
|
||||||
return
|
return
|
||||||
|
|
||||||
targets = await get_targets(payment.wallet_id)
|
targets = await get_targets(payment.wallet_id)
|
||||||
logger.debug(targets)
|
|
||||||
if not targets:
|
if not targets:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# validate target percentages
|
||||||
total_percent = sum([target.percent for target in targets])
|
total_percent = sum([target.percent for target in targets])
|
||||||
|
|
||||||
if total_percent > 100:
|
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
|
||||||
|
if not all([target.percent > 0 for target in targets]):
|
||||||
|
logger.error("splitpayment: not all percentages are positive > 0")
|
||||||
return
|
return
|
||||||
|
|
||||||
logger.debug(f"performing split payments to {len(targets)} targets")
|
logger.trace(f"splitpayments: performing split payments to {len(targets)} targets")
|
||||||
|
|
||||||
amount_to_split = payment.amount
|
|
||||||
|
|
||||||
if payment.extra.get("amount"):
|
if payment.extra.get("amount"):
|
||||||
amount_to_split = (payment.extra.get("amount") or 0) * 1000
|
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
|
||||||
|
|
||||||
for target in targets:
|
for target in targets:
|
||||||
tagged = target.tag in payment.extra
|
tagged = target.tag in payment.extra
|
||||||
if tagged or target.percent > 0:
|
if tagged or target.percent > 0:
|
||||||
|
|
||||||
amount = int(amount_to_split * target.percent / 100)
|
|
||||||
memo = (
|
|
||||||
f"split payment: {target.percent}% for {target.alias or target.wallet}"
|
|
||||||
)
|
|
||||||
if tagged:
|
if tagged:
|
||||||
memo = f"Pushed tagged payment to {target.alias}"
|
memo = f"Pushed tagged payment to {target.alias}"
|
||||||
amount = int(amount_to_split)
|
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(
|
payment_hash, payment_request = await create_invoice(
|
||||||
wallet_id=target.wallet,
|
wallet_id=target.wallet,
|
||||||
amount=int(amount / 1000),
|
amount=int(amount_msat / 1000),
|
||||||
internal=True,
|
internal=True,
|
||||||
memo=memo,
|
memo=memo,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.debug(f"created split invoice: {payment_hash}")
|
extra = {**payment.extra, "splitted": True}
|
||||||
|
|
||||||
extra = {**payment.extra, "tag": "splitpayments", "splitted": True}
|
await pay_invoice(
|
||||||
|
|
||||||
checking_id = await pay_invoice(
|
|
||||||
payment_request=payment_request,
|
payment_request=payment_request,
|
||||||
wallet_id=payment.wallet_id,
|
wallet_id=payment.wallet_id,
|
||||||
extra=extra,
|
extra=extra,
|
||||||
)
|
)
|
||||||
logger.debug(f"paid split invoice: {checking_id}")
|
|
||||||
|
|
||||||
logger.debug(f"performing split to {len(targets)} targets")
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue