refactor splitpayments with tags

This commit is contained in:
Tiago Vasconcelos 2023-01-05 12:20:31 +00:00
parent 4ae88ad3b9
commit 7a7bd93639

View file

@ -45,13 +45,21 @@ async def on_invoice_paid(payment: Payment) -> None:
amount_to_split = int(payment.extra.get("amount") * 1000) amount_to_split = int(payment.extra.get("amount") * 1000)
for target in targets: for target in targets:
if target.percent > 0:
tagged = target.tag in payment.extra
amount = int(payment.amount * target.percent / 100) # msats amount = int(payment.amount * target.percent / 100) # msats
memo = (
f"split payment: {target.percent}% for {target.alias or target.wallet}"
)
if tagged:
memo = f"Pushed tagged payment to {target.alias}"
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), # sats amount=int(amount / 1000), # sats
internal=True, internal=True,
memo=f"split payment: {target.percent}% for {target.alias or target.wallet}", memo=memo,
) )
logger.debug(f"created split invoice: {payment_hash}") logger.debug(f"created split invoice: {payment_hash}")
@ -63,28 +71,8 @@ async def on_invoice_paid(payment: Payment) -> None:
checking_id = 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={**payment.extra, "splitted": True}, extra={**extra},
) )
logger.debug(f"paid split invoice: {checking_id}") logger.debug(f"paid split invoice: {checking_id}")
logger.debug(f"performing split to {len(targets)} targets") 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}")