From b075ae593d619a860c0b38350cada35f29f65c23 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 9 Sep 2022 10:27:32 -0400 Subject: use actual received amount for lightning payments --- lib/sync.js | 12 ++++++------ routes/lightning/invoice.js | 3 --- routes/lightning/receive.js | 4 ++-- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/sync.js b/lib/sync.js index 7b72b20a..536bd582 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -80,14 +80,14 @@ const sync = async () => { let recent = invoices .filter( i => - i.settled && - differenceInDays(new Date(), new Date(i.settle_date * 1000)) < 2 + i.secret && + differenceInDays(new Date(), new Date(i.created_at)) < 2 ) .map(i => ({ - amount: parseInt(i.amt_paid_sat), - preimage: i.r_preimage.toString("hex"), - pr: i.payment_request.toString("hex"), - createdAt: new Date(i.settle_date * 1000) + amount: parseInt(i.received), + preimage: i.secret, + pr: i.request.toString("hex"), + createdAt: new Date(i.created_at) })); const twoDaysAgo = new Date(new Date().setDate(new Date().getDate() - 2)); diff --git a/routes/lightning/invoice.js b/routes/lightning/invoice.js index 3ef0e109..94aac768 100644 --- a/routes/lightning/invoice.js +++ b/routes/lightning/invoice.js @@ -8,7 +8,6 @@ export default async (req, res) => { if (!tip) tip = 0; let value = amount + tip; - console.log("getting invoice"); try { if (config.lna.clightning) { if (!memo) memo = "coinos"; @@ -20,13 +19,11 @@ export default async (req, res) => { ); res.send({ text: invoice.bolt11 }); } else { - console.log("creating invoice"); const invoice = await createInvoice({ lnd, tokens: value, description: memo }); - console.log("created"); res.send({ text: invoice.request }); } } catch (e) { diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index 8141e5d3..3ecc4ce9 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -12,7 +12,7 @@ import { l, err, warn } from "$lib/logging"; const handlePayment = async msg => { try { - l("incoming lightning payment", msg.tokens, msg.request.substr(0,20)); + l("incoming lightning payment", msg.received, msg.request.substr(0,20)); if (!msg.secret) return; let account, total, user; @@ -28,7 +28,7 @@ const handlePayment = async msg => { await db.transaction(async transaction => { const { text: hash, currency, memo, rate, tip, user_id } = invoice; - const amount = parseInt(msg.tokens) - tip; + const amount = parseInt(msg.received) - tip; if (amount < 0) throw new Error("amount out of range"); account = await db.Account.findOne({ -- cgit v1.2.3 From 86c59fc85cba222abc6dbe82d862ca45892a27c8 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 9 Sep 2022 14:27:02 -0400 Subject: remove cancelled ln payments --- lib/sync.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/sync.js b/lib/sync.js index 536bd582..57f0c507 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -75,12 +75,14 @@ const sync = async () => { ] }); - let { invoices } = await getInvoices({ lnd, limit: 200 }); + let { invoices } = await getInvoices({ lnd, limit: 1000 }); + let x = invoices.find(i => i.request.includes('lnbc10100u1p333l3lpp5znwsrpr9g')); + let cancelled = invoices.filter(i => i.is_canceled); let recent = invoices .filter( i => - i.secret && + i.is_confirmed && differenceInDays(new Date(), new Date(i.created_at)) < 2 ) .map(i => ({ @@ -98,6 +100,28 @@ const sync = async () => { }) ).map(p => p.preimage); + for (let i = 0; i < cancelled.length; i++) { + let c = cancelled[i]; + if (settled.includes(c.secret)) { + await db.transaction(async transaction => { + let p = await db.Payment.findOne({ + where: { + preimage: c.secret + }, + include: { + model: db.Account, + as: "account" + }, + transaction + }); + + console.log("destroying", p.id); + await p.account.decrement({ balance: p.amount }, { transaction }); + await p.destroy({ transaction }); + }); + } + } + let missed = recent.filter(i => !settled.includes(i.preimage)); for (let i = 0; i < missed.length; i++) { let { amount, preimage, pr: text, createdAt } = missed[i]; -- cgit v1.2.3 From b83e7189e66c4d4f04a029268e87af185bf7aee7 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 9 Sep 2022 14:27:28 -0400 Subject: remove cancelled fix --- lib/sync.js | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/lib/sync.js b/lib/sync.js index 57f0c507..62d13b24 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -100,28 +100,6 @@ const sync = async () => { }) ).map(p => p.preimage); - for (let i = 0; i < cancelled.length; i++) { - let c = cancelled[i]; - if (settled.includes(c.secret)) { - await db.transaction(async transaction => { - let p = await db.Payment.findOne({ - where: { - preimage: c.secret - }, - include: { - model: db.Account, - as: "account" - }, - transaction - }); - - console.log("destroying", p.id); - await p.account.decrement({ balance: p.amount }, { transaction }); - await p.destroy({ transaction }); - }); - } - } - let missed = recent.filter(i => !settled.includes(i.preimage)); for (let i = 0; i < missed.length; i++) { let { amount, preimage, pr: text, createdAt } = missed[i]; -- cgit v1.2.3 From fdb1494a717583a2048861b7c4b1cb6b283d0c0c Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 9 Sep 2022 20:28:34 -0400 Subject: adjust ln fee settings --- lib/send.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/send.js b/lib/send.js index 26709b61..77d954f6 100644 --- a/lib/send.js +++ b/lib/send.js @@ -9,7 +9,7 @@ import { conversionFeeReceiver } from "../routes/lightning/conversionFee"; import lnd from "$lib/lnd"; -import { pay } from "lightning"; +import { payViaPaymentRequest } from "lightning"; import { l, warn, err } from "$lib/logging"; export default async (amount, memo, hash, user) => { @@ -89,12 +89,10 @@ export default async (amount, memo, hash, user) => { let tokens; if (!payreq.satoshis) tokens = amount; - let m = await pay({ + let m = await payViaPaymentRequest({ lnd, - tokens, request: hash, - max_paths: 10, - max_fee: { fixed: Math.min(10000, account.balance) } + max_fee: Math.min(10000, account.balance) }); if (!m) throw new Error(`lightning payment failed ${amount} ${hash}`); -- cgit v1.2.3