Skip to content

Commit

Permalink
Merge pull request #410 from TheHolyRogerCoin/api-fix/add-address-las…
Browse files Browse the repository at this point in the history
…t-txs

Add `last_txs` to `getaddress` API
  • Loading branch information
TheHolyRoger authored Oct 18, 2020
2 parents a2bfcd0 + 3f1abae commit c681306
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
50 changes: 39 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,45 @@ app.use('/ext/getmoneysupply', function(req,res){

app.use('/ext/getaddress/:hash', function(req,res){
db.get_address(req.params.hash, function(address){
if (address) {
var a_ext = {
address: address.a_id,
sent: (address.sent / 100000000),
received: (address.received / 100000000),
balance: (address.balance / 100000000).toString().replace(/(^-+)/mg, ''),
};
res.send(a_ext);
} else {
res.send({ error: 'address not found.', hash: req.params.hash})
}
db.get_address_txs_ajax(req.params.hash, 0, settings.txcount, function(txs, count){
if (address) {
var last_txs = [];
for(i=0; i<txs.length; i++){
if(typeof txs[i].txid !== "undefined") {
var out = 0,
vin = 0,
tx_type = 'vout',
row = {};
txs[i].vout.forEach(function (r) {
if (r.addresses == req.params.hash) {
out += r.amount;
}
});
txs[i].vin.forEach(function (s) {
if (s.addresses == req.params.hash) {
vin += s.amount;
}
});
if (vin > out) {
tx_type = 'vin';
}
row['addresses'] = txs[i].txid;
row['type'] = tx_type;
last_txs.push(row);
}
}
var a_ext = {
address: address.a_id,
sent: (address.sent / 100000000),
received: (address.received / 100000000),
balance: (address.balance / 100000000).toString().replace(/(^-+)/mg, ''),
last_txs: last_txs,
};
res.send(a_ext);
} else {
res.send({ error: 'address not found.', hash: req.params.hash})
}
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ module.exports = {
} else {
var txs = [];
var count = address_tx.length;
var running_balance = balance_sum[0].balance;
var running_balance = balance_sum.length > 0 ? balance_sum[0].balance : 0;

var txs = [];

Expand Down

0 comments on commit c681306

Please sign in to comment.