From 38710d87523430bdfda7422d18420405091599ea Mon Sep 17 00:00:00 2001 From: bnonni Date: Sat, 7 May 2022 18:45:02 -0400 Subject: [PATCH] clear --- backend/api/account/accountController.js | 2 +- backend/api/account/accountService.js | 28 ++++++++----- backend/api/admin/adminController.js | 4 +- backend/api/admin/adminService.js | 4 +- backend/sensei/admin.js | 3 +- backend/sensei/nodes.js | 50 ++++-------------------- backend/utils/apiCall.js | 33 ++++++++++------ 7 files changed, 53 insertions(+), 71 deletions(-) diff --git a/backend/api/account/accountController.js b/backend/api/account/accountController.js index 0557955..433614b 100644 --- a/backend/api/account/accountController.js +++ b/backend/api/account/accountController.js @@ -46,4 +46,4 @@ const createInvoice = async (req, res) => { } }; -module.exports = { getAccountAllowance }; +module.exports = { getAccountAllowance, payInvoice, createInvoice }; diff --git a/backend/api/account/accountService.js b/backend/api/account/accountService.js index 84561cf..f94b8ba 100644 --- a/backend/api/account/accountService.js +++ b/backend/api/account/accountService.js @@ -1,5 +1,5 @@ const debug = require('../../utils/debug'); -const senseiAdmin = require('../../sensei/admin'); +const senseiNodes = require('../../sensei/nodes'); const accounts = require('../../db/collection'); const getAccountAllowance = async (username) => { @@ -12,15 +12,23 @@ const getAccountAllowance = async (username) => { } }; -const payInvoice = async (req, res) => { +const getAccountBalance = async (username) => { try { - const response = await accountService.getAccountAllowance(); - - debug.info(`Account Allowance Response: ${JSON.stringify(response)}`); + + return { success: true, message: charge }; + } catch (error) { + debug.error(error.stack, error.status, error.message); + throw new Error(error); + } +}; +const payInvoice = async (req, res) => { + try { + const invoice = req.body.invoice; + const response = await senseiNodes.payInvoice(invoice); + debug.info(`Pay Invoice Response: ${JSON.stringify(response)}`); if (!response.success) res.status(500).json(response); else res.status(200).json(response); - } catch (error) { debug.error(error.stack); res.status(500).json({ message: error.message, error: error.stack }); @@ -29,10 +37,10 @@ const payInvoice = async (req, res) => { const createInvoice = async (req, res) => { try { - const response = await accountService.getAccountAllowance(); - - debug.info(`Account Allowance Response: ${JSON.stringify(response)}`); - + const amountMillisats = req.body.amountMillisats; + const description = req.body.description; + const response = await senseiNodes.createInvoice(amountMillisats, description); + debug.info(`Create Invoice Response: ${JSON.stringify(response)}`); if (!response.success) res.status(500).json(response); else res.status(200).json(response); diff --git a/backend/api/admin/adminController.js b/backend/api/admin/adminController.js index d766031..c463efe 100644 --- a/backend/api/admin/adminController.js +++ b/backend/api/admin/adminController.js @@ -5,12 +5,10 @@ const senseiNodes = require('../../sensei/nodes'); const getAllBalances = async (req, res) => { try { - const response = adminService.getAllBalances(); + const response = await adminService.getAllBalances(); debug.info(`Admin All Balances Response: ${JSON.stringify(response)}`); - if (!response.success) res.status(500).json(response); else res.status(200).json(response); - } catch (error) { debug.error(error.stack); res.status(500).json({ message: error.message, error: error.stack }); diff --git a/backend/api/admin/adminService.js b/backend/api/admin/adminService.js index 5152d55..61896b1 100644 --- a/backend/api/admin/adminService.js +++ b/backend/api/admin/adminService.js @@ -8,8 +8,8 @@ const getAllBalances = async (req, res) => { const response = await senseiAdmin.listNodes(); let nodes = []; for (let node of response.nodes) { - node.balance = await senseiNodes.getBalance(node.username); - nodes.push(node);x + node.balance = (await senseiNodes.getBalance(node.username)).balance_satoshis; + nodes.push(node); } return { success: true, message: nodes }; } catch (error) { diff --git a/backend/sensei/admin.js b/backend/sensei/admin.js index 24c6e4c..f8510e9 100644 --- a/backend/sensei/admin.js +++ b/backend/sensei/admin.js @@ -30,8 +30,7 @@ const listNodes = async (page, take, query) => { headers: { 'Content-Type': 'application/json', 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', + } }); console.log(res); diff --git a/backend/sensei/nodes.js b/backend/sensei/nodes.js index 01b669a..46736d1 100644 --- a/backend/sensei/nodes.js +++ b/backend/sensei/nodes.js @@ -1,4 +1,4 @@ -const apiCall = require('../utils/apiCall'); +const { apiCall } = require('../utils/apiCall'); const getUnusedAddress = async () => { const { address } = await apiCall('/v1/node/wallet/address', 'GET') @@ -6,7 +6,7 @@ const getUnusedAddress = async () => { } const getBalance = async () => { - const { balance } = await apiCall('/v1/node/wallet/balance', 'GET') + const balance = await apiCall('/v1/node/wallet/balance', 'GET') return await balance.json(); } @@ -34,62 +34,28 @@ const getInfo = async () => { } const getPeers = async () => { - const { peers } = await fetch(`${BASE_URL}/v1/node/peers`); + const { peers } = await apiCall('/v1/node/peers', 'GET') return await peers.json(); } const stopNode = async () => { - const res = await fetch(`${BASE_URL}/v1/node/stop`); + const res = await apiCall('/v1/node/stop', 'GET'); 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, - }), - }); + const res = await apiCall('/v1/node/invoices', 'POST', { 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 - }) - } - - ); + const res = await apiCall('/v1/node/invoices/pay', 'POST', { 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 - }) - } - ); + const res = await apiCall('/v1/node/invoices/pay', 'POST', { dest_pubkey: destPubkey, amt_msat: amtMsat }); + return await res.json(); } diff --git a/backend/utils/apiCall.js b/backend/utils/apiCall.js index 0dd87bb..e82d622 100644 --- a/backend/utils/apiCall.js +++ b/backend/utils/apiCall.js @@ -3,16 +3,27 @@ const BASE_URL = process.env.BASE_URL; const MACAROON = process.env.MACAROON; const TOKEN = process.env.TOKEN; -const apiCall = async (path, method, json={}) => { - return await fetch(BASE_URL + path, { - method: method, - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', - body: JSON.stringify(json), - }); +const apiCall = async (path, method, json = null) => { + if (json) { + return await fetch(BASE_URL + path, { + method: method, + headers: { + 'Content-Type': 'application/json', + Cookie: `macaroon=${MACAROON}; token=${TOKEN}`, + }, + credentials: 'include', + body: JSON.stringify(json), + }); + } else { + return await fetch(BASE_URL + path, { + method: method, + headers: { + 'Content-Type': 'application/json', + Cookie: `macaroon=${MACAROON}; token=${TOKEN}`, + }, + credentials: 'include', + }); + } }; -module.exports = { apiCall } \ No newline at end of file +module.exports = { apiCall };