Skip to content

Commit

Permalink
add support for max_fee_mtokens in bolt12 interface
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Dec 18, 2024
1 parent 20c3e58 commit 90f2c9c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/bolt11.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
4 changes: 2 additions & 2 deletions lib/bolt12.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down
6 changes: 3 additions & 3 deletions lib/boltInvoices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions lib/lndk.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 90f2c9c

Please sign in to comment.