From 03b1d5288ed1aa4989e833563a28273dfdfce21f Mon Sep 17 00:00:00 2001 From: Jordan Brookman Date: Sat, 7 May 2022 14:35:47 -0400 Subject: [PATCH] Add some functions to nodes.js --- backend/sensei/nodes.js | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 backend/sensei/nodes.js diff --git a/backend/sensei/nodes.js b/backend/sensei/nodes.js new file mode 100644 index 0000000..34a6633 --- /dev/null +++ b/backend/sensei/nodes.js @@ -0,0 +1,79 @@ +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' + }); + console.log(res); + return await res.json(); +} + +const getBalance = async () => { + const res = await fetch(`${BASE_URL}/v1/node/wallet/balance`, { + method: 'GET' + }); + console.log(res); + return await res.json(); +} + +const getChannels = async ({ page, searchTerm, take }) => { + const { channels, pagination } = await fetch( + `${BASE_URL}/v1/node/channels?page=${page}&take=${take}&query=${searchTerm}`, { + method: 'GET' + }); + + return await channels; +} + +const getPayments = async ({ filter = {}, pagination }) => { + const { page, take, } = pagination; + + 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, + }; + }), + }; +} + +const getInfo = async () => { + const { node_info } = await fetch(`${BASE_URL}/v1/node/info`); + + return { + version: node_info.version, + nodePubkey: node_info.node_pubkey, + numChannels: node_info.num_channels, + numUsableChannels: node_info.num_usable_channels, + numPeers: node_info.num_peers, + localBalanceMsat: node_info.local_balance_msat, + }; +} + +const getPeers = async () => { + const { peers } = await fetch(`${BASE_URL}/v1/node/peers`); + + return { + peers: peers.map((peer) => { + return { + nodePubkey: peer.node_pubkey, + }; + }), + }; +} \ No newline at end of file