2021-12-29 23:08:41 +00:00
|
|
|
import request from 'request-promise';
|
|
|
|
import { Logger, LoggerService } from '../../utils/logger.js';
|
|
|
|
import { Common, CommonService } from '../../utils/common.js';
|
2023-12-06 04:32:05 +00:00
|
|
|
import { CommonSelectedNode } from '../../models/config.model.js';
|
|
|
|
|
2021-12-29 23:08:41 +00:00
|
|
|
let options = null;
|
|
|
|
const logger: LoggerService = Logger;
|
|
|
|
const common: CommonService = Common;
|
|
|
|
|
|
|
|
export const getRoute = (req, res, next) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Network', msg: 'Getting Network Routes..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
2023-12-06 04:32:05 +00:00
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/getroute';
|
|
|
|
options.body = req.body;
|
|
|
|
request.post(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Network', msg: 'Network Routes Received', data: body });
|
2023-12-06 04:32:05 +00:00
|
|
|
return Promise.all(body.route?.map((rt) => getAlias(req.session.selectedNode, rt, 'id'))).then((values) => {
|
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Network Routes with Alias Received', data: body });
|
|
|
|
res.status(200).json(body || []);
|
|
|
|
});
|
2021-12-29 23:08:41 +00:00
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Network', 'Query Routes Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-12-06 04:32:05 +00:00
|
|
|
export const listChannels = (req, res, next) => {
|
2021-12-29 23:08:41 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Network', msg: 'Channel Lookup..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
2023-12-06 04:32:05 +00:00
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/listchannels';
|
|
|
|
options.body = req.body;
|
|
|
|
request.post(options).then((body) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Network', msg: 'Channel Lookup Finished', data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Network', 'Channel Lookup Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const feeRates = (req, res, next) => {
|
2024-03-14 22:07:06 +00:00
|
|
|
const { style } = req.body;
|
2021-12-29 23:08:41 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Network', msg: 'Getting Network Fee Rates..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
2023-12-06 04:32:05 +00:00
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/feerates';
|
|
|
|
options.body = req.body;
|
|
|
|
request.post(options).then((body) => {
|
2024-03-14 22:07:06 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Network', msg: 'Network Fee Rates Received for ' + style, data: body });
|
2021-12-29 23:08:41 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Network', 'Fee Rates Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
2022-05-17 02:53:13 +00:00
|
|
|
|
|
|
|
export const listNodes = (req, res, next) => {
|
2024-03-14 22:07:06 +00:00
|
|
|
const filter_liquidity_ads = !!req.body.liquidity_ads;
|
|
|
|
delete req.body.liquidity_ads;
|
2022-05-17 02:53:13 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Network', msg: 'List Nodes..' });
|
|
|
|
options = common.getOptions(req);
|
|
|
|
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
|
2023-12-06 04:32:05 +00:00
|
|
|
options.url = req.session.selectedNode.ln_server_url + '/v1/listnodes';
|
|
|
|
options.body = req.body;
|
2022-05-19 19:47:41 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Network', msg: 'List Nodes URL' + options.url });
|
2023-12-06 04:32:05 +00:00
|
|
|
request.post(options).then((body) => {
|
2022-05-17 02:53:13 +00:00
|
|
|
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Network', msg: 'List Nodes Finished', data: body });
|
2023-12-06 04:32:05 +00:00
|
|
|
let response = body.nodes;
|
|
|
|
if (filter_liquidity_ads) {
|
|
|
|
response = body.nodes.filter((node) => ((node.option_will_fund) ? node : null));
|
|
|
|
}
|
|
|
|
res.status(200).json(response);
|
2022-05-17 02:53:13 +00:00
|
|
|
}).catch((errRes) => {
|
|
|
|
const err = common.handleError(errRes, 'Network', 'Node Lookup Error', req.session.selectedNode);
|
|
|
|
return res.status(err.statusCode).json({ message: err.message, error: err.error });
|
|
|
|
});
|
|
|
|
};
|
2023-12-06 04:32:05 +00:00
|
|
|
|
|
|
|
export const getAlias = (selNode: CommonSelectedNode, peer: any, id: string) => {
|
|
|
|
options.url = selNode.ln_server_url + '/v1/listnodes';
|
|
|
|
if (!peer[id]) {
|
|
|
|
logger.log({ selectedNode: selNode, level: 'ERROR', fileName: 'Network', msg: 'Empty Peer ID' });
|
|
|
|
peer.alias = '';
|
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
options.body = { id : peer[id] };
|
|
|
|
return request.post(options).then((body) => {
|
|
|
|
logger.log({ selectedNode: selNode, level: 'DEBUG', fileName: 'Network', msg: 'Peer Alias Finished', data: body });
|
|
|
|
peer.alias = body.nodes[0] && body.nodes[0].alias ? body.nodes[0].alias : peer[id].substring(0, 20);
|
|
|
|
return peer;
|
|
|
|
}).catch((errRes) => {
|
|
|
|
common.handleError(errRes, 'Network', 'Peer Alias Error', selNode);
|
|
|
|
peer.alias = peer[id].substring(0, 20);
|
|
|
|
return peer;
|
|
|
|
});
|
|
|
|
};
|