-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstat.service.js
254 lines (217 loc) · 7.5 KB
/
stat.service.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* Node Trinity source code
* See LICENCE file at the top of the source tree
*
* ******************************************
*
* stat.service.js
* Stat module business logic
*
* ******************************************
*
* Authors: K. Zhidanov, A. Prudanov, M. Vasil'ev
*/
const Utils = require('./Utils');
let prev_max_tps = 0;
class StatService {
constructor(db) {
this.db = db;
this.init_max_tps();
}
async init_max_tps(){
prev_max_tps = (await this.db.get_stats('max_tps'))[0].value;
}
async get_tps(){
let tmp = await this.db.get_tps(300);
return tmp.tps;
};
async get_max_tps(){
let tmp = await this.db.get_tps(300);
if(prev_max_tps == null || prev_max_tps < tmp.tps)
prev_max_tps = tmp.tps;
return prev_max_tps;
};
async get_total_daily_stake(){
let tmp = await this.db.count_total_daily_stake();
return tmp.stake !== null ? tmp.stake : 0;
}
async get_total_daily_pos_stake(){
let tmp = await this.db.count_total_daily_pos_stake();
return tmp.stake !== null ? tmp.stake : 0;
}
async get_csup() {
let exclude = [
'0270a88ea6f7c5ea2a2ec3878008d878a70fd5d4ca27d5866d0eec3594cab0b912',
'026df0aa41967d8d47082c36b29a164aa1c90cdd07cb02d373daaba90b8eca5301'
];
let circ = BigInt((await this.db.get_tokens([Utils.ENQ_TOKEN_NAME]))[0].total_supply);
for(let wallet of exclude){
let balance = await this.db.get_balance(wallet, Utils.ENQ_TOKEN_NAME);
circ -= BigInt(balance.amount);
}
return circ;
};
// Can be changed by token info
async get_tsup(){
let tsup = (await this.db.get_tokens([Utils.ENQ_TOKEN_NAME]))[0].total_supply;
return tsup;
};
// async get_msup(){
// let token_enq = (await this.db.get_tokens([Utils.ENQ_TOKEN_NAME]))[0];
// return token_enq.max_supply;
// };
async get_cg_usd(){
let tmp = await Utils.http.get('https://api.coingecko.com/api/v3/simple/price',
{
ids : 'enq-enecuum',
vs_currencies : 'usd'
});
return tmp['enq-enecuum'].usd;
};
async get_cg_btc(){
let tmp = await Utils.http.get('https://api.coingecko.com/api/v3/simple/price',
{
ids : 'enq-enecuum',
vs_currencies : 'btc'
});
return tmp['enq-enecuum'].btc;
};
async get_cg_eth(){
let tmp = await Utils.http.get('https://api.coingecko.com/api/v3/simple/price',
{
ids : 'enq-enecuum',
vs_currencies : 'eth'
});
return tmp['enq-enecuum'].eth;
};
async get_cg_token_usg(cg_id){
let tmp = await Utils.http.get('https://api.coingecko.com/api/v3/simple/price',
{
ids : cg_id,
vs_currencies : 'usd'
});
return tmp[cg_id].usd;
};
async get_cg_tokens_usg(cg_ids){
let data = await Utils.http.get('https://api.coingecko.com/api/v3/simple/price',
{
ids : cg_ids.join(','),
vs_currencies : 'usd'
});
return data;
};
async get_accounts_count(){
let tmp = await this.db.get_accounts_count();
return parseInt(tmp.count);
};
async get_poa_reward(){
let tmp = await this.db.get_poa_reward();
return tmp.reward;
}
async get_pow_reward(){
let tmp = await this.db.get_pow_reward();
return tmp.reward;
}
async get_pos_reward(){
let tmp = await this.db.get_pos_reward();
return tmp.reward;
}
async get_peer_count(type){
let tmp = await this.db.get_peer_count(type);
return tmp.count === undefined ? 0 : tmp.count;
}
async get_difficulty(){
let data = await this.db.get_difficulty(5760);
return data.difficulty;
}
async get_height(){
let height = await this.db.get_mblocks_height();
// Update pos uptime
let db_data = {
n : (height.height - 1 - 5760)
};
let stat = await this.db.get_pos_statuses(db_data);
await this.db.update_pos_statuses(stat);
return height.height;
}
async get_network_hashrate(){
let data = await this.db.get_difficulty(5760);
return Math.pow(2, data.difficulty);
}
async get_engaged_balance(){
let pos_stake = (await this.db.get_stats('total_daily_pos_stake'))[0].value;
let poa_stake = (await this.db.get_stats('total_daily_stake'))[0].value;
return BigInt(pos_stake) + BigInt(poa_stake);
}
async get_pos_active_count(){
let poses = await this.db.get_pos_contract_all();
let res = 0;
for(let pos of poses){
if(pos.uptime > 0)
res++;
}
return res;
}
async get_poa_capable_count(){
let res = await this.db.get_staking_poa_count();
return res.total;
}
async get_pos_total_count(){
let poses = await this.db.get_pos_contract_all();
return poses.length;
}
async get_proposed_inflation(){
let token_enq = (await this.db.get_tokens([Utils.ENQ_TOKEN_NAME]))[0];
let year_bloock_count = 60 * 60 * 24 * 365 / this.db.app_config.target_speed;
return year_bloock_count * token_enq.block_reward / token_enq.total_supply;
}
async get_block_time_30d_avg(){
let monthly = 5760 * 30;
let res = await this.db.get_avg_block_time(monthly);
return res;
}
async get_block_time_24h_avg(){
let daily = 5760;
let res = await this.db.get_avg_block_time(daily);
return res;
}
async get_txfee_hourly_24h_avg(){
let daily = 5760;
let res = (await this.db.get_tx_count_ranged(daily + 1))[0];
return res.count / 24;
}
async get_txfee_daily_30d_avg(){
let monthly = 5760 * 30;
let res = (await this.db.get_tx_count_ranged(monthly + 1))[0];
return res.count / 30;
}
async update_iptable(){
let unresolved_ips = await this.db.get_unresolved_ips();
unresolved_ips = unresolved_ips.slice(0, 50);
console.silly(`unresolved_ips = `, JSON.stringify(unresolved_ips));
let batch = unresolved_ips.map((ip) => {return {query:ip.ipstring.split(':')[0], fields:"status,query,city,country,countryCode,lat,lon"}});
console.silly('batch = ', JSON.stringify(batch));
let url = 'http://ip-api.com/batch';
if(this.config.ip_api_key !== undefined){
url = 'http://pro.ip-api.com/batch?key=' + this.config.ip_api_key;
}
if (batch.length > 0) {
let res = await Utils.http.post(url, batch);
if(res !== undefined && res !== 'Unprocessable Entity\n') {
if (res.length > 0) {
let values = res.filter(x => x.status === 'success')
.map(x => [x.query, x.country.replace(/[\\$'"]/g, "\\$&"),
x.countryCode, x.city.replace(/[\\$'"]/g, "\\$&").substr(0,40),
x.lat, x.lon]);
if(values.length > 0){
this.db.update_iptable([values]);
}
}
}
else {
console.info('IP-API empty response');
}
}
}
}
module.exports.StatService = StatService;