Full address history with DataTables ajax loading

This commit is contained in:
joeuhren
2020-11-19 21:37:42 -07:00
parent da7330ded6
commit fd277bc674
7 changed files with 276 additions and 176 deletions
+93 -37
View File
@@ -2,6 +2,7 @@ var mongoose = require('mongoose')
, Stats = require('../models/stats')
, Markets = require('../models/markets')
, Address = require('../models/address')
, AddressTx = require('../models/addresstx')
, Tx = require('../models/tx')
, Richlist = require('../models/richlist')
, Peers = require('../models/peers')
@@ -59,43 +60,39 @@ function update_address(hash, txid, amount, type, cb) {
return cb();
});
} else {
// ensure tx doesnt already exist in address.txs
lib.is_unique(address.txs, txid, function(unique, index) {
var tx_array = address.txs;
var received = address.received;
var sent = address.sent;
if (type == 'vin') {
sent = sent + amount;
} else {
received = received + amount;
}
if (unique == true) {
tx_array.push({addresses: txid, type: type});
if ( tx_array.length > settings.txcount ) {
tx_array.shift();
}
Address.updateOne({a_id:hash}, {
txs: tx_array,
received: received,
sent: sent,
balance: received - sent
}, function() {
return cb();
});
} else {
if (type == tx_array[index].type) {
return cb(); //duplicate
//Considering no duplicate
var tx_array = [];
var received = address.received;
var sent = address.sent;
if (type == 'vin') {
sent = sent + amount;
} else {
received = received + amount;
}
tx_array.push({addresses: txid, type: type});
Address.updateOne({a_id:hash}, {
received: received,
sent: sent,
balance: received - sent
}, function() {
var newAddressTx = new AddressTx({
a_id: hash,
addresses: txid,
type: type
});
newAddressTx.save(function(err) {
if (err) {
return cb(err);
} else {
Address.updateOne({a_id:hash}, {
txs: tx_array,
received: received,
sent: sent,
balance: received - sent
}, function() {
return cb();
});
//console.log('address saved: %s', hash);
//console.log(newAddress);
return cb();
}
}
});
return cb();
});
}
} else {
@@ -103,14 +100,12 @@ function update_address(hash, txid, amount, type, cb) {
if (type == 'vin') {
var newAddress = new Address({
a_id: hash,
txs: [ {addresses: txid, type: 'vin'} ],
sent: amount,
balance: amount,
});
} else {
var newAddress = new Address({
a_id: hash,
txs: [ {addresses: txid, type: 'vout'} ],
received: amount,
balance: amount,
});
@@ -122,6 +117,21 @@ function update_address(hash, txid, amount, type, cb) {
} else {
//console.log('address saved: %s', hash);
//console.log(newAddress);
var newAddressTx = new AddressTx({
a_id: hash,
addresses: txid,
type: type
});
newAddressTx.save(function(err) {
if (err) {
return cb(err);
} else {
//console.log('address saved: %s', hash);
//console.log(newAddress);
return cb();
}
});
return cb();
}
});
@@ -394,6 +404,52 @@ module.exports = {
});
},
get_address_txs_ajax: function(hash, start, length, cb) {
var totalCount = 0;
Address.findOne({a_id: hash}, function(err, addressTotalTxs) {
if(err) {
return cb(err);
} else {
AddressTx.find({a_id: hash}).count({}, function(err, count){
if(err) {
return cb(err);
} else {
totalCount = count;
AddressTx.find({a_id: hash}).sort({_id: 'desc'}).skip(Number(start)).limit(Number(length)).exec(function (err, address) {
if (err) {
return cb(err);
} else {
//console.log(address);
var txs = [];
var count = address.length;
var hashes = address;
var txs = [];
lib.syncLoop(count, function (loop) {
var i = loop.iteration();
find_tx(hashes[i].addresses, function (tx) {
if (tx) {
txs.push(tx);
loop.next();
} else {
txs.push("1. Not found");
loop.next();
}
})
}, function () {
return cb(txs, totalCount);
});
}
});
}
});
}
});
},
create_market: function(coin, exchange, market, cb) {
var newMarkets = new Markets({
market: market,