2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-11-19 15:25:29 +00:00
RTL/server/controllers/cln/channels.ts

133 lines
8.7 KiB
TypeScript
Raw Normal View History

2021-12-29 23:08:41 +00:00
import request from 'request-promise';
import { Logger, LoggerService } from '../../utils/logger.js';
import { Common, CommonService } from '../../utils/common.js';
2023-10-31 22:06:39 +00:00
import { getAlias } from './network.js';
2021-12-29 23:08:41 +00:00
let options = null;
const logger: LoggerService = Logger;
const common: CommonService = Common;
export const listPeerChannels = (req, res, next) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Channels', msg: 'Getting Peer Channels..' });
options = common.getOptions(req);
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
2023-10-20 00:38:08 +00:00
options.url = req.session.selectedNode.ln_server_url + '/v1/listpeerchannels';
2023-11-07 06:31:27 +00:00
request.post(options).then((body) => {
2023-10-24 22:29:20 +00:00
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Channels', msg: 'Peer Channels List Received', data: body.channels });
2023-11-07 06:31:27 +00:00
return Promise.all(body.channels?.map((channel) => {
2023-11-08 05:24:26 +00:00
channel.to_us_msat = common.removeMSat(channel.to_us_msat);
channel.total_msat = common.removeMSat(channel.total_msat);
channel.last_tx_fee_msat = common.removeMSat(channel.last_tx_fee_msat);
channel.funding.local_funds_msat = common.removeMSat(channel.funding.local_funds_msat);
channel.funding.remote_funds_msat = common.removeMSat(channel.funding.remote_funds_msat);
channel.funding.pushed_msat = common.removeMSat(channel.funding.pushed_msat);
channel.min_to_us_msat = common.removeMSat(channel.min_to_us_msat);
channel.max_to_us_msat = common.removeMSat(channel.max);
channel.fee_base_msat = common.removeMSat(channel.fee_base_msat);
channel.dust_limit_msat = common.removeMSat(channel.dust_limit_msat);
channel.max_total_htlc_in_msat = common.removeMSat(channel.max_total_htlc_in_msat);
channel.their_reserve_msat = common.removeMSat(channel.their_reserve_msat);
channel.our_reserve_msat = common.removeMSat(channel.our_reserve_msat);
channel.spendable_msat = common.removeMSat(channel.spendable_msat);
channel.receivable_msat = common.removeMSat(channel.receivable_msat);
channel.minimum_htlc_in_msat = common.removeMSat(channel.minimum_htlc_in_msat);
channel.minimum_htlc_out_msat = common.removeMSat(channel.minimum_htlc_out_msat);
channel.maximum_htlc_out_msat = common.removeMSat(channel.maximum_htlc_out_msat);
channel.in_offered_msat = common.removeMSat(channel.in_offered_msat);
channel.in_fulfilled_msat = common.removeMSat(channel.in_fulfilled_msat);
channel.out_offered_msat = common.removeMSat(channel.out_offered_msat);
channel.out_fulfilled_msat = common.removeMSat(channel.out_fulfilled_msat);
channel.balancedness = (channel.total_msat === 0) ? 1 : (1 - Math.abs((channel.to_us_msat - (channel.total_msat - channel.to_us_msat)) / channel.total_msat)).toFixed(3);
2023-11-07 06:31:27 +00:00
return getAlias(req.session.selectedNode, channel, 'peer_id');
})).then((values) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Channels', msg: 'Peer Channels List With Aliases Received', data: body.channels });
return res.status(200).json(body.channels || []);
});
}).catch((errRes) => {
const err = common.handleError(errRes, 'Channels', 'List Peer Channels Error', req.session.selectedNode);
return res.status(err.statusCode).json({ message: err.message, error: err.error });
});
};
2021-12-29 23:08:41 +00:00
export const openChannel = (req, res, next) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Channels', msg: 'Opening Channel..' });
options = common.getOptions(req);
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
2023-10-20 00:38:08 +00:00
options.url = req.session.selectedNode.ln_server_url + '/v1/fundchannel';
2023-10-26 03:47:08 +00:00
options.body = req.body;
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Channels', msg: 'Open Channel Options', data: options.body });
2021-12-29 23:08:41 +00:00
request.post(options).then((body) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Channels', msg: 'Channel Opened', data: body });
2021-12-29 23:08:41 +00:00
res.status(201).json(body);
}).catch((errRes) => {
const err = common.handleError(errRes, 'Channels', 'Open Channel Error', req.session.selectedNode);
return res.status(err.statusCode).json({ message: err.message, error: err.error });
});
};
export const setChannelFee = (req, res, next) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Channels', msg: 'Setting Channel Fee..' });
options = common.getOptions(req);
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
2023-10-20 00:38:08 +00:00
options.url = req.session.selectedNode.ln_server_url + '/v1/setchannel';
2023-10-26 03:47:08 +00:00
options.body = req.body;
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Channels', msg: 'Update Channel Policy Options', data: options.body });
2021-12-29 23:08:41 +00:00
request.post(options).then((body) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Channels', msg: 'Updated Channel Policy', data: body });
2021-12-29 23:08:41 +00:00
res.status(201).json(body);
}).catch((errRes) => {
const err = common.handleError(errRes, 'Channels', 'Update Channel Policy Error', req.session.selectedNode);
return res.status(err.statusCode).json({ message: err.message, error: err.error });
});
};
export const closeChannel = (req, res, next) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Channels', msg: 'Closing Channel..' });
req.setTimeout(60000 * 10); // timeout 10 mins
options = common.getOptions(req);
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
2023-10-20 00:38:08 +00:00
options.url = req.session.selectedNode.ln_server_url + '/v1/close';
2023-11-02 03:39:50 +00:00
options.body = req.body;
2021-12-29 23:08:41 +00:00
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Channels', msg: 'Closing Channel', data: options.url });
2023-10-20 00:38:08 +00:00
request.post(options).then((body) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Channels', msg: 'Channel Closed', data: body });
2021-12-29 23:08:41 +00:00
res.status(204).json(body);
}).catch((errRes) => {
const err = common.handleError(errRes, 'Channels', 'Close Channel Error', req.session.selectedNode);
return res.status(err.statusCode).json({ message: err.message, error: err.error });
});
};
export const listForwards = (req, res, next) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Channels', msg: 'Getting Channel List Forwards..' });
options = common.getOptions(req);
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
2023-10-20 00:38:08 +00:00
options.url = req.session.selectedNode.ln_server_url + '/v1/listforwards';
2023-11-02 03:39:50 +00:00
options.body = req.body;
2021-12-29 23:08:41 +00:00
request.get(options).then((body) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Channels', msg: 'Forwarding History Received For Status ' + req.query.status, data: body });
2023-10-20 00:38:08 +00:00
res.status(200).json(!body.forwards ? [] : (req.query.status === 'failed' || req.query.status === 'local_failed') ? body.forwards.slice(Math.max(0, body.forwards.length - 1000), Math.max(1000, body.forwards.length)).reverse() : body.forwards.reverse());
2021-12-29 23:08:41 +00:00
}).catch((errRes) => {
const err = common.handleError(errRes, 'Channels', 'Forwarding History Error', req.session.selectedNode);
return res.status(err.statusCode).json({ message: err.message, error: err.error });
});
};
export const funderUpdatePolicy = (req, res, next) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Channels', msg: 'Getting or Updating Funder Policy..' });
options = common.getOptions(req);
if (options.error) { return res.status(options.statusCode).json({ message: options.message, error: options.error }); }
2023-10-20 00:38:08 +00:00
options.url = req.session.selectedNode.ln_server_url + '/v1/funderupdate';
2023-10-26 03:47:08 +00:00
options.body = req.body;
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Channels', msg: 'Funder Update Body', data: options.body });
request.post(options).then((body) => {
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Channels', msg: 'Funder Policy Received', data: body });
2023-11-08 05:24:26 +00:00
body.channel_fee_max_base_msat = common.removeMSat(body.channel_fee_max_base_msat);
body.lease_fee_base_msat = common.removeMSat(body.lease_fee_base_msat);
res.status(200).json(body);
}).catch((errRes) => {
const err = common.handleError(errRes, 'Channels', 'Funder Policy Error', req.session.selectedNode);
return res.status(err.statusCode).json({ message: err.message, error: err.error });
});
};