From b53875cf6bdf47c359139aa846c6597fa3627c87 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 20 Sep 2022 22:48:01 -0700 Subject: clightning support --- lib/ln.js | 3 +- lib/send.js | 18 +++++++- lib/sync.js | 137 +++++++++++++++++++++++++++++++----------------------------- 3 files changed, 88 insertions(+), 70 deletions(-) (limited to 'lib') diff --git a/lib/ln.js b/lib/ln.js index 8e96efea..7a1b1822 100644 --- a/lib/ln.js +++ b/lib/ln.js @@ -2,7 +2,8 @@ import config from "$config"; let ln; if (config.lna.clightning) { - ln = (await import("clightning-client"))(config.lna.dir); + let { LightningClient } = (await import("clightning-client")).default; + ln = new LightningClient(config.lna.dir); } else { ln = await import("./lnd"); } diff --git a/lib/send.js b/lib/send.js index 77d954f6..9d70b34e 100644 --- a/lib/send.js +++ b/lib/send.js @@ -9,6 +9,7 @@ import { conversionFeeReceiver } from "../routes/lightning/conversionFee"; import lnd from "$lib/lnd"; +import ln from "$lib/ln"; import { payViaPaymentRequest } from "lightning"; import { l, warn, err } from "$lib/logging"; @@ -89,7 +90,9 @@ export default async (amount, memo, hash, user) => { let tokens; if (!payreq.satoshis) tokens = amount; - let m = await payViaPaymentRequest({ + let m; + if (config.lnd) { + m = await payViaPaymentRequest({ lnd, request: hash, max_fee: Math.min(10000, account.balance) @@ -97,8 +100,19 @@ export default async (amount, memo, hash, user) => { if (!m) throw new Error(`lightning payment failed ${amount} ${hash}`); if (m.payment_error) throw new Error(m.payment_error); - ({ fee, secret: preimage, tokens: total } = m); + } else { + m = await ln.pay(hash, tokens ? `${tokens}sats` : undefined); + let sent; + ({ + msatoshi: total, + msatoshi_sent: sent, + payment_preimage: preimage + } = m); + + fee = parseInt((sent - total) / 1000); + total = parseInt(total / 1000); + } preimage = preimage.toString("hex"); diff --git a/lib/sync.js b/lib/sync.js index 467ce107..95abb99d 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -75,82 +75,85 @@ const sync = async () => { ] }); - let { invoices } = await getInvoices({ lnd, limit: 1000 }); + let missed, invoices; + if (config.lnd) { + ({ invoices } = await getInvoices({ lnd, limit: 1000 })); + + let recent = invoices + .filter( + i => + i.is_confirmed && + differenceInDays(new Date(), new Date(i.created_at)) < 2 + ) + .map(i => ({ + amount: parseInt(i.received), + preimage: i.secret, + pr: i.request, + createdAt: new Date(i.created_at) + })); + + const twoDaysAgo = new Date(new Date().setDate(new Date().getDate() - 2)); + let settled = ( + await db.Payment.findAll({ + where: { network: "lightning", createdAt: { [Op.gt]: twoDaysAgo } }, + attributes: ["preimage"] + }) + ).map(p => p.preimage); + + missed = recent.filter(i => !settled.includes(i.preimage)); + for (let i = 0; i < missed.length; i++) { + let { amount, preimage, pr: text, createdAt } = missed[i]; + if (!text || !text.length) continue; - let recent = invoices - .filter( - i => - i.is_confirmed && - differenceInDays(new Date(), new Date(i.created_at)) < 2 - ) - .map(i => ({ - amount: parseInt(i.received), - preimage: i.secret, - pr: i.request, - createdAt: new Date(i.created_at) - })); - - const twoDaysAgo = new Date(new Date().setDate(new Date().getDate() - 2)); - let settled = ( - await db.Payment.findAll({ - where: { network: "lightning", createdAt: { [Op.gt]: twoDaysAgo } }, - attributes: ["preimage"] - }) - ).map(p => p.preimage); - - 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]; - if (!text || !text.length) continue; - - try { - let invoice = await db.Invoice.findOne({ - where: { text }, - include: { - model: db.User, - as: "user" - } - }); - - if (invoice && invoice.user_id) { - let account = await db.Account.findOne({ - where: { - user_id: invoice.user_id, - asset: config.liquid.btcasset, - pubkey: null + try { + let invoice = await db.Invoice.findOne({ + where: { text }, + include: { + model: db.User, + as: "user" } }); - if (account) { - let payment = await db.Payment.create({ - account_id: account.id, - user_id: invoice.user_id, - hash: text, - amount, - currency: invoice.currency, - preimage, - rate: invoice.rate, - received: true, - confirmed: true, - network: "lightning", - tip: invoice.tip, - invoice_id: invoice.id, - createdAt, - updatedAt: createdAt + if (invoice && invoice.user_id) { + let account = await db.Account.findOne({ + where: { + user_id: invoice.user_id, + asset: config.liquid.btcasset, + pubkey: null + } }); - await account.increment({ balance: amount }); + if (account) { + let payment = await db.Payment.create({ + account_id: account.id, + user_id: invoice.user_id, + hash: text, + amount, + currency: invoice.currency, + preimage, + rate: invoice.rate, + received: true, + confirmed: true, + network: "lightning", + tip: invoice.tip, + invoice_id: invoice.id, + createdAt, + updatedAt: createdAt + }); + + await account.increment({ balance: amount }); - l("lightning missing from account", account.id, amount); + l("lightning missing from account", account.id, amount); + } } + } catch (e) { + err( + "problem syncing lightning payment", + JSON.stringify(missed[i]), + e.message, + e.stack + ); } - } catch (e) { - err( - "problem syncing lightning payment", - JSON.stringify(missed[i]), - e.message, - e.stack - ); } } -- cgit v1.2.3