2020-07-07 17:57:15 +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');
|
2020-07-07 17:57:15 +00:00
|
|
|
var options = {};
|
|
|
|
|
2021-02-21 19:02:31 +00:00
|
|
|
arrangeBalances = (body) => {
|
2021-08-28 21:03:18 +00:00
|
|
|
if (!body.confirmed) { body.confirmed = 0; }
|
|
|
|
if (!body.unconfirmed) { body.unconfirmed = 0; }
|
2021-02-21 19:02:31 +00:00
|
|
|
body.total = +body.confirmed + +body.unconfirmed;
|
|
|
|
body.btc_total = +body.btc_confirmed + +body.btc_unconfirmed;
|
|
|
|
return body;
|
|
|
|
};
|
|
|
|
|
2020-07-07 17:57:15 +00:00
|
|
|
exports.getNewAddress = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'Generating New Address..'});
|
2020-07-07 17:57:15 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/getnewaddress';
|
|
|
|
options.form = {};
|
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Onchain', msg: 'New Address Generated', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'New Address Generated'});
|
2020-07-07 17:57:15 +00:00
|
|
|
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});
|
2020-07-07 17:57:15 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getBalance = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'Getting On Chain Balance..'});
|
2020-07-07 17:57:15 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/onchainbalance';
|
|
|
|
options.form = {};
|
2021-02-21 19:02:31 +00:00
|
|
|
if (common.read_dummy_data) {
|
|
|
|
common.getDummyData('OnChainBalance').then(function(data) { res.status(200).json(arrangeBalances(data)); });
|
|
|
|
} else {
|
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Onchain', msg: 'Balance Received', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'On Chain Balance Received'});
|
2021-02-21 19:02:31 +00:00
|
|
|
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});
|
2020-07-07 17:57:15 +00:00
|
|
|
});
|
2021-02-21 19:02:31 +00:00
|
|
|
}
|
2020-07-07 17:57:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getTransactions = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'Getting On Chain Transactions..'});
|
2020-07-07 17:57:15 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/onchaintransactions';
|
|
|
|
options.form = {
|
|
|
|
count: req.query.count,
|
|
|
|
skip: req.query.skip
|
|
|
|
};
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'OnChain', msg: 'Getting On Chain Transactions Options', data: options.form});
|
2020-07-07 17:57:15 +00:00
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'OnChain', msg: 'Transaction Received', data: body});
|
2020-07-07 17:57:15 +00:00
|
|
|
if (body && body.length > 0) {
|
|
|
|
body = common.sortDescByKey(body, 'timestamp');
|
|
|
|
}
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'On Chain Transaction Received'});
|
2020-07-07 17:57:15 +00:00
|
|
|
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});
|
2020-07-07 17:57:15 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.sendFunds = (req, res, next) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'Sending On Chain Funds..'});
|
2020-07-07 17:57:15 +00:00
|
|
|
options = common.getOptions();
|
|
|
|
options.url = common.getSelLNServerUrl() + '/sendonchain';
|
|
|
|
options.form = {
|
|
|
|
address: req.body.address,
|
|
|
|
amountSatoshis: req.body.amount,
|
|
|
|
confirmationTarget: req.body.blocks
|
|
|
|
};
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Onchain', msg: 'Send Funds Options', data: options.form});
|
2020-07-07 17:57:15 +00:00
|
|
|
request.post(options).then((body) => {
|
2021-06-20 20:27:08 +00:00
|
|
|
logger.log({level: 'DEBUG', fileName: 'Onchain', msg: 'Send Funds Response', data: body});
|
|
|
|
logger.log({level: 'INFO', fileName: 'OnChain', msg: 'On Chain Fund Sent'});
|
2020-07-07 17:57:15 +00:00
|
|
|
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});
|
2020-07-07 17:57:15 +00:00
|
|
|
});
|
|
|
|
};
|