Skip to content

Commit

Permalink
clear
Browse files Browse the repository at this point in the history
  • Loading branch information
bnonni committed May 7, 2022
1 parent 62e1a04 commit 38710d8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 71 deletions.
2 changes: 1 addition & 1 deletion backend/api/account/accountController.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ const createInvoice = async (req, res) => {
}
};

module.exports = { getAccountAllowance };
module.exports = { getAccountAllowance, payInvoice, createInvoice };
28 changes: 18 additions & 10 deletions backend/api/account/accountService.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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 });
Expand All @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions backend/api/admin/adminController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
4 changes: 2 additions & 2 deletions backend/api/admin/adminService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions backend/sensei/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
50 changes: 8 additions & 42 deletions backend/sensei/nodes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const apiCall = require('../utils/apiCall');
const { apiCall } = require('../utils/apiCall');

const getUnusedAddress = async () => {
const { address } = await apiCall('/v1/node/wallet/address', 'GET')
return await address.json();
}

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();
}

Expand Down Expand Up @@ -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();
}


Expand Down
33 changes: 22 additions & 11 deletions backend/utils/apiCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
module.exports = { apiCall };

0 comments on commit 38710d8

Please sign in to comment.