aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-02-13 15:33:37 -0800
committerAdam Soltys <[email protected]>2026-02-13 15:33:37 -0800
commitb2b9fe837db7308285909ec1b374546194405d00 (patch)
treebe9dbc10c7f0699b40ac2ed2534869e6b9e772e7 /lib
parent542cf1d084d189bcdf127c9c2359cced5267295e (diff)
downloadcoinos-server-b2b9fe837db7308285909ec1b374546194405d00.tar.gz
coinos-server-b2b9fe837db7308285909ec1b374546194405d00.zip
bolt12 autowithdraw
Diffstat (limited to 'lib')
-rw-r--r--lib/payments.ts35
1 files changed, 26 insertions, 9 deletions
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) {