2019-01-01 16:26:51 +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-09-15 01:31:01 +00:00
|
|
|
|
|
|
|
exports.getBalance = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Balance', msg: 'Getting Balance..'});
|
2019-04-07 00:20:40 +00:00
|
|
|
options = common.getOptions();
|
2020-08-18 14:34:40 +00:00
|
|
|
options.url = common.getSelLNServerUrl() + '/v1/balance/' + req.params.source;
|
2018-09-15 01:31:01 +00:00
|
|
|
options.qs = req.query;
|
2019-01-01 16:26:51 +00:00
|
|
|
request(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Balance', msg: '[Request params, Request Query, Balance Received]', data: [req.params, req.query, body]});
|
2020-05-03 19:52:38 +00:00
|
|
|
if (body) {
|
2020-12-20 23:36:04 +00:00
|
|
|
if (req.params.source === 'blockchain') {
|
2019-11-06 00:26:40 +00:00
|
|
|
if (!body.total_balance) { body.total_balance = 0; }
|
|
|
|
if (!body.confirmed_balance) { body.confirmed_balance = 0; }
|
|
|
|
if (!body.unconfirmed_balance) { body.unconfirmed_balance = 0; }
|
|
|
|
body.btc_total_balance = common.convertToBTC(body.total_balance);
|
|
|
|
body.btc_confirmed_balance = common.convertToBTC(body.confirmed_balance);
|
|
|
|
body.btc_unconfirmed_balance = common.convertToBTC(body.unconfirmed_balance);
|
|
|
|
}
|
2020-12-20 23:36:04 +00:00
|
|
|
if (req.params.source === 'channels') {
|
2019-11-06 00:26:40 +00:00
|
|
|
if (!body.balance) { body.balance = 0; }
|
|
|
|
if (!body.pending_open_balance) { body.pending_open_balance = 0; }
|
|
|
|
body.btc_balance = common.convertToBTC(body.balance);
|
|
|
|
body.btc_pending_open_balance = common.convertToBTC(body.pending_open_balance);
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'Balance', msg: 'Balance Received'});
|
2018-10-03 22:47:43 +00:00
|
|
|
res.status(200).json(body);
|
2018-09-15 01:31:01 +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: '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
|
|
|
});
|
|
|
|
};
|