From 63fe7c28cc433f211a13634a65b7d9ac17949311 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 12 Dec 2023 04:58:09 +0000 Subject: allow undefined amount for lightning payments --- lib/payments.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/payments.js b/lib/payments.js index 14bdf7ae..f53fc2c4 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -5,7 +5,7 @@ import { v4 } from "uuid"; import { notify } from "$lib/notifications"; import { db, g, s, t } from "$lib/db"; import { l, err, warn } from "$lib/logging"; -import { fail, wait } from "$lib/utils"; +import { fail, wait, SATS } from "$lib/utils"; import { callWebhook } from "$lib/webhooks"; import dollarsToWords from "dollars-to-words"; import got from "got"; @@ -23,7 +23,7 @@ export const types = { bitcoin: "bitcoin", lightning: "lightning", fund: "fund", - classic: "classic" + classic: "classic", }; export let debit = async ( @@ -91,7 +91,7 @@ export let debit = async ( type, ref, tip, - created: Date.now() + created: Date.now(), }; await s(`payment:${id}`, p); @@ -150,7 +150,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { tip, type, confirmed: true, - created: Date.now() + created: Date.now(), }; if (type === types.bitcoin) invoice.pending += amount; @@ -190,7 +190,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { new Intl.NumberFormat("en-US", { maximumFractionDigits: 0 }).format( p.amount ), - link: `${process.env.URL}/payment/${p.id}` + link: `${process.env.URL}/payment/${p.id}`, }); } @@ -231,8 +231,10 @@ export let payLightning = async (user, payreq, amount, maxfee, memo) => { let p; let hash = payreq; - amount = parseInt(amount); - if (amount < 0) fail("Invalid amount"); + if (typeof amount !== "undefined") { + amount = parseInt(amount); + if (amount < 0 || amount > SATS || isNaN(amount)) fail("Invalid amount"); + } maxfee = maxfee ? parseInt(maxfee) : 0; if (maxfee < 0) fail("Max fee cannot be negative"); @@ -292,13 +294,13 @@ export let payLightning = async (user, payreq, amount, maxfee, memo) => { let interval = setInterval(check, 5000); try { - l("paying lightning invoice", payreq); + l("paying lightning invoice", payreq.substr(-8), total, amount, maxfee); r = await ln.pay({ bolt11: payreq.replace(/\s/g, "").toLowerCase(), amount_msat: amount_msat ? undefined : amount * 1000, maxfee: maxfee * 1000, - retry_for: 5 + retry_for: 5, }); if (!(r.status === "complete" && r.payment_preimage)) @@ -306,8 +308,7 @@ export let payLightning = async (user, payreq, amount, maxfee, memo) => { l("payment completed", p.id, r.payment_preimage); - p.amount = -amount; - p.tip = total - amount; + p.amount = -total; p.fee = Math.round((r.amount_sent_msat - r.amount_msat) / 1000); p.ref = r.payment_preimage; -- cgit v1.2.3