2019-01-09 03:04:34 +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-03-02 17:55:19 +00:00
|
|
|
var options = {};
|
2018-11-23 01:30:30 +00:00
|
|
|
|
2019-12-04 23:14:34 +00:00
|
|
|
getAliasForChannel = (channel) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
let pubkey = (channel.remote_pubkey) ? channel.remote_pubkey : (channel.remote_node_pub) ? channel.remote_node_pub : '';
|
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/graph/node/' + pubkey;
|
|
|
|
return request(options).then(function(aliasBody) {
|
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Alias', data: aliasBody.node.alias});
|
|
|
|
channel.remote_alias = aliasBody.node.alias;
|
|
|
|
return aliasBody.node.alias;
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
channel.remote_alias = pubkey.slice(0, 10) + '...' + pubkey.slice(-10);
|
|
|
|
return pubkey;
|
2018-11-24 05:41:21 +00:00
|
|
|
});
|
|
|
|
}
|
2018-09-15 01:31:01 +00:00
|
|
|
|
2019-12-04 23:14:34 +00:00
|
|
|
exports.getAllChannels = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Getting Channels..'});
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/channels';
|
2018-09-15 01:31:01 +00:00
|
|
|
options.qs = req.query;
|
2019-12-26 01:13:21 +00:00
|
|
|
let local = 0;
|
|
|
|
let remote = 0;
|
|
|
|
let total = 0;
|
2018-11-23 01:30:30 +00:00
|
|
|
request(options).then(function (body) {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'All Channels Received', data: body});
|
2019-12-04 23:14:34 +00:00
|
|
|
if(body.channels) {
|
2021-06-20 20:27:08 +00:00
|
|
|
return Promise.all(
|
2019-12-05 20:16:41 +00:00
|
|
|
body.channels.map(channel => {
|
2019-12-26 01:13:21 +00:00
|
|
|
local = (channel.local_balance) ? +channel.local_balance : 0;
|
|
|
|
remote = (channel.remote_balance) ? +channel.remote_balance : 0;
|
|
|
|
total = local + remote;
|
2020-01-10 03:50:02 +00:00
|
|
|
channel.balancedness = (total == 0) ? 1 : (1 - Math.abs((local-remote)/total)).toFixed(3);
|
2019-12-04 23:14:34 +00:00
|
|
|
return getAliasForChannel(channel);
|
2018-12-25 04:01:31 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.then(function(values) {
|
2019-12-05 20:16:41 +00:00
|
|
|
body.channels = common.sortDescByKey(body.channels, 'balancedness');
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'All Channels with Alias', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channels Received'});
|
2018-12-25 04:01:31 +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['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.options.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.response.request.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Get All Channel Alias Error', error: err});
|
2019-12-04 23:14:34 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: 'Fetching Channels Alias Failed!',
|
|
|
|
error: err.error
|
|
|
|
});
|
2018-12-25 04:01:31 +00:00
|
|
|
});
|
2018-11-24 05:41:21 +00:00
|
|
|
} else {
|
2019-12-04 23:14:34 +00:00
|
|
|
body.channels = [];
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Empty Channels Received'});
|
2018-09-15 01:31:01 +00:00
|
|
|
res.status(200).json(body);
|
2018-12-25 04:01:31 +00:00
|
|
|
}
|
2019-01-01 16:26:51 +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['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.options.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.response.request.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Get All Channels Error', error: err});
|
2019-12-04 23:14:34 +00:00
|
|
|
return res.status(500).json({
|
|
|
|
message: 'Fetching All Channels Failed!',
|
|
|
|
error: err.error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getPendingChannels = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Getting Pending Channels..'});
|
2019-12-04 23:14:34 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/channels/pending';
|
2019-12-04 23:14:34 +00:00
|
|
|
options.qs = req.query;
|
|
|
|
request(options).then(function (body) {
|
2020-03-10 17:25:52 +00:00
|
|
|
if (!body.total_limbo_balance) {
|
2019-12-04 23:14:34 +00:00
|
|
|
body.total_limbo_balance = 0;
|
|
|
|
}
|
2020-05-03 19:52:38 +00:00
|
|
|
const promises = [];
|
|
|
|
if(body.pending_open_channels && body.pending_open_channels.length > 0) {
|
|
|
|
body.pending_open_channels.map(channel => { return promises.push(getAliasForChannel(channel.channel))});
|
|
|
|
}
|
|
|
|
if(body.pending_closing_channels && body.pending_closing_channels.length > 0) {
|
|
|
|
body.pending_closing_channels.map(channel => { return promises.push(getAliasForChannel(channel.channel))});
|
|
|
|
}
|
|
|
|
if(body.pending_force_closing_channels && body.pending_force_closing_channels.length > 0) {
|
|
|
|
body.pending_force_closing_channels.map(channel => { return promises.push(getAliasForChannel(channel.channel))});
|
|
|
|
}
|
|
|
|
if(body.waiting_close_channels && body.waiting_close_channels.length > 0) {
|
|
|
|
body.waiting_close_channels.map(channel => { return promises.push(getAliasForChannel(channel.channel))});
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
return Promise.all(promises).then(function(values) {
|
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Pending Channels', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Pending Channels Received'});
|
2020-05-03 19:52:38 +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['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.options.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.response.request.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Get Pending Channel Alias Error', error: err});
|
2020-05-03 19:52:38 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: 'Fetching Pending Channels Failed!',
|
|
|
|
error: err.error
|
|
|
|
});
|
|
|
|
});
|
2019-12-04 23:14:34 +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['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.options.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.response.request.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Get Pending Channel Error', error: err});
|
2019-12-04 23:14:34 +00:00
|
|
|
return res.status(500).json({
|
|
|
|
message: 'Fetching Pending Channels Failed!',
|
|
|
|
error: err.error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getClosedChannels = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Getting Closed Channels..'});
|
2019-12-04 23:14:34 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/channels/closed';
|
2019-12-04 23:14:34 +00:00
|
|
|
options.qs = req.query;
|
|
|
|
request(options).then(function (body) {
|
|
|
|
if (body.channels && body.channels.length > 0) {
|
2021-06-20 20:27:08 +00:00
|
|
|
return Promise.all(
|
2020-05-03 19:52:38 +00:00
|
|
|
body.channels.map(channel => {
|
|
|
|
channel.close_type = (!channel.close_type) ? 'COOPERATIVE_CLOSE' : channel.close_type;
|
|
|
|
return getAliasForChannel(channel);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.then(function(values) {
|
|
|
|
body.channels = common.sortDescByKey(body.channels, 'close_height');
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Closed Channels', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Closed Channels Received'});
|
2020-05-03 19:52:38 +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['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.options.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.response.request.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Get All Channel Alias Error', error: err});
|
2020-05-03 19:52:38 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: 'Fetching Channels Alias Failed!',
|
|
|
|
error: err.error
|
|
|
|
});
|
2019-12-04 23:14:34 +00:00
|
|
|
});
|
2020-05-03 19:52:38 +00:00
|
|
|
} else {
|
|
|
|
body.channels = [];
|
|
|
|
res.status(200).json(body);
|
|
|
|
}
|
2019-12-04 23:14:34 +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['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.options.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.response.request.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Get Closed Channel Error', error: err});
|
2019-01-01 16:26:51 +00:00
|
|
|
return res.status(500).json({
|
2019-12-04 23:14:34 +00:00
|
|
|
message: 'Fetching Closed Channels Failed!',
|
2019-01-01 16:26:51 +00:00
|
|
|
error: err.error
|
|
|
|
});
|
2018-09-15 01:31:01 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.postChannel = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Opening Channel..'});
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/channels';
|
2019-03-13 01:54:39 +00:00
|
|
|
options.form = {
|
2018-09-15 01:31:01 +00:00
|
|
|
node_pubkey_string: req.body.node_pubkey,
|
2019-03-13 01:54:39 +00:00
|
|
|
local_funding_amount: req.body.local_funding_amount,
|
2019-05-03 01:27:21 +00:00
|
|
|
private: req.body.private,
|
2019-03-13 01:54:39 +00:00
|
|
|
spend_unconfirmed: req.body.spend_unconfirmed
|
|
|
|
};
|
|
|
|
if (req.body.trans_type === '1') {
|
|
|
|
options.form.target_conf = req.body.trans_type_value;
|
|
|
|
} else if (req.body.trans_type === '2') {
|
|
|
|
options.form.sat_per_byte = req.body.trans_type_value;
|
|
|
|
}
|
|
|
|
options.form = JSON.stringify(options.form);
|
2019-01-01 16:26:51 +00:00
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Channel Open 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 New Channel Error', error: body.error});
|
2018-09-15 01:31:01 +00:00
|
|
|
res.status(500).json({
|
2019-01-09 03:04:34 +00:00
|
|
|
message: 'Open Channel Failed!',
|
2020-03-10 17:25:52 +00:00
|
|
|
error: (!body) ? 'Error From Server!' : body.error
|
2018-10-12 00:03:54 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channels Opened'});
|
2018-10-12 00:03:54 +00:00
|
|
|
res.status(201).json(body);
|
|
|
|
}
|
2019-01-01 16:26:51 +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['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.options.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.response.request.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Open New Channel Error', error: err});
|
2019-01-01 16:26:51 +00:00
|
|
|
return res.status(500).json({
|
2019-01-09 03:04:34 +00:00
|
|
|
message: 'Open Channel Failed!',
|
2019-01-01 16:26:51 +00:00
|
|
|
error: err.error
|
|
|
|
});
|
2018-10-12 00:03:54 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.postTransactions = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Sending Payment..'});
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/channels/transactions';
|
2021-04-24 19:08:44 +00:00
|
|
|
options.form = { payment_request: req.body.paymentReq };
|
|
|
|
if(req.body.paymentAmount) {
|
|
|
|
options.form.amt = req.body.paymentAmount;
|
2019-01-06 01:44:59 +00:00
|
|
|
}
|
2020-02-04 22:04:54 +00:00
|
|
|
if (req.body.feeLimit) { options.form.fee_limit = req.body.feeLimit; }
|
|
|
|
if (req.body.outgoingChannel) { options.form.outgoing_chan_id = req.body.outgoingChannel; }
|
|
|
|
if (req.body.allowSelfPayment) { options.form.allow_self_payment = req.body.allowSelfPayment; }
|
2020-02-05 00:31:52 +00:00
|
|
|
if (req.body.lastHopPubkey) { options.form.last_hop_pubkey = Buffer.from(req.body.lastHopPubkey, 'hex').toString('base64'); }
|
2020-01-02 23:23:47 +00:00
|
|
|
options.form = JSON.stringify(options.form);
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Send Payment Options', data: options.form});
|
2019-01-01 16:26:51 +00:00
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Send Payment Response', data: body});
|
2020-02-04 22:04:54 +00:00
|
|
|
if(!body || body.error) {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Send Payment Error', error: body.error});
|
2018-10-12 00:03:54 +00:00
|
|
|
res.status(500).json({
|
2019-01-09 03:04:34 +00:00
|
|
|
message: 'Send Payment Failed!',
|
2020-02-04 22:04:54 +00:00
|
|
|
error: (!body) ? 'Error From Server!' : body.error
|
2018-09-15 01:31:01 +00:00
|
|
|
});
|
2019-01-06 01:44:59 +00:00
|
|
|
} else if (body.payment_error) {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Send Payment Error: ' + JSON.stringify(body.payment_error)});
|
2019-01-06 01:44:59 +00:00
|
|
|
res.status(500).json({
|
2019-01-09 03:04:34 +00:00
|
|
|
message: 'Send Payment Failed!',
|
2020-02-04 22:04:54 +00:00
|
|
|
error: (!body) ? 'Error From Server!' : body.payment_error
|
2019-01-06 01:44:59 +00:00
|
|
|
});
|
2018-09-15 01:31:01 +00:00
|
|
|
} else {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Payment Sent'});
|
2018-09-15 01:31:01 +00:00
|
|
|
res.status(201).json(body);
|
|
|
|
}
|
2019-01-01 16:26:51 +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['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.options.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.response.request.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Send Payment Error', error: err});
|
2019-01-01 16:26:51 +00:00
|
|
|
return res.status(500).json({
|
2019-01-09 03:04:34 +00:00
|
|
|
message: 'Send Payment Failed!',
|
2019-01-01 16:26:51 +00:00
|
|
|
error: err.error
|
|
|
|
});
|
2018-09-15 01:31:01 +00:00
|
|
|
});
|
|
|
|
};
|
2018-11-04 23:47:00 +00:00
|
|
|
|
|
|
|
exports.closeChannel = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Closing Channel..'});
|
2019-02-22 00:32:54 +00:00
|
|
|
req.setTimeout(60000 * 10); // timeout 10 mins
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2019-01-09 03:04:34 +00:00
|
|
|
let channelpoint = req.params.channelPoint.replace(':', '/');
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/channels/' + channelpoint + '?force=' + req.query.force;
|
2020-02-07 16:36:46 +00:00
|
|
|
if(req.query.target_conf) { options.url = options.url + '&target_conf=' + req.query.target_conf; }
|
|
|
|
if(req.query.sat_per_byte) { options.url = options.url + '&sat_per_byte=' + req.query.sat_per_byte; }
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Closing Channel Options URL', data: options.url});
|
2019-01-01 16:26:51 +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});
|
2018-11-04 23:47:00 +00:00
|
|
|
res.status(500).json({
|
2019-01-09 03:04:34 +00:00
|
|
|
message: 'Close Channel Failed!',
|
2020-03-10 17:25:52 +00:00
|
|
|
error: (!body) ? 'Error From Server!' : body.error
|
2018-11-04 23:47:00 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Closed'});
|
2018-11-04 23:47:00 +00:00
|
|
|
res.status(204).json({message: 'Channel Closed!'});
|
|
|
|
}
|
2019-01-01 16:26:51 +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['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.options.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.response.request.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Close Channel Error', error: err});
|
2019-01-01 16:26:51 +00:00
|
|
|
return res.status(500).json({
|
2019-01-09 03:04:34 +00:00
|
|
|
message: 'Close Channel Failed!',
|
2019-01-01 16:26:51 +00:00
|
|
|
error: err.error
|
|
|
|
});
|
2018-11-04 23:47:00 +00:00
|
|
|
});
|
2019-01-19 23:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.postChanPolicy = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Updating Channel Policy..'});
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/chanpolicy';
|
2019-01-19 23:47:56 +00:00
|
|
|
if(req.body.chanPoint === 'all') {
|
|
|
|
options.form = JSON.stringify({
|
|
|
|
global: true,
|
|
|
|
base_fee_msat: req.body.baseFeeMsat,
|
|
|
|
fee_rate: parseFloat(req.body.feeRate/1000000),
|
|
|
|
time_lock_delta: parseInt(req.body.timeLockDelta)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
let breakPoint = req.body.chanPoint.indexOf(':');
|
|
|
|
let txid_str = req.body.chanPoint.substring(0, breakPoint);
|
|
|
|
let output_idx = req.body.chanPoint.substring(breakPoint+1, req.body.chanPoint.length);
|
|
|
|
options.form = JSON.stringify({
|
|
|
|
base_fee_msat: req.body.baseFeeMsat,
|
|
|
|
fee_rate: parseFloat(req.body.feeRate/1000000),
|
|
|
|
time_lock_delta: parseInt(req.body.timeLockDelta),
|
|
|
|
chan_point: {funding_txid_str: txid_str, output_index: parseInt(output_idx)}
|
|
|
|
});
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Channels', msg: 'Update Channel Policy Options', data: options.form});
|
2019-01-19 23:47:56 +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-01-19 23:47:56 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: 'Update Channel Failed!',
|
2020-03-10 17:25:52 +00:00
|
|
|
error: (!body) ? 'Error From Server!' : body.error
|
2019-01-19 23:47:56 +00:00
|
|
|
});
|
|
|
|
} else {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Channels', msg: 'Channel Policy Updated'});
|
2019-01-19 23:47:56 +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['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.options.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
|
|
|
if (err.response && err.response.request && err.response.request.headers && err.response.request.headers['Grpc-Metadata-macaroon']) {
|
|
|
|
delete err.response.request.headers['Grpc-Metadata-macaroon'];
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'ERROR', fileName: 'Channels', msg: 'Update Channel Policy Error', error: err});
|
2019-01-19 23:47:56 +00:00
|
|
|
return res.status(500).json({
|
|
|
|
message: 'Update Channel Failed!',
|
|
|
|
error: err.error
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|