-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstat.js
More file actions
85 lines (68 loc) · 1.88 KB
/
stat.js
File metadata and controls
85 lines (68 loc) · 1.88 KB
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
const db = require("./db");
const Periods = {
Daily: "daily",
Monthly: "monthly"
};
const PeriodBlocks = {
[Periods.Daily]: 17280, // blocks per day
[Periods.Monthly]: 17280 * 30 // blocks per month
};
async function addStat(transactionData, coin, periodName) {
const key = coin + "_" + periodName;
const periodBlocks = PeriodBlocks[periodName];
const newData = [];
const oldData = await db.get(key);
if (oldData) {
for (let el of oldData) {
if (transactionData.height - el.height <= periodBlocks && el.hash !== transactionData.hash) {
newData.push(el);
}
}
}
newData.push(transactionData);
await db.set(key, newData);
}
async function getStat(coin, periodName) {
const key = coin + "_" + periodName;
const data = await db.get(key);
const uniqueAddresses = [];
let transactions = 0;
let volume = 0;
if (data) {
transactions = data.length;
for (let el of data) {
volume += el.amount;
for (let address of el.addresses) {
if (uniqueAddresses.indexOf(address) === -1) {
uniqueAddresses.push(address);
}
}
}
}
return {
uniqueAddresses: uniqueAddresses.length,
transactions,
volume
};
}
async function printStat(coin, periodName) {
const data = await getStat(coin, periodName);
if (data) {
for (let prop in data) {
console.log(`${prop}=${data[prop]}`);
}
}
}
async function addToDailyStat(transactionData, coin) {
await addStat(transactionData, coin, Periods.Daily);
}
async function addToMonthlyStat(transactionData, coin) {
await addStat(transactionData, coin, Periods.Monthly);
}
module.exports = {
Periods,
PeriodBlocks,
addToDailyStat,
addToMonthlyStat,
printStat,
};