2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-11-15 18:13:00 +00:00
RTL/controllers/eclair/onchain.js

93 lines
3.8 KiB
JavaScript
Raw Normal View History

var request = require('request-promise');
var common = require('../../routes/common');
var logger = require('../shared/logger');
var options = {};
arrangeBalances = (body) => {
2021-08-28 21:03:18 +00:00
if (!body.confirmed) { body.confirmed = 0; }
if (!body.unconfirmed) { body.unconfirmed = 0; }
body.total = +body.confirmed + +body.unconfirmed;
body.btc_total = +body.btc_confirmed + +body.btc_unconfirmed;
return body;
};
exports.getNewAddress = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'Generating New Address..'});
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/getnewaddress';
options.form = {};
request.post(options).then((body) => {
logger.log({level: 'DEBUG', fileName: 'Onchain', msg: 'New Address Generated', data: body});
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'New Address Generated'});
res.status(200).json(body);
})
.catch(errRes => {
2021-08-28 21:03:18 +00:00
const err = common.handleError(errRes, 'OnChain', 'Get New Address Error');
return res.status(err.statusCode).json({message: err.message, error: err.error});
});
};
exports.getBalance = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'Getting On Chain Balance..'});
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/onchainbalance';
options.form = {};
if (common.read_dummy_data) {
common.getDummyData('OnChainBalance').then(function(data) { res.status(200).json(arrangeBalances(data)); });
} else {
request.post(options).then((body) => {
logger.log({level: 'DEBUG', fileName: 'Onchain', msg: 'Balance Received', data: body});
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'On Chain Balance Received'});
res.status(200).json(arrangeBalances(body));
})
.catch(errRes => {
2021-08-28 21:03:18 +00:00
const err = common.handleError(errRes, 'OnChain', 'Get Balance Error');
return res.status(err.statusCode).json({message: err.message, error: err.error});
});
}
};
exports.getTransactions = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'Getting On Chain Transactions..'});
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/onchaintransactions';
options.form = {
count: req.query.count,
skip: req.query.skip
};
logger.log({level: 'DEBUG', fileName: 'OnChain', msg: 'Getting On Chain Transactions Options', data: options.form});
request.post(options).then((body) => {
logger.log({level: 'DEBUG', fileName: 'OnChain', msg: 'Transaction Received', data: body});
if (body && body.length > 0) {
body = common.sortDescByKey(body, 'timestamp');
}
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'On Chain Transaction Received'});
res.status(200).json(body);
})
.catch(errRes => {
2021-08-28 21:03:18 +00:00
const err = common.handleError(errRes, 'OnChain', 'Get Transactions Error');
return res.status(err.statusCode).json({message: err.message, error: err.error});
});
};
exports.sendFunds = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'Sending On Chain Funds..'});
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/sendonchain';
options.form = {
address: req.body.address,
amountSatoshis: req.body.amount,
confirmationTarget: req.body.blocks
};
logger.log({level: 'DEBUG', fileName: 'Onchain', msg: 'Send Funds Options', data: options.form});
request.post(options).then((body) => {
logger.log({level: 'DEBUG', fileName: 'Onchain', msg: 'Send Funds Response', data: body});
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'On Chain Fund Sent'});
res.status(201).json(body);
})
.catch(errRes => {
2021-08-28 21:03:18 +00:00
const err = common.handleError(errRes, 'OnChain', 'Send Funds Error');
return res.status(err.statusCode).json({message: err.message, error: err.error});
});
};