-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
218 lines (206 loc) · 7.21 KB
/
app.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
'use strict';
const config = {
"chain": "juno-1",
"addr_prefix": "juno",
"rpc": "https://rpc-juno-old-archive.cosmoapi.com",
"output": "/home/ubuntu/output.csv", // must be .csv
"startBlock": 1, // must not be 0
"maxBlocks": 0 // 0 to walk until latest block
}
const { Tx } = require('cosmjs-types/cosmos/tx/v1beta1/tx');
const { PubKey } = require('cosmjs-types/cosmos/crypto/secp256k1/keys');
const { pubkeyToAddress } = require('@cosmjs/amino');
const { PublicKey } = require('@injectivelabs/sdk-ts');
const axios = require('axios');
const ObjectsToCsv = require('objects-to-csv')
function calculateFeeTotals(data) {
data.forEach((relayer) => {
if (relayer.hasOwnProperty('txs')) {
var totalFees = [];
relayer.txs.forEach((tx) => {
var feeamounts = tx.authInfo.fee.amount;
var valid = false;
feeamounts.forEach((fee) => {
for (var i = 0; i < totalFees.length; i++) {
if (fee.denom == totalFees[i].denom) {
totalFees[i].amount = parseInt(totalFees[i].amount) + parseInt(fee.amount);
valid = true;
}
}
if (valid == false) {
totalFees.push(fee);
}
});
});
if (relayer.hasOwnProperty('total_fees') == false) {
relayer.total_fees = totalFees;
relayer.total_txs = relayer.txs.length;
}
else {
totalFees.forEach((newFee) => {
var valid = false;
relayer.total_fees.forEach((fee) => {
if (fee.denom == newFee.denom) {
valid = true;
fee.amount = parseInt(fee.amount) + parseInt(newFee.amount)
}
});
if (valid == false) {
relayer.total_fees.push(newFee);
}
});
relayer.total_txs = parseInt(relayer.total_txs) + parseInt(relayer.txs.length);
}
delete relayer.txs;
}
});
return data;
}
function sortRelayTxs(txs, data) {
txs.forEach((tx) => {
let address = "";
if (tx.authInfo.fee.granter == "") {
if (tx.authInfo.signerInfos[0].publicKey.typeUrl.includes("ethsecp256k1.PubKey")) {
let key = PubKey.toJSON(PubKey.decode(tx.authInfo.signerInfos[0].publicKey.value)).key.toString();
let pubkey = PublicKey.fromBase64(key)
address = pubkey.toAddress().toBech32(config.addr_prefix);
}
else {
let key = PubKey.toJSON(PubKey.decode(tx.authInfo.signerInfos[0].publicKey.value)).key.toString();
let pubkey = {
"type": "tendermint/PubKeySecp256k1",
"value": key
}
address = pubkeyToAddress(pubkey, config.addr_prefix);
}
}
else address = tx.authInfo.fee.granter;
var indb = false;
for (var i = 0; i < data.length; i++) {
if (data[i].address == address) {
if (data[i].hasOwnProperty('txs')) {
data[i].txs.push(tx);
}
else data[i].txs = [tx]
indb = true;
}
}
if (indb == false) {
data.push({
"address": address,
"txs": [tx]
});
}
});
return data;
}
async function blockwalker(maxblocks) {
var results = [];
var data = [];
var block = 0;
var repeat = true;
while (repeat) {
var valid = true;
if (block == 0) {
block = config.startBlock;
console.log(`-> Start block: ${config.startBlock}, end block: ${config.startBlock + maxblocks} - walking blocks...`);
}
try {
var res = await axios.get(config.rpc + '/block?height=' + block);
}
catch (e) {
console.log(e);
valid = false;
console.log('waiting 5s to try again...');
await new Promise(resolve => setTimeout(resolve, 5000)); // wait 5s if node kicks
}
if (valid) {
block++;
let txs = res.data.result.block.data.txs;
let ibcCounter = 0;
txs.forEach((tx) => {
var isIbcTx = false;
let buff = Buffer.from(tx, 'base64');
try {
let msgs = Tx.decode(buff).body.messages;
msgs.forEach((msg) => {
if (msg.typeUrl.includes('/ibc') && msg.typeUrl != "/ibc.applications.transfer.v1.MsgTransfer") {
isIbcTx = true
}
});
} catch(e) {
console.log(e);
}
if (isIbcTx) {
var log_data = Tx.decode(buff);
delete log_data.body;
delete log_data.signatures;
results.push(log_data);
ibcCounter++;
}
});
if (ibcCounter != 0) {
console.log(`block ${block} logged txs: ${ibcCounter}`);
}
}
// sort txs, calculate totals & purge tx data every 10k results to save RAM
if (results.length >= 10000) {
data = sortRelayTxs(results,data);
data = calculateFeeTotals(data);
results = [];
}
if (block >= (config.startBlock + maxblocks)) {
data = sortRelayTxs(results, data);
data = calculateFeeTotals(data);
repeat = false;
}
}
return data;
}
async function getLastBlock() {
var tryCons = false;
try {
var res = await axios.get(config.rpc + '/status');
}
catch (e) {
console.log(e);
tryCons = true;
}
if (tryCons) {
try {
var res = await axios.get(config.rpc + '/consensus_state');
}
catch (e) {
return false;
}
return parseInt(res.data.result.round_state['height/round/step'].split('/')[0]) - 1;
}
return res.data.result.sync_info.latest_block_height;
}
async function main() {
var maxblocks = config.maxBlocks;
if (maxblocks == 0) {
var latest = await getLastBlock();
if (latest != false) {
maxblocks = latest - config.startBlock;
}
else {
console.log("Error fetching latest height, please check your endpoint");
return;
}
}
var data = await blockwalker(maxblocks);
data.forEach((relayer) => {
console.log(relayer.address);
console.log(`total txs: ${relayer.total_txs}`);
relayer.total_fees.forEach((fee) => {
console.log(`denom: ${fee.denom} amount: ${fee.amount}`);
});
delete relayer.txs;
});
const csv = new ObjectsToCsv(data)
await csv.toDisk(config.output);
console.log("done");
return;
}
main();