forked from MattyIce/postpromoter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegators.js
65 lines (48 loc) · 1.83 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
const steem = require('steem');
var delegation_transactions = [];
function loadDelegations(account, callback) {
getTransactions(account, -1, callback);
}
function getTransactions(account, start, callback) {
var last_trans = start;
steem.api.getAccountHistory(account, start, (start < 0) ? 10000 : Math.min(start, 10000), function (err, result) {
if(err) {
console.log(err);
return;
}
result.reverse();
result.forEach(function(trans) {
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)
getTransactions(account, last_trans, callback);
else
processDelegations(callback);
});
}
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
}