2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-11-09 13:10:44 +00:00
RTL/controllers/c-lightning/channels.js

211 lines
9.1 KiB
JavaScript
Raw Normal View History

2019-09-02 04:11:37 +00:00
var request = require('request-promise');
var common = require('../../routes/common');
var logger = require('../shared/logger');
2019-09-02 04:11:37 +00:00
var options = {};
2019-09-10 23:53:28 +00:00
exports.listChannels = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Getting Channels..'});
2019-09-10 23:53:28 +00:00
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/v1/channel/listChannels';
2019-09-10 23:53:28 +00:00
request(options).then(function (body) {
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'List Channels', data: body});
2020-01-04 01:06:36 +00:00
body.map(channel => {
if (!channel.alias || channel.alias === '') { channel.alias = channel.id.substring(0, 20); }
2020-01-05 04:28:40 +00:00
local = (channel.msatoshi_to_us) ? channel.msatoshi_to_us : 0;
remote = (channel.msatoshi_to_them) ? channel.msatoshi_to_them : 0;
total = channel.msatoshi_total ? channel.msatoshi_total : 0;
channel.balancedness = (total == 0) ? 1 : (1 - Math.abs((local-remote)/total)).toFixed(3);
})
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channels Received'});
2019-09-10 23:53:28 +00:00
res.status(200).json(body);
})
.catch(errRes => {
let err = JSON.parse(JSON.stringify(errRes));
if (err.options && err.options.headers && err.options.headers.macaroon) {
delete err.options.headers.macaroon;
}
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.macaroon) {
delete err.response.request.headers.macaroon;
}
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'List Channels Error', error: err});
2019-09-10 23:53:28 +00:00
return res.status(500).json({
message: 'Fetching List Channels Failed!',
error: err.error
});
});
}
exports.openChannel = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Opening Channel..'});
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/v1/channel/openChannel';
options.body = req.body;
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Open Channel Options', data: options.body});
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.macaroon) {
delete err.options.headers.macaroon;
}
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.macaroon) {
delete err.response.request.headers.macaroon;
}
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Open Channel Error', error: err});
return res.status(500).json({
message: 'Open Channel Failed!',
error: err.error
});
});
}
2019-09-10 23:53:28 +00:00
exports.setChannelFee = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Setting Channel Fee..'});
2019-09-10 23:53:28 +00:00
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/v1/channel/setChannelFee';
2019-09-10 23:53:28 +00:00
options.body = req.body;
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Update Channel Policy Options', data: options.body});
2019-09-10 23:53:28 +00:00
request.post(options).then((body) => {
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Update Channel Policy', data: body});
if(!body || body.error) {
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Update Channel Policy Error', error: body.error});
2019-09-10 23:53:28 +00:00
res.status(500).json({
message: 'Update Channel Policy Failed!',
error: (!body) ? 'Error From Server!' : body.error
2019-09-10 23:53:28 +00:00
});
} else {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Fee Set'});
2019-09-10 23:53:28 +00:00
res.status(201).json(body);
}
})
.catch(errRes => {
let err = JSON.parse(JSON.stringify(errRes));
if (err.options && err.options.headers && err.options.headers.macaroon) {
delete err.options.headers.macaroon;
}
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.macaroon) {
delete err.response.request.headers.macaroon;
}
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Update Channel Policy Error', error: err});
2019-09-10 23:53:28 +00:00
return res.status(500).json({
message: 'Update Channel Policy Failed!',
error: err.error
});
});
}
exports.closeChannel = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Closing Channel..'});
req.setTimeout(60000 * 10); // timeout 10 mins
2019-09-10 23:53:28 +00:00
options = common.getOptions();
const unilateralTimeoutQuery = req.query.force ? '?unilateralTimeout=1' : '';
options.url = common.getSelLNServerUrl() + '/v1/channel/closeChannel/' + req.params.channelId + unilateralTimeoutQuery;
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Closing Channel', data: options.url});
2019-09-10 23:53:28 +00:00
request.delete(options).then((body) => {
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Close Channel Response', data: body});
if(!body || body.error) {
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Close Channel Error', error: body.error});
2019-09-10 23:53:28 +00:00
res.status(500).json({
message: 'Close Channel Failed!',
error: (!body) ? 'Error From Server!' : body.error
2019-09-10 23:53:28 +00:00
});
} else {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Closed'});
2019-09-10 23:53:28 +00:00
res.status(204).json(body);
}
})
.catch(errRes => {
let err = JSON.parse(JSON.stringify(errRes));
if (err.options && err.options.headers && err.options.headers.macaroon) {
delete err.options.headers.macaroon;
}
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.macaroon) {
delete err.response.request.headers.macaroon;
}
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Close Channel Error', error: err});
2019-09-10 23:53:28 +00:00
return res.status(500).json({
message: 'Close Channel Failed!',
error: err.error
});
});
}
2019-09-02 04:11:37 +00:00
exports.getLocalRemoteBalance = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Getting Local & Remote Balances..'});
2019-09-02 04:11:37 +00:00
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/v1/channel/localremotebal';
2019-09-02 04:11:37 +00:00
request(options).then(function (body) {
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Local Remote Balance', data: body});
if(!body.localBalance) {
2019-09-02 04:11:37 +00:00
body.localBalance = 0;
}
if(!body.remoteBalance) {
2019-09-02 04:11:37 +00:00
body.remoteBalance = 0;
}
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Local & Remote Balances Received'});
2019-09-02 04:11:37 +00:00
res.status(200).json(body);
})
.catch(errRes => {
let err = JSON.parse(JSON.stringify(errRes));
if (err.options && err.options.headers && err.options.headers.macaroon) {
delete err.options.headers.macaroon;
}
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.macaroon) {
delete err.response.request.headers.macaroon;
}
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Local Remote Balance Error', error: err});
2019-09-02 04:11:37 +00:00
return res.status(500).json({
message: 'Fetching Local Remote Balance Failed!',
error: err.error
});
});
};
2019-09-08 19:09:00 +00:00
2019-09-10 23:53:28 +00:00
exports.listForwards = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Getting Channel List Forwards..'});
2019-09-08 19:09:00 +00:00
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/v1/channel/listForwards/';
2019-09-12 01:42:00 +00:00
request.get(options).then((body) => {
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Forwarding History Response', data: body});
if(!body || body.error) {
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Forwarding History Error', error: body.error});
2019-09-08 19:09:00 +00:00
res.status(500).json({
2019-09-12 01:42:00 +00:00
message: "Forwarding History Failed!",
error: (!body) ? 'Error From Server!' : body.error
2019-09-08 19:09:00 +00:00
});
} else {
if (body && body.length > 0) {
2019-09-12 01:42:00 +00:00
body = common.sortDescByKey(body, 'received_time');
2019-09-08 19:09:00 +00:00
}
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Forwarding History Received', data: body});
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel List Forwards Received'});
2019-09-12 01:42:00 +00:00
res.status(200).json({ last_offset_index: 0, forwarding_events: body });
2019-09-08 19:09:00 +00:00
}
})
.catch(errRes => {
let err = JSON.parse(JSON.stringify(errRes));
if (err.options && err.options.headers && err.options.headers.macaroon) {
delete err.options.headers.macaroon;
}
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers.macaroon) {
delete err.response.request.headers.macaroon;
}
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Forwarding History Error', error: err});
2019-09-08 19:09:00 +00:00
return res.status(500).json({
2019-09-12 01:42:00 +00:00
message: "Forwarding History Failed!",
2019-09-08 19:09:00 +00:00
error: err.error
});
});
};