From 90f2c9ca5ca7e34ea6f9ac390ea218afb29d377b Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Wed, 18 Dec 2024 15:30:44 +0100 Subject: [PATCH] add support for max_fee_mtokens in bolt12 interface --- lib/bolt11.js | 3 ++- lib/bolt12.js | 4 ++-- lib/boltInvoices.js | 6 +++--- lib/lndk.js | 11 ++++++++--- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/bolt11.js b/lib/bolt11.js index 42ee584e6..c6a0fc9f3 100644 --- a/lib/bolt11.js +++ b/lib/bolt11.js @@ -13,12 +13,13 @@ export async function parseBolt11 ({ request }) { return parsePaymentRequest({ request }) } -export async function payBolt11 ({ lnd, request, max_fee, ...args }) { +export async function payBolt11 ({ lnd, request, max_fee, max_fee_mtokens, ...args }) { if (!isBolt11(request)) throw new Error('not a bolt11 invoice') return payViaPaymentRequest({ lnd, request, max_fee, + max_fee_mtokens, ...args }) } diff --git a/lib/bolt12.js b/lib/bolt12.js index cb6d671a9..71a2c09f4 100644 --- a/lib/bolt12.js +++ b/lib/bolt12.js @@ -19,9 +19,9 @@ export function isBolt12 (invoice) { return isBolt12Offer(invoice) || isBolt12Invoice(invoice) } -export async function payBolt12 ({ lnd, request: invoice, max_fee }) { +export async function payBolt12 ({ lnd, request: invoice, max_fee, max_fee_mtokens }) { if (!isBolt12Invoice(invoice)) throw new Error('not a bolt12 invoice') - return await payViaBolt12PaymentRequest({ lnd, request: invoice, max_fee }) + return await payViaBolt12PaymentRequest({ lnd, request: invoice, max_fee, max_fee_mtokens }) } export async function parseBolt12 ({ lnd, request: invoice }) { diff --git a/lib/boltInvoices.js b/lib/boltInvoices.js index 57e7092c1..32937c125 100644 --- a/lib/boltInvoices.js +++ b/lib/boltInvoices.js @@ -4,11 +4,11 @@ import { payBolt11, parseBolt11, isBolt11 } from './bolt11' import { estimateBolt12RouteFee } from '@/lib/lndk' import { estimateRouteFee } from '@/api/lnd' -export async function payInvoice ({ lnd, request: invoice, max_fee, ...args }) { +export async function payInvoice ({ lnd, request: invoice, max_fee, max_fee_mtokens, ...args }) { if (isBolt12Invoice(invoice)) { - return await payBolt12({ lnd, request: invoice, max_fee, ...args }) + return await payBolt12({ lnd, request: invoice, max_fee, max_fee_mtokens, ...args }) } else if (isBolt11(invoice)) { - return await payBolt11({ lnd, request: invoice, max_fee, ...args }) + return await payBolt11({ lnd, request: invoice, max_fee, max_fee_mtokens, ...args }) } else if (isBolt12Offer(invoice)) { throw new Error('cannot pay bolt12 offer directly, please fetch a bolt12 invoice from the offer first') } else { diff --git a/lib/lndk.js b/lib/lndk.js index 564eb1d43..880deba82 100644 --- a/lib/lndk.js +++ b/lib/lndk.js @@ -1,4 +1,4 @@ -import { msatsToSats, toPositiveNumber } from '@/lib/format' +import { msatsToSats, satsToMsats, toPositiveNumber } from '@/lib/format' import { loadPackageDefinition } from '@grpc/grpc-js' import LNDK_RPC_PROTO from '@/lib/lndkrpc-proto' import protobuf from 'protobufjs' @@ -179,19 +179,24 @@ export async function fetchBolt12InvoiceFromOffer ({ lnd, offer, msats, descript export async function payViaBolt12PaymentRequest ({ lnd, request: invoice_hex_str, - max_fee + max_fee, + max_fee_mtokens }) { const lndk = lnd?.lndk if (!lndk) throw new Error('lndk not installed, please use installLNDK') const parsedInvoice = await parseBolt12Request({ lnd, request: invoice_hex_str }) + if (!max_fee_mtokens && max_fee) { + max_fee_mtokens = toPositiveNumber(satsToMsats(max_fee)) + } + return new Promise((resolve, reject) => { lndk.PayInvoice({ invoice: parsedInvoice.payment, // expects msats amount: https://github.com/lndk-org/lndk/blob/bce93885f5fc97f3ceb15dc470117e10061de018/src/lib.rs#L403 amount: toPositiveNumber(parsedInvoice.mtokens), - max_fee + max_fee: toPositiveNumber(max_fee_mtokens) }, (error, response) => { if (error) { return reject(error)