-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_block.js
154 lines (125 loc) · 4.39 KB
/
parse_block.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
const Big = require("big.js");
const db = require("./db");
const { addToMonthlyStat, addToDailyStat, Periods, PeriodBlocks } = require("./stat");
const minter = require("./minter_api");
const { TransactionType } = require("./minter_api");
const { sleep } = require("./utils");
const POW = Big(10).pow(18);
async function processNewBlocks(coin) {
let lastBlock = await db.get("lastBlock");
const status = await minter.getStatus();
let lastBlockHeight = parseInt(status["latest_block_height"]);
if (!lastBlock) {
lastBlock = {
height: lastBlockHeight - PeriodBlocks[Periods.Monthly]
};
}
while (lastBlock.height < lastBlockHeight) {
const block = await minter.getBlock(lastBlock.height);
if (!block) {
await sleep(5000);
continue;
}
console.log(`parse block=${lastBlock.height} tx=${block["transactions"].length}`);
if (block["transactions"] && block["transactions"].length) {
await processTransactions(lastBlock.height, block["transactions"], coin);
}
await db.set("lastBlock", lastBlock);
lastBlock.height++;
}
}
async function processTransactions(height, transactions, coin) {
for (let transaction of transactions) {
const data = processTransaction(height, transaction, coin);
if (data) {
await addToDailyStat(data, coin);
await addToMonthlyStat(data, coin);
}
}
}
function processTransaction(height, transaction, coin) {
if ((transaction.code !== undefined && transaction.code > 0) || transaction.log) {
return null;
}
const hash = transaction.hash;
const transactionData = {
hash: hash,
height: parseInt(height),
type: null,
addresses: [],
amount: 0
};
const addAddress = address => {
if (transactionData.addresses.indexOf(address) === -1) {
transactionData.addresses.push(address);
}
};
const addAmount = amountStr => {
const amount = parseFloat(
Big(amountStr)
.div(POW)
.toFixed(4)
);
transactionData.amount += amount;
};
switch (transaction.type) {
case TransactionType.Send:
transactionData.type = "send";
if (transaction.data.coin === coin) {
addAddress(transaction.from);
addAmount(transaction.data.value);
addAddress(transaction.data.to);
}
break;
case TransactionType.SellCoin:
transactionData.type = "sell";
if (transaction.data.coin_to_sell === coin) {
addAddress(transaction.from);
addAmount(transaction.data.value_to_sell);
}
if (transaction.data.coin_to_buy === coin) {
addAddress(transaction.from);
addAmount(transaction.tags["tx.return"]);
}
break;
case TransactionType.SellAllCoin:
transactionData.type = "sell";
if (transaction.data.coin_to_sell === coin) {
addAddress(transaction.from);
addAmount(transaction.tags["tx.sell_amount"]);
}
if (transaction.data.coin_to_buy === coin) {
addAddress(transaction.from);
addAmount(transaction.tags["tx.return"]);
}
break;
case TransactionType.BuyCoin:
transactionData.type = "buy";
if (transaction.data.coin_to_buy === coin) {
addAddress(transaction.from);
addAmount(transaction.data["value_to_buy"]);
}
if (transaction.data.coin_to_sell === coin) {
addAddress(transaction.from);
addAmount(transaction.tags["tx.return"]);
}
break;
case TransactionType.Multisend:
transactionData.type = "send";
for (let el of transaction.data.list) {
if (el.coin === coin) {
addAddress(transaction.from);
addAddress(el.to);
addAmount(el.value);
}
}
break;
}
if (transactionData.amount === 0 || !transactionData.addresses.length) {
return null;
}
return transactionData;
}
module.exports = {
processNewBlocks
};