mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-02 10:52:27 +00:00
Merge pull request #312 from PR0M3TH3AN/codex/update-sendpayment-to-use-msats
Convert NWC client payment amount to msats
This commit is contained in:
2 files changed
+61
-12
No files matched your search
+18
-12
@@ -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,
|
||||
});
|
||||
@@ -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);
|
||||
Reference in new issue
Block a user