-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathdelegators.js
70 lines (53 loc) · 2.37 KB
/
delegators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const steem = require('steem');
var utils = require('./utils');
var dsteem = require('dsteem');
var delegation_transactions = [];
function loadDelegations(client, account, callback) {
getTransactions(client, account, -1, callback);
}
function getTransactions(client, account, start, callback) {
var last_trans = start;
utils.log('Loading history for delegators at transaction: ' + (start < 0 ? 'latest' : start));
client.database.call('get_account_history', [account, start, (start < 0) ? 10000 : Math.min(start, 10000)]).then(function (result) {
result.reverse();
for(var i = 0; i < result.length; i++) {
var trans = result[i];
var op = trans[1].op;
if(op[0] == 'delegate_vesting_shares' && op[1].delegatee == account)
delegation_transactions.push({ id: trans[0], data: op[1] });
// Save the ID of the last transaction that was processed.
last_trans = trans[0];
}
if(last_trans > 0 && last_trans != start)
getTransactions(client, account, last_trans, callback);
else {
if(last_trans > 0) {
utils.log('********* ALERT - Full account history not available from this node, not all delegators may have been loaded!! ********');
utils.log('********* Last available transaction was: ' + last_trans + ' ********');
}
processDelegations(callback);
}
}, function(err) { console.log('Error loading account history for delegations: ' + err); });
}
function processDelegations(callback) {
var delegations = [];
// Go through the delegation transactions from oldest to newest to find the final delegated amount from each account
delegation_transactions.reverse();
for(var i = 0; i < delegation_transactions.length; i++) {
var trans = delegation_transactions[i];
// Check if this is a new delegation or an update to an existing delegation from this account
var delegation = delegations.find(d => d.delegator == trans.data.delegator);
if(delegation) {
delegation.vesting_shares = trans.data.vesting_shares;
} else {
delegations.push({ delegator: trans.data.delegator, vesting_shares: trans.data.vesting_shares });
}
}
delegation_transactions = [];
// Return a list of all delegations (and filter out any that are 0)
if(callback)
callback(delegations.filter(function(d) { return parseFloat(d.vesting_shares) > 0; }));
}
module.exports = {
loadDelegations: loadDelegations
}