From 1e42b2af56579803e505041415466aa1555fda1a Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Sun, 5 Oct 2025 19:33:58 -0400 Subject: [PATCH] Convert NWC payment amounts to msats --- js/payments/nwcClient.js | 30 ++++++++++++++++----------- tests/nwc-client.test.mjs | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 tests/nwc-client.test.mjs diff --git a/js/payments/nwcClient.js b/js/payments/nwcClient.js index 151deca1..eb3953f8 100644 --- a/js/payments/nwcClient.js +++ b/js/payments/nwcClient.js @@ -881,26 +881,31 @@ function sanitizeAmount(amount) { return Math.max(0, rounded); } -export async function sendPayment(bolt11, { settings, amountSats, zapRequest, timeoutMs } = {}) { - const context = await ensureWallet({ settings }); - const invoice = sanitizeInvoice(bolt11); - const payload = { - id: `req-${Date.now()}-${++requestCounter}`, - method: "pay_invoice", - params: { - invoice, - }, - }; +function buildPayInvoiceParams({ invoice, amountSats, zapRequest }) { + const params = { invoice }; const amount = sanitizeAmount(amountSats); if (amount && amount > 0) { - payload.params.amount = amount; + params.amount = amount * 1000; } if (zapRequest) { - payload.params.zap_request = zapRequest; + params.zap_request = zapRequest; } + return params; +} + +export async function sendPayment(bolt11, { settings, amountSats, zapRequest, timeoutMs } = {}) { + const context = await ensureWallet({ settings }); + const invoice = sanitizeInvoice(bolt11); + const params = buildPayInvoiceParams({ invoice, amountSats, zapRequest }); + const payload = { + id: `req-${Date.now()}-${++requestCounter}`, + method: "pay_invoice", + params, + }; + const response = await sendWalletRequest(context, payload, { timeoutMs }); return response; } @@ -918,4 +923,5 @@ export const __TESTING__ = Object.freeze({ closeSocket, pendingRequests, ensureActiveState, + buildPayInvoiceParams, }); diff --git a/tests/nwc-client.test.mjs b/tests/nwc-client.test.mjs new file mode 100644 index 00000000..af2fd6d2 --- /dev/null +++ b/tests/nwc-client.test.mjs @@ -0,0 +1,43 @@ +import assert from "node:assert/strict"; + +if (typeof globalThis.window === "undefined") { + globalThis.window = {}; +} + +const { __TESTING__ } = await import("../js/payments/nwcClient.js"); +const { buildPayInvoiceParams } = __TESTING__; + +await (async () => { + const params = buildPayInvoiceParams({ + invoice: "bolt11-invoice", + amountSats: 123, + zapRequest: "zap-request-json", + }); + + assert.equal(params.invoice, "bolt11-invoice"); + assert.equal(params.amount, 123_000); + assert.equal(params.zap_request, "zap-request-json"); +})(); + +await (async () => { + const params = buildPayInvoiceParams({ + invoice: "bolt11-invoice", + amountSats: 0, + zapRequest: null, + }); + + assert.equal(params.invoice, "bolt11-invoice"); + assert.ok(!("amount" in params)); + assert.ok(!("zap_request" in params)); +})(); + +await (async () => { + const params = buildPayInvoiceParams({ + invoice: "bolt11-invoice", + amountSats: 42.7, + }); + + assert.equal(params.amount, 43_000); +})(); + +process.exit(0);