-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathholderTrackBX.js
145 lines (132 loc) · 3.43 KB
/
holderTrackBX.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const config = require("./config");
const axios = require('axios');
const burnPrefix = [
'0x00000000000000000000',
'0xdead'
];
/////////////////////////////////////
function HolderTrack(name, contract, createdTS, decimals, counter){
/////////////////////////////////////
let holders = null;
let oldHolders = null;
const apiUrl = `https://api.bloxy.info/token/token_holders_list?token=TOKEN&limit=5000&key=KEY&format=structure`;
function isBurnPrefix(address){
for (let p of burnPrefix){
if (address.substring(0,p.length).toLowerCase() === p) {
return true;
}
}
}
/////////////////////////////////////
async function update(latestBlock, dgDecimals){
// patchy update
if(!decimals)
decimals = dgDecimals;
// backup
oldHolders = holders;
var mapObj = {
TOKEN: contract,
KEY: config.keys.bloxy
};
const url = apiUrl.replace(/TOKEN|KEY/g, function(matched){
return mapObj[matched];
});
let newHolders = [];
//await callApi(url, newHolders);
try{
const res = await axios({
method: 'get',
url: url,
timeout: 3000
});
if(res.status === 200){
// reset holders
holders = {};
const oh = oldHolders? Object.keys(oldHolders) : null;
try{
for (const h of res.data){
holders[h.address] = h;
if((!oh || !oh.includes(h.address)) && !isBurnPrefix(h.address)){
newHolders.push(h.address);
}
}
}catch(e){
counter.addError('holdertrackBX.resData');
console.log('holdertrackBX', res);
console.error('holdertrackBX',e);
}
}else{
console.error('bloxy holder list', res);
counter.addError("holderTrackBX.apiStatus_"+res.status);
}
}
catch(e){
if(e.code === 'ECONNABORTED'){
counter.addError("holderTrackBX.apiTimeout");
}
else{
counter.addError("holderTrackBX."+e.code);
}
console.error('axios', e);
}
// extract new holders
console.log(`holderTrack ${name}\t${newHolders.length} new holders`);
return newHolders;
}
/////////////////////////////////////
function count(positive){
return get().length;
}
/////////////////////////////////////
function get(){
return Object.keys(holders || {});
}
/////////////////////////////////////
function distribution(){
if(!count()){
console.log(`${name} holders is empty`);
}
let dis = {}
// agg per type
for( let address in holders ){
const h = holders[address];
if( h.address_type){
if(!dis[h.address_type]){
dis[h.address_type] = 0;
}
dis[h.address_type] += 1;
}
}
return dis;
}
/////////////////////////////////////
function posBalance(){
let res = 0;
for (let id in holders){
if(holders[id])
res += holders[id].balance;
}
return res;
}
/////////////////////////////////////
function balanceArr(){
let res = [];
for (let id in holders){
if(holders[id])
res.push(holders[id].balance);
}
return res;
}
/////////////////////////////////////
return {
update:update,
count:count,
get:get,
posBalance:posBalance,
distribution:distribution,
balanceArr:balanceArr,
}
}
module.exports = HolderTrack;
function test() {
}