From d40d52154e067ee728474740854658a5fff7f8ff Mon Sep 17 00:00:00 2001 From: Jordan Brookman Date: Sat, 7 May 2022 16:11:59 -0400 Subject: [PATCH] Finish adding functions in nodes.js --- backend/sensei/nodes.js | 95 ++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 35 deletions(-) diff --git a/backend/sensei/nodes.js b/backend/sensei/nodes.js index 34a6633..52b95d1 100644 --- a/backend/sensei/nodes.js +++ b/backend/sensei/nodes.js @@ -2,17 +2,13 @@ const fetch = require('node-fetch'); const BASE_URL = process.env.BASE_URL; const getUnusedAddress = async () => { - const res = await fetch(`${BASE_URL}/v1/node/wallet/address`, { - method: 'GET' - }); + const res = await fetch(`${BASE_URL}/v1/node/wallet/address`); console.log(res); return await res.json(); } const getBalance = async () => { - const res = await fetch(`${BASE_URL}/v1/node/wallet/balance`, { - method: 'GET' - }); + const res = await fetch(`${BASE_URL}/v1/node/wallet/balance`); console.log(res); return await res.json(); } @@ -23,34 +19,15 @@ const getChannels = async ({ page, searchTerm, take }) => { method: 'GET' }); - return await channels; + return await channels.json(); } const getPayments = async ({ filter = {}, pagination }) => { const { page, take, } = pagination; - const res = await fetch( - `${BASE_URL}/v1/node/payments?page=${page}&take=${take}`, - ); + const res = await fetch(`${BASE_URL}/v1/node/payments?page=${page}&take=${take}`); - return { - pagination: res.pagination, - payments: res.payments.map((payment) => { - return { - id: payment.id, - paymentHash: payment.payment_hash, - preimage: payment.preimage, - secret: payment.secret, - status: payment.status, - origin: payment.origin, - amtMsat: payment.amt_msat, - createdAt: payment.created_at, - updatedAt: payment.updated_at, - label: payment.label, - invoice: payment.invoice, - }; - }), - }; + return await res.json(); } const getInfo = async () => { @@ -69,11 +46,59 @@ const getInfo = async () => { const getPeers = async () => { const { peers } = await fetch(`${BASE_URL}/v1/node/peers`); - return { - peers: peers.map((peer) => { - return { - nodePubkey: peer.node_pubkey, - }; - }), - }; + return await peers.json(); +} + +const stopNode = async () => { + const res = await fetch(`${BASE_URL}/v1/node/stop`); + return res.json(); +} + +const createInvoice = async (amountMillisats, description) => { + const res = await fetch(`${BASE_URL}/v1/node/invoices`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify({ + amt_msat: amountMillisats, + description, + }), + }); + return await res.json(); +} + +const payInvoice = async (invoice) => { + const res = await fetch(`${BASE_URL}/v1/node/invoices/pay`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify({ + invoice + }) + } + + ); + return await res.json(); +} + +const keysend = async (destPubkey, amtMsat) => { + const res = await fetch(`${BASE_URL}/v1/node/keysend`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify({ + dest_pubkey: destPubkey, + amt_msat: amtMsat + }) + } + ); } \ No newline at end of file