2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-11-07 15:20:31 +00:00
RTL/controllers/eclair/channels.js
ShahanaFarooqui e45d6d598a
Release 0.11.0 (#713)
- Package updates
    Updated docker NodeJS from 10 to 14
    Updated Angular from 11 to 12
    Updated Material from 11 to 12
    Updated Angular cli from 11 to 12
    Updated Karma from 5 to 6
    Updated rxjs from 6 to 7
    Updated ngrx from 10 to 12
    Updated angularx-qrcode from 10 to 11
    Updated @angular/flex-layout from 11 to 12
    Updated angular-user-idle from 2.2.4 to 2.2.5
    Updated typescript from 4.0.2 to 4.2.4
    Updated zone.js from 0.10.2 to 0.11.4
    Migrated from TSLint to ESLint
    Installed save-dev crypto-browserify & stream-browserify

- Mask password with fixed length #689
- CSRF Token (#696)
- Route lock default password (#700)
- ECL Invoice amount mislabeled #694
- ECL & LND Fee report time zone offset bug fixes #692 & #693
- Loop remove max routing fee validation #690
- Child route refresh bug
- Adding Password Blacklist (#704)
- Fee rate in percentage #621 (#705)
- ECL Adding BaseFee and FeeRate on Channels
- LND Invoice and Payment pagination fix (#707)
- Keysend missing QR code bug fix
- Login page XS layout fix
- Reports tables load time improved (#709)
- Report initial table load bug fix
2021-06-20 16:27:08 -04:00

246 lines
13 KiB
JavaScript

var request = require('request-promise');
var common = require('../../routes/common');
var logger = require('../shared/logger');
var options = {};
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');
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});
return ({activeChannels: activeChannels, pendingChannels: pendingChannels, inactiveChannels: inactiveChannels, lightningBalances: lightningBalances, channelStatus: channelStatus});
};
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 : '',
isFunder: channel.data && channel.data.commitments && channel.data.commitments.localParams && channel.data.commitments.localParams.isFunder ? channel.data.commitments.localParams.isFunder : false,
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,
feeRatePerKw: (channel.data.commitments.localCommit.spec.feeratePerKw) ? channel.data.commitments.localCommit.spec.feeratePerKw : 0,
feeProportionalMillionths: channel.data && channel.data.channelUpdate && channel.data.channelUpdate.feeProportionalMillionths ? channel.data.channelUpdate.feeProportionalMillionths : 0,
alias: ''
});
});
channelNodeIds = channelNodeIds.substring(1);
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;
});
};
exports.getChannels = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'List Channels..'});
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/channels';
options.form = {};
if (req.query && req.query.nodeId) {
options.form = req.query;
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Channels Node Id', data: options.form});
}
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Options', data: options});
if (common.read_dummy_data) {
common.getDummyData('Channels').then(function(data) { res.status(200).json(arrangeChannels(data)); });
} else {
request.post(options).then(function (body) {
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'All Channels', data: body});
if(body && body.length) {
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'});
res.status(200).json(arrangeChannels(simplifiedChannels));
});
} else {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Empty Channels List Received'});
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 => {
let err = JSON.parse(JSON.stringify(errRes));
if (err.options && err.options.headers && err.options.headers.authorization) {
delete err.options.headers.authorization;
}
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.authorization) {
delete err.response.request.headers.authorization;
}
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Get Channels Error', error: err});
return res.status(err.statusCode ? err.statusCode : 500).json({
message: 'Fetching Channels Failed!',
error: err.error && err.error.error ? err.error.error : err.error ? err.error : "Unknown Server Error"
});
});
}
};
exports.getChannelStats = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Getting Channel States..'});
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/channelstats';
options.form = {};
request.post(options).then((body) => {
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Channel Stats Response', data: body});
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel States Received'});
res.status(201).json(body);
})
.catch(errRes => {
let err = JSON.parse(JSON.stringify(errRes));
if (err.options && err.options.headers && err.options.headers.authorization) {
delete err.options.headers.authorization;
}
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.authorization) {
delete err.response.request.headers.authorization;
}
logger.log({level: 'ERROR', fileName: 'ChannelStats', msg: 'Get Channel Stats Error', error: err});
return res.status(err.statusCode ? err.statusCode : 500).json({
message: "Channel Stats Failed!",
error: err.error && err.error.error ? err.error.error : err.error ? err.error : "Unknown Server Error"
});
});
}
exports.openChannel = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Opening Channel..'});
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/open';
options.form = req.body;
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Open Channel Params', data: options.form});
request.post(options).then((body) => {
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Open Channel Response', data: body});
if(!body || body.error) {
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Open Channel Error', error: body.error});
res.status(500).json({
message: 'Open Channel Failed!',
error: (!body) ? 'Error From Server!' : body.error
});
} else {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Opened'});
res.status(201).json(body);
}
})
.catch(errRes => {
let err = JSON.parse(JSON.stringify(errRes));
if (err.options && err.options.headers && err.options.headers.authorization) {
delete err.options.headers.authorization;
}
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.authorization) {
delete err.response.request.headers.authorization;
}
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Open Channel Error', error: err});
return res.status(err.statusCode ? err.statusCode : 500).json({
message: "Open Channel Failed!",
error: err.error && err.error.error ? err.error.error : err.error ? err.error : "Unknown Server Error"
});
});
}
exports.updateChannelRelayFee = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Updating Channel Relay Fee..'});
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/updaterelayfee';
options.form = req.query;
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Update Relay Fee Params', data: options.form});
request.post(options).then((body) => {
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Update Relay Fee Response', data: body});
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Relay Fee Updated'});
res.status(201).json(body);
})
.catch(errRes => {
let err = JSON.parse(JSON.stringify(errRes));
if (err.options && err.options.headers && err.options.headers.authorization) {
delete err.options.headers.authorization;
}
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.authorization) {
delete err.response.request.headers.authorization;
}
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Update Relay Fee Error', error: err});
return res.status(err.statusCode ? err.statusCode : 500).json({
message: "Update Relay Fee Failed!",
error: err.error && err.error.error ? err.error.error : err.error ? err.error : "Unknown Server Error"
});
});
}
exports.closeChannel = (req, res, next) => {
options = common.getOptions();
if (req.query.force !== 'true') {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Closing Channel..'});
options.url = common.getSelLNServerUrl() + '/close';
} else {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Force Closing Channel..'});
options.url = common.getSelLNServerUrl() + '/forceclose';
}
options.form = { channelId: req.query.channelId };
logger.log({level: 'DEBUG', fileName: 'Channels', msg: '[Close URL, Close Params]', data: [options.url, options.form]});
request.post(options).then((body) => {
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Close Channel Response', data: body});
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Closed'});
res.status(204).json(body);
})
.catch(errRes => {
let err = JSON.parse(JSON.stringify(errRes));
if (err.options && err.options.headers && err.options.headers.authorization) {
delete err.options.headers.authorization;
}
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.authorization) {
delete err.response.request.headers.authorization;
}
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Close Channel Error', error: err});
return res.status(err.statusCode ? err.statusCode : 500).json({
message: "Close Channel Failed!",
error: err.error && err.error.error ? err.error.error : err.error ? err.error : "Unknown Server Error"
});
});
}