From b2b9fe837db7308285909ec1b374546194405d00 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 13 Feb 2026 15:33:37 -0800 Subject: bolt12 autowithdraw --- lib/payments.ts | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/payments.ts b/lib/payments.ts index cc1391fe..3b6a4a86 100644 --- a/lib/payments.ts +++ b/lib/payments.ts @@ -521,13 +521,30 @@ const pay = async ({ aid = undefined, amount, to, user }) => { }); } - const fee = Math.max(5, Math.round(amount * 0.02)); + let fee; + const ourfee = Math.round(amount * (config.fee.lightning || 0)); if (lnurl) { - amount -= fee; + fee = Math.max(5, Math.round(amount * 0.02)); + amount -= fee + ourfee; const { callback } = (await got(lnurl).json()) as any; ({ pr } = (await got(`${callback}?amount=${amount * 1000}`).json()) as any); + } else if (to.startsWith("lno")) { + const { id: source } = await ln.getinfo(); + const { offer_issuer_id } = await ln.decode(to); + const { routes } = await ln.getroutes( + source, offer_issuer_id, amount * 1000, + ["auto.localchans", "auto.sourcefree"], + Math.round(amount * 0.02) * 1000, 6, + ); + fee = routes.length + ? Math.max(5, Math.round((routes[0].path[0].amount_msat - routes[0].amount_msat) / 1000)) + : 0; + amount -= fee + ourfee; + const { invoice } = await ln.fetchinvoice(to, amount * 1000); + pr = invoice; } else if (to.startsWith("ln")) { - amount -= fee; + fee = Math.max(5, Math.round(amount * 0.02)); + amount -= fee + ourfee; pr = to; } @@ -763,7 +780,7 @@ export const sendKeysend = async ({ destination: pubkey, amount_msat: amount * 1000, maxfee: fee * 1000, - retry_for: 10, + retry_for: 20, extratlvs, }); } catch (e) { @@ -796,9 +813,9 @@ export const sendLightning = async ({ payee = invoice_node_id; } - let minfee = 2; const { channels } = await ln.listpeerchannels(); - if (channels.some((c) => c.peer_id === payee)) minfee = 0; + const isDirect = channels.some((c) => c.peer_id === payee); + const minfee = isDirect ? 0 : Math.max(Math.round(amount * 0.005), 5); fee = Math.max(Number.parseInt(fee || 0), minfee); if (fee < 0) fail("Fee cannot be negative"); @@ -829,7 +846,7 @@ export const sendLightning = async ({ // bolt11: pr.replace(/\s/g, "").toLowerCase(), amount_msat: amount_msat ? undefined : amount * 1000, maxfee: fee * 1000, - retry_for: 10, + retry_for: 20, }); try { @@ -1396,8 +1413,8 @@ const finalize = async (r, p) => { nwcNotify(p); const maxfee = p.fee; - const { amount_msat } = await ln.decode(p.hash); - p.fee = Math.round((r.amount_sent_msat - amount_msat) / 1000); + const { amount_msat, invoice_amount_msat } = await ln.decode(p.hash); + p.fee = Math.round((r.amount_sent_msat - (amount_msat || invoice_amount_msat)) / 1000); p.ref = preimage; if (!(await g(`payment:${p.id}`)).ref) { -- cgit v1.2.3