2020-07-07 17:57:15 +00:00
|
|
|
var request = require('request-promise');
|
2021-06-20 20:27:08 +00:00
|
|
|
var common = require('../../routes/common');
|
2021-02-21 19:02:31 +00:00
|
|
|
var logger = require('../shared/logger');
|
2020-07-07 17:57:15 +00:00
|
|
|
var options = {};
|
|
|
|
|
2021-02-21 19:02:31 +00:00
|
|
|
arrangeChannels = (simplifiedChannels) => {
|
|
|
|
let channelTotal = 0;
|
|
|
|
let totalLocalBalance = 0;
|
|
|
|
let totalRemoteBalance = 0;
|
|
|
|
let lightningBalances = { localBalance: 0, remoteBalance: 0 };
|
|
|
|
let channelStatus = {active: { channels: 0, capacity: 0 }, inactive: { channels: 0, capacity: 0 }, pending: { channels: 0, capacity: 0 }};
|
|
|
|
let activeChannels = [];
|
|
|
|
let pendingChannels = [];
|
|
|
|
let inactiveChannels = [];
|
|
|
|
simplifiedChannels.forEach((channel, i) => {
|
|
|
|
if (channel.state === 'NORMAL') {
|
|
|
|
channelTotal = channel.toLocal + channel.toRemote;
|
|
|
|
totalLocalBalance = totalLocalBalance + channel.toLocal;
|
|
|
|
totalRemoteBalance = totalRemoteBalance + channel.toRemote;
|
|
|
|
channel.balancedness = (channelTotal == 0) ? 1 : (1 - Math.abs((channel.toLocal-channel.toRemote)/channelTotal)).toFixed(3);
|
|
|
|
activeChannels.push(channel);
|
|
|
|
channelStatus.active.channels = channelStatus.active.channels + 1;
|
|
|
|
channelStatus.active.capacity = channelStatus.active.capacity + channel.toLocal;
|
|
|
|
} else if (channel.state.includes('WAIT') || channel.state.includes('CLOSING') || channel.state.includes('SYNCING')) {
|
|
|
|
channel.state = channel.state.replace(/_/g, ' ');
|
|
|
|
pendingChannels.push(channel);
|
|
|
|
channelStatus.pending.channels = channelStatus.pending.channels + 1;
|
|
|
|
channelStatus.pending.capacity = channelStatus.pending.capacity + channel.toLocal;
|
|
|
|
} else {
|
|
|
|
channel.state = channel.state.replace(/_/g, ' ');
|
|
|
|
inactiveChannels.push(channel);
|
|
|
|
channelStatus.inactive.channels = channelStatus.inactive.channels + 1;
|
|
|
|
channelStatus.inactive.capacity = channelStatus.inactive.capacity + channel.toLocal;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
lightningBalances = { localBalance: totalLocalBalance, remoteBalance: totalRemoteBalance };
|
|
|
|
activeChannels = common.sortDescByKey(activeChannels, 'balancedness');
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Lightning Balances', data: lightningBalances});
|
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Active Channels', data: activeChannels});
|
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Pending Channels', data: pendingChannels});
|
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Inactive Channels', data: inactiveChannels});
|
2021-02-21 19:02:31 +00:00
|
|
|
return ({activeChannels: activeChannels, pendingChannels: pendingChannels, inactiveChannels: inactiveChannels, lightningBalances: lightningBalances, channelStatus: channelStatus});
|
|
|
|
};
|
|
|
|
|
2020-07-07 17:57:15 +00:00
|
|
|
simplifyAllChannels = (channels) => {
|
|
|
|
let channelNodeIds = '';
|
|
|
|
let simplifiedChannels = [];
|
|
|
|
channels.forEach(channel => {
|
|
|
|
channelNodeIds = channelNodeIds + ',' + channel.nodeId;
|
|
|
|
simplifiedChannels.push({
|
|
|
|
nodeId: channel.nodeId ? channel.nodeId : '',
|
|
|
|
channelId: channel.channelId ? channel.channelId : '',
|
|
|
|
state: channel.state ? channel.state : '',
|
|
|
|
channelFlags: channel.data && channel.data.commitments && channel.data.commitments.channelFlags ? channel.data.commitments.channelFlags : 0,
|
|
|
|
toLocal: (channel.data.commitments.localCommit.spec.toLocal) ? Math.round(+channel.data.commitments.localCommit.spec.toLocal/1000) : 0,
|
|
|
|
toRemote: (channel.data.commitments.localCommit.spec.toRemote) ? Math.round(+channel.data.commitments.localCommit.spec.toRemote/1000) : 0,
|
|
|
|
shortChannelId: channel.data && channel.data.shortChannelId ? channel.data.shortChannelId : '',
|
2020-10-30 00:37:49 +00:00
|
|
|
isFunder: channel.data && channel.data.commitments && channel.data.commitments.localParams && channel.data.commitments.localParams.isFunder ? channel.data.commitments.localParams.isFunder : false,
|
2020-07-07 17:57:15 +00:00
|
|
|
buried: channel.data && channel.data.buried ? channel.data.buried : false,
|
|
|
|
feeBaseMsat: channel.data && channel.data.channelUpdate && channel.data.channelUpdate.feeBaseMsat ? channel.data.channelUpdate.feeBaseMsat : 0,
|
2021-06-20 20:27:08 +00:00
|
|
|
feeRatePerKw: (channel.data.commitments.localCommit.spec.feeratePerKw) ? channel.data.commitments.localCommit.spec.feeratePerKw : 0,
|
2020-07-07 17:57:15 +00:00
|
|
|
feeProportionalMillionths: channel.data && channel.data.channelUpdate && channel.data.channelUpdate.feeProportionalMillionths ? channel.data.channelUpdate.feeProportionalMillionths : 0,
|
|
|
|
alias: ''
|
|
|
|
});
|
|
|
|
});
|
|
|
|
channelNodeIds = channelNodeIds.substring(1);
|
2021-06-20 20:27:08 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/nodes';
|
|
|
|
options.form = { nodeIds: channelNodeIds };
|
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Node Ids to find alias', data: channelNodeIds});
|
|
|
|
return request.post(options).then(function(nodes) {
|
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Filtered Nodes', data: nodes});
|
|
|
|
let foundPeer = {};
|
|
|
|
simplifiedChannels.map(channel => {
|
|
|
|
foundPeer = nodes.find(channelWithAlias => channel.nodeId === channelWithAlias.nodeId);
|
|
|
|
channel.alias = foundPeer ? foundPeer.alias : channel.nodeId.substring(0, 20);
|
|
|
|
});
|
|
|
|
return simplifiedChannels;
|
|
|
|
}).catch(err => {
|
|
|
|
return simplifiedChannels;
|
2020-07-07 17:57:15 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getChannels = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'List Channels..'});
|
2020-07-07 17:57:15 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/channels';
|
|
|
|
options.form = {};
|
|
|
|
if (req.query && req.query.nodeId) {
|
|
|
|
options.form = req.query;
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Channels Node Id', data: options.form});
|
2020-07-07 17:57:15 +00:00
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Options', data: options});
|
2021-02-21 19:02:31 +00:00
|
|
|
if (common.read_dummy_data) {
|
|
|
|
common.getDummyData('Channels').then(function(data) { res.status(200).json(arrangeChannels(data)); });
|
|
|
|
} else {
|
|
|
|
request.post(options).then(function (body) {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'All Channels', data: body});
|
2021-08-17 11:21:01 +00:00
|
|
|
if (body && body.length) {
|
2021-06-20 20:27:08 +00:00
|
|
|
return simplifyAllChannels(body).then(function(simplifiedChannels) {
|
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Simplified Channels with Alias', data: simplifiedChannels});
|
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channels List Received'});
|
2021-02-21 19:02:31 +00:00
|
|
|
res.status(200).json(arrangeChannels(simplifiedChannels));
|
2020-07-07 17:57:15 +00:00
|
|
|
});
|
2021-02-21 19:02:31 +00:00
|
|
|
} else {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Empty Channels List Received'});
|
2021-02-21 19:02:31 +00:00
|
|
|
res.status(200).json({activeChannels: [], pendingChannels: [], inactiveChannels: [], lightningBalances: { localBalance: 0, remoteBalance: 0 }, channelStatus: {active: { channels: 0, capacity: 0 }, inactive: { channels: 0, capacity: 0 }, pending: { channels: 0, capacity: 0 }}});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(errRes => {
|
2021-08-17 11:21:01 +00:00
|
|
|
const err = common.handleError(errRes, 'Channels', 'List Channels Error');
|
|
|
|
return res.status(err.statusCode).json({message: err.message, error: err.error});
|
2020-07-07 17:57:15 +00:00
|
|
|
});
|
2021-02-21 19:02:31 +00:00
|
|
|
}
|
2020-07-07 17:57:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getChannelStats = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Getting Channel States..'});
|
2020-07-07 17:57:15 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/channelstats';
|
|
|
|
options.form = {};
|
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Channel Stats Response', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel States Received'});
|
2020-07-07 17:57:15 +00:00
|
|
|
res.status(201).json(body);
|
|
|
|
})
|
|
|
|
.catch(errRes => {
|
2021-08-17 11:21:01 +00:00
|
|
|
const err = common.handleError(errRes, 'Channels', 'Get Channel Stats Error');
|
|
|
|
return res.status(err.statusCode).json({message: err.message, error: err.error});
|
2020-07-07 17:57:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.openChannel = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Opening Channel..'});
|
2020-07-07 17:57:15 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/open';
|
|
|
|
options.form = req.body;
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Open Channel Params', data: options.form});
|
2020-07-07 17:57:15 +00:00
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Open Channel Response', data: body});
|
2021-08-17 11:21:01 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Opened'});
|
|
|
|
res.status(201).json(body);
|
2020-07-07 17:57:15 +00:00
|
|
|
})
|
|
|
|
.catch(errRes => {
|
2021-08-17 11:21:01 +00:00
|
|
|
const err = common.handleError(errRes, 'Channels', 'Open Channel Error');
|
|
|
|
return res.status(err.statusCode).json({message: err.message, error: err.error});
|
2020-07-07 17:57:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.updateChannelRelayFee = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Updating Channel Relay Fee..'});
|
2020-07-07 17:57:15 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/updaterelayfee';
|
|
|
|
options.form = req.query;
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Update Relay Fee Params', data: options.form});
|
2020-07-07 17:57:15 +00:00
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Update Relay Fee Response', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Relay Fee Updated'});
|
2020-07-07 17:57:15 +00:00
|
|
|
res.status(201).json(body);
|
|
|
|
})
|
|
|
|
.catch(errRes => {
|
2021-08-17 11:21:01 +00:00
|
|
|
const err = common.handleError(errRes, 'Channels', 'Update Relay Fee Error');
|
|
|
|
return res.status(err.statusCode).json({message: err.message, error: err.error});
|
2020-07-07 17:57:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.closeChannel = (req, res, next) => {
|
|
|
|
options = common.getOptions();
|
2020-10-30 00:37:49 +00:00
|
|
|
if (req.query.force !== 'true') {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Closing Channel..'});
|
2020-07-07 17:57:15 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/close';
|
|
|
|
} else {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Force Closing Channel..'});
|
2020-07-07 17:57:15 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/forceclose';
|
|
|
|
}
|
|
|
|
options.form = { channelId: req.query.channelId };
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: '[Close URL, Close Params]', data: [options.url, options.form]});
|
2020-07-07 17:57:15 +00:00
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Close Channel Response', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Closed'});
|
2020-07-07 17:57:15 +00:00
|
|
|
res.status(204).json(body);
|
|
|
|
})
|
|
|
|
.catch(errRes => {
|
2021-08-17 11:21:01 +00:00
|
|
|
const err = common.handleError(errRes, 'Channels', 'Close Channel Error');
|
|
|
|
return res.status(err.statusCode).json({message: err.message, error: err.error});
|
2020-07-07 17:57:15 +00:00
|
|
|
});
|
|
|
|
}
|
2021-02-21 19:02:31 +00:00
|
|
|
|