Skip to content

Commit

Permalink
api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
bnonni committed May 7, 2022
1 parent e69de52 commit 25ea534
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 35 deletions.
1 change: 1 addition & 0 deletions backend/api/account/accountService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const debug = require('../../utils/debug');
const senseiAdmin = require('../../sensei/admin');
const accounts = require('../../db/collection');

const getAccountAllowance = async (username) => {
try {
Expand Down
2 changes: 2 additions & 0 deletions backend/api/account/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ const router = express();
const { getAccountAllowance } = require('./accountController');

router.get('/allowance', getAccountAllowance);
router.get('/payment/send', );
router.get('/payment/receive', );

module.exports = router;
10 changes: 2 additions & 8 deletions backend/api/admin/adminController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ const senseiNodes = require('../../sensei/nodes');

const getAllBalances = async (req, res) => {
try {
const response = await senseiAdmin.listNodes();
let nodesBalances = []
for(let node of response.nodes){
node.balance = await senseiNodes.getBalance(node.username);
nodesBalances.push(node)
}

debug.info(`Admin All Balances Response: ${nodesBalances}`);
const response = 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);
Expand Down
19 changes: 18 additions & 1 deletion backend/api/admin/adminService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
const debug = require('../../utils/debug');
const accounts = require('../../db/collection');
const senseiAdmin = require('../../sensei/admin');
const senseiNodes = require('../../sensei/nodes');

module.exports = { };
const getAllBalances = async (req, res) => {
try {
const response = await senseiAdmin.listNodes();
let nodes = [];
for (let node of response.nodes) {
node.balance = await senseiNodes.getBalance(node.username);
nodes.push(node);x
}
return { success: true, message: nodes };
} catch (error) {
debug.error(error.stack);
throw new Error(error);
}
};

module.exports = { getAllBalances };
2 changes: 1 addition & 1 deletion backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ app.use(
);

app.get('/', async (req, res) => {
res.send(`Health check! Server srunning on port ${PORT}!`);
res.send(`Health check! Server running on port ${PORT}!`);
});

const account = require('./api/account');
Expand Down
6 changes: 3 additions & 3 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"repository": "[email protected]:atlantabitdevs/familybtc.git",
"author": "[email protected]",
"license": "MIT",
"private": false,
"scripts": {
"start": "NODE_ENV=prod node server.js",
"dev": "NODE_ENV=local nodemon server.js"
"start": "PORT=8080 node server.js",
"dev": "PORT=8081 nodemon server.js"
},
"dependencies": {
"cors": "^2.8.5",
Expand All @@ -20,6 +19,7 @@
"http-errors": "^2.0.0",
"mailgun-js": "^0.22.0",
"morgan": "^1.10.0",
"node-fetch": "2.6.6",
"nodemon": "^2.0.15",
"opennode": "^1.3.0",
"uuid": "^8.3.2",
Expand Down
64 changes: 63 additions & 1 deletion backend/sensei/admin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const fetch = require('node-fetch');
const BASE_URL = process.env.BASE_URL;
const MACAROON = process.env.MACAROON;
const TOKEN = process.env.TOKEN;

const initSensei = async (username, passphrase, alias, electrum_url, start) => {
const res = await fetch(`${BASE_URL}/v1/init`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
username,
passphrase,
Expand All @@ -18,8 +25,13 @@ const initSensei = async (username, passphrase, alias, electrum_url, start) => {
};

const listNodes = async (page, take, query) => {
const res = await fetch(`${BASE_URL}/v1/nodes?page=${page || 0}&take=${take || 10}`, {
const res = await fetch(`${BASE_URL}/v1/nodes?page=${0}&take=${10}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
});

console.log(res);
Expand All @@ -29,6 +41,11 @@ const listNodes = async (page, take, query) => {
const createNode = async (username, passphrase, alias, start) => {
const res = await fetch(`${BASE_URL}/v1/nodes`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
username,
passphrase,
Expand All @@ -44,6 +61,11 @@ const createNode = async (username, passphrase, alias, start) => {
const startNode = async (pubkey, passphrase) => {
const res = await fetch(`${BASE_URL}/v1/nodes/start`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
pubkey,
passphrase,
Expand All @@ -57,6 +79,11 @@ const startNode = async (pubkey, passphrase) => {
const stopNode = async (pubkey) => {
const res = await fetch(`${BASE_URL}/v1/nodes/stop`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
pubkey,
},
Expand All @@ -69,6 +96,11 @@ const stopNode = async (pubkey) => {
const deleteNode = async (pubkey) => {
const res = await fetch(`${BASE_URL}/v1/nodes/delete`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
pubkey,
},
Expand All @@ -81,6 +113,11 @@ const deleteNode = async (pubkey) => {
const nodeStatus = async () => {
const res = await fetch(`${BASE_URL}/v1/status`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
});

console.log(res);
Expand All @@ -90,6 +127,11 @@ const nodeStatus = async () => {
const startSensi = async (passphrase) => {
const res = await fetch(`${BASE_URL}/v1/start`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
passphrase,
},
Expand All @@ -102,6 +144,11 @@ const startSensi = async (passphrase) => {
const login = async (username, passphrase) => {
const res = await fetch(`${BASE_URL}/v1/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
username,
passphrase,
Expand All @@ -115,6 +162,11 @@ const login = async (username, passphrase) => {
const logout = async () => {
const res = await fetch(`${BASE_URL}/v1/logout`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
});

console.log(res);
Expand All @@ -124,6 +176,11 @@ const logout = async () => {
const getConfig = async () => {
const res = await fetch(`${BASE_URL}/v1/config`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
});

console.log(res);
Expand All @@ -133,6 +190,11 @@ const getConfig = async () => {
const updateConfig = async (electrum_url) => {
const res = await fetch(`${BASE_URL}/v1/config`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': `macaroon=${MACAROON}; token=${TOKEN}`
},
credentials: 'include',
body: {
electrum_url,
},
Expand Down
45 changes: 24 additions & 21 deletions backend/sensei/nodes.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
const fetch = require('node-fetch');
const BASE_URL = process.env.BASE_URL;
const apiCall = require('../utils/apiCall');

const getUnusedAddress = async () => {
const res = await fetch(`${BASE_URL}/v1/node/wallet/address`);
console.log(res);
return await res.json();
const { address } = await apiCall('/v1/node/wallet/address', 'GET')
return await address.json();
}

const getBalance = async () => {
const res = await fetch(`${BASE_URL}/v1/node/wallet/balance`);
console.log(res);
return await res.json();
const { balance } = await apiCall('/v1/node/wallet/balance', 'GET')
return await balance.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'
});

const { channels, pagination } = await apiCall(`/v1/node/channels?page=${page}&take=${take}&query=${searchTerm}`, 'GET')
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}`);

return await res.json();
const getPayments = async (page, take) => {
const { payments } = await apiCall(`/v1/node/payments?page=${page}&take=${take}`, 'GET')
return await payments.json();
}

const getInfo = async () => {
const { node_info } = await fetch(`${BASE_URL}/v1/node/info`);
const { node_info } = await apiCall(`/v1/node/payments?page=${page}&take=${take}`, 'GET')

return {
version: node_info.version,
Expand All @@ -45,7 +35,6 @@ const getInfo = async () => {

const getPeers = async () => {
const { peers } = await fetch(`${BASE_URL}/v1/node/peers`);

return await peers.json();
}

Expand Down Expand Up @@ -102,3 +91,17 @@ const keysend = async (destPubkey, amtMsat) => {
}
);
}


module.exports = {
getUnusedAddress,
getBalance,
getChannels,
getPayments,
getInfo,
getPeers,
stopNode,
createInvoice,
payInvoice,
keysend,
}
18 changes: 18 additions & 0 deletions backend/utils/apiCall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fetch = require('node-fetch');
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),
});
};

module.exports = { apiCall }

0 comments on commit 25ea534

Please sign in to comment.