Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async def m003_add_id_and_tag(db: Connection):
"wallet": row["wallet"],
"source": row["source"],
"percent": row["percent"],
"tag": row["tag"],
"tag": "",
"alias": row["alias"],
},
)
Expand Down
6 changes: 1 addition & 5 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ function hashTargets(targets) {
}

function isTargetComplete(target) {
return (
target.wallet &&
target.wallet.trim() !== '' &&
(target.percent > 0 || target.tag != '')
)
return target.wallet && target.wallet.trim() !== '' && target.percent > 0
}

window.app = Vue.createApp({
Expand Down
61 changes: 48 additions & 13 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,35 @@ async def on_invoice_paid(payment: Payment) -> None:
logger.trace(f"splitpayments: performing split payments to {len(targets)} targets")

for target in targets:
if target.percent > 0:
try:
if target.percent <= 0:
continue

amount_msat = int(payment.amount * target.percent / 100)
amount_sat = amount_msat // 1000
if amount_sat < 1:
logger.warning(
f"splitpayments: skipping target {target.alias or target.wallet},"
f" amount too small ({amount_msat} msat)"
)
continue

memo = (
f"Split payment: {target.percent}% "
f"for {target.alias or target.wallet}"
f";{payment.memo};{payment.payment_hash}"
)

if "@" in target.wallet or "LNURL" in target.wallet:
payment_request = None
if "@" in target.wallet or "lnurl" in target.wallet.lower():
safe_amount_msat = amount_msat - fee_reserve(amount_msat)
if safe_amount_msat < 1000:
logger.warning(
f"splitpayments: skipping LNURL target"
f" {target.alias or target.wallet},"
f" amount after fee reserve too small"
)
continue
payment_request = await get_lnurl_invoice(
target.wallet, payment.wallet_id, safe_amount_msat, memo
)
Expand All @@ -65,24 +84,40 @@ async def on_invoice_paid(payment: Payment) -> None:
target.wallet = wallet.id
new_payment = await create_invoice(
wallet_id=target.wallet,
amount=int(amount_msat / 1000),
amount=amount_sat,
internal=True,
memo=memo,
)
payment_request = new_payment.bolt11

extra = {**payment.extra, "splitted": True}
if not payment_request:
continue

if payment_request:
task = asyncio.create_task(
pay_invoice_in_background(
payment_request=payment_request,
wallet_id=payment.wallet_id,
description=memo,
extra=extra,
)
extra = {**payment.extra, "splitted": True}
task = asyncio.create_task(
pay_invoice_in_background(
payment_request=payment_request,
wallet_id=payment.wallet_id,
description=memo,
extra=extra,
)
task.add_done_callback(lambda fut: logger.success(fut.result()))
)
task.add_done_callback(_log_task_result)

except Exception as e:
logger.error(
f"splitpayments: failed to process target"
f" {target.alias or target.wallet}: {e}"
)


def _log_task_result(fut):
try:
result = fut.result()
if result:
logger.success(result)
except Exception as e:
logger.error(f"splitpayments: background task failed: {e}")


async def pay_invoice_in_background(payment_request, wallet_id, description, extra):
Expand Down
4 changes: 3 additions & 1 deletion views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def api_targets_set(
targets: list[Target] = []
for entry in target_put.targets:

if entry.wallet.find("@") < 0 and entry.wallet.find("LNURL") < 0:
if entry.wallet.find("@") < 0 and "lnurl" not in entry.wallet.lower():
wallet = await get_wallet(entry.wallet)
if not wallet:
wallet = await get_wallet_for_key(entry.wallet)
Expand Down Expand Up @@ -70,6 +70,8 @@ async def api_targets_set(

await set_targets(source_wallet.wallet.id, targets)

except HTTPException:
raise
except Exception as ex:
logger.warning(ex)
raise HTTPException(
Expand Down