2019-09-02 04:11:37 +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');
|
2019-09-02 04:11:37 +00:00
|
|
|
var options = {};
|
|
|
|
|
2019-09-10 23:53:28 +00:00
|
|
|
exports.listChannels = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Getting Channels..'});
|
2019-09-10 23:53:28 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/channel/listChannels';
|
2019-09-10 23:53:28 +00:00
|
|
|
request(options).then(function (body) {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'List Channels', data: body});
|
2020-01-04 01:06:36 +00:00
|
|
|
body.map(channel => {
|
2020-08-17 19:58:00 +00:00
|
|
|
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;
|
2020-01-10 03:50:02 +00:00
|
|
|
channel.balancedness = (total == 0) ? 1 : (1 - Math.abs((local-remote)/total)).toFixed(3);
|
2021-06-20 20:27:08 +00:00
|
|
|
})
|
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channels Received'});
|
2019-09-10 23:53:28 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
})
|
2020-05-03 19:52:38 +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;
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-14 19:58:21 +00:00
|
|
|
exports.openChannel = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Opening Channel..'});
|
2019-09-14 19:58:21 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/channel/openChannel';
|
2019-09-14 19:58:21 +00:00
|
|
|
options.body = req.body;
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Open Channel Options', data: options.body});
|
2019-09-14 19:58:21 +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});
|
2020-03-10 17:25:52 +00:00
|
|
|
if(!body || body.error) {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Open Channel Error', error: body.error});
|
2019-09-14 19:58:21 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: 'Open Channel Failed!',
|
2020-03-10 17:25:52 +00:00
|
|
|
error: (!body) ? 'Error From Server!' : body.error
|
2019-09-14 19:58:21 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Opened'});
|
2019-09-14 19:58:21 +00:00
|
|
|
res.status(201).json(body);
|
|
|
|
}
|
|
|
|
})
|
2020-05-03 19:52:38 +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;
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Open Channel Error', error: err});
|
2019-09-14 19:58:21 +00:00
|
|
|
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) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Setting Channel Fee..'});
|
2019-09-10 23:53:28 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/channel/setChannelFee';
|
2019-09-10 23:53:28 +00:00
|
|
|
options.body = req.body;
|
2021-06-20 20:27:08 +00:00
|
|
|
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) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Update Channel Policy', data: body});
|
2020-03-10 17:25:52 +00:00
|
|
|
if(!body || body.error) {
|
2021-06-20 20:27:08 +00:00
|
|
|
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!',
|
2020-03-10 17:25:52 +00:00
|
|
|
error: (!body) ? 'Error From Server!' : body.error
|
2019-09-10 23:53:28 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Fee Set'});
|
2019-09-10 23:53:28 +00:00
|
|
|
res.status(201).json(body);
|
|
|
|
}
|
|
|
|
})
|
2020-05-03 19:52:38 +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;
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
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) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Closing Channel..'});
|
2020-10-12 22:43:16 +00:00
|
|
|
req.setTimeout(60000 * 10); // timeout 10 mins
|
2019-09-10 23:53:28 +00:00
|
|
|
options = common.getOptions();
|
2020-10-12 22:43:16 +00:00
|
|
|
const unilateralTimeoutQuery = req.query.force ? '?unilateralTimeout=1' : '';
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/channel/closeChannel/' + req.params.channelId + unilateralTimeoutQuery;
|
2021-06-20 20:27:08 +00:00
|
|
|
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) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Close Channel Response', data: body});
|
2020-03-10 17:25:52 +00:00
|
|
|
if(!body || body.error) {
|
2021-06-20 20:27:08 +00:00
|
|
|
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!',
|
2020-03-10 17:25:52 +00:00
|
|
|
error: (!body) ? 'Error From Server!' : body.error
|
2019-09-10 23:53:28 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Closed'});
|
2019-09-10 23:53:28 +00:00
|
|
|
res.status(204).json(body);
|
|
|
|
}
|
|
|
|
})
|
2020-05-03 19:52:38 +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;
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
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) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Getting Local & Remote Balances..'});
|
2019-09-02 04:11:37 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/channel/localremotebal';
|
2019-09-02 04:11:37 +00:00
|
|
|
request(options).then(function (body) {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Local Remote Balance', data: body});
|
2020-03-10 17:25:52 +00:00
|
|
|
if(!body.localBalance) {
|
2019-09-02 04:11:37 +00:00
|
|
|
body.localBalance = 0;
|
|
|
|
}
|
2020-03-10 17:25:52 +00:00
|
|
|
if(!body.remoteBalance) {
|
2019-09-02 04:11:37 +00:00
|
|
|
body.remoteBalance = 0;
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Local & Remote Balances Received'});
|
2019-09-02 04:11:37 +00:00
|
|
|
res.status(200).json(body);
|
|
|
|
})
|
2020-05-03 19:52:38 +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;
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
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) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Getting Channel List Forwards..'});
|
2019-09-08 19:09:00 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/channel/listForwards/';
|
2019-09-12 01:42:00 +00:00
|
|
|
request.get(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Forwarding History Response', data: body});
|
2020-03-10 17:25:52 +00:00
|
|
|
if(!body || body.error) {
|
2021-06-20 20:27:08 +00:00
|
|
|
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!",
|
2020-03-10 17:25:52 +00:00
|
|
|
error: (!body) ? 'Error From Server!' : body.error
|
2019-09-08 19:09:00 +00:00
|
|
|
});
|
|
|
|
} else {
|
2019-10-27 20:39:40 +00:00
|
|
|
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
|
|
|
}
|
2021-06-20 20:27:08 +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
|
|
|
}
|
|
|
|
})
|
2020-05-03 19:52:38 +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;
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|