2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-11-07 15:20:31 +00:00
RTL/controllers/lnd/balance.js

42 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-01-01 16:26:51 +00:00
var request = require('request-promise');
var common = require('../../routes/common');
var logger = require('../shared/logger');
var options = {};
2018-09-15 01:31:01 +00:00
exports.getBalance = (req, res, next) => {
logger.log({level: 'INFO', fileName: 'Balance', msg: 'Getting Balance..'});
options = common.getOptions();
options.url = common.getSelLNServerUrl() + '/v1/balance/' + (req.params.source).toLowerCase();
2018-09-15 01:31:01 +00:00
options.qs = req.query;
2019-01-01 16:26:51 +00:00
request(options).then((body) => {
logger.log({level: 'DEBUG', fileName: 'Balance', msg: '[Request params, Request Query, Balance Received]', data: [req.params, req.query, body]});
if (body) {
if (req.params.source === 'blockchain') {
if (!body.total_balance) { body.total_balance = 0; }
if (!body.confirmed_balance) { body.confirmed_balance = 0; }
if (!body.unconfirmed_balance) { body.unconfirmed_balance = 0; }
}
if (req.params.source === 'channels') {
if (!body.balance) { body.balance = 0; }
if (!body.pending_open_balance) { body.pending_open_balance = 0; }
}
logger.log({level: 'INFO', fileName: 'Balance', msg: 'Balance Received'});
res.status(200).json(body);
2018-09-15 01:31:01 +00:00
}
2019-01-01 16:26:51 +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'];
}
logger.log({level: 'ERROR', fileName: 'Balance', msg: 'Fetch Balance Error', error: err});
2019-01-01 16:26:51 +00:00
return res.status(500).json({
message: "Fetching balance failed!",
error: err.error
});
2018-09-15 01:31:01 +00:00
});
};