-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
225 lines (196 loc) · 7.24 KB
/
index.mjs
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
import { defineChain, createPublicClient, http, erc20Abi, formatEther } from 'viem'
import fs from 'fs';
const TREASURY_ADDRESS = "0x245db945c485b68fdc429e4f7085a1761aa4d45d";
const WETH_ADDRESS = "0xc99a6a985ed2cac1ef41640596c5a5f9f4e19ef5";
const AXS_ADDRESS = "0x97a9107c1793bc407d6f527b77e7fff4d812bece";
const ronin = defineChain({
id: 2020,
name: 'Ronin',
network: 'ronin',
nativeCurrency: {
decimals: 18,
name: 'RON',
symbol: 'RON'
},
rpcUrls: {
public: {
http: ["$RPC_URL"]
},
default: { http: ["$RPC_URL"] }
},
blockExplorers: {
default: { name: 'Ronin Explorer', url: 'https://app.roninchain.com/' }
}
})
const publicClient = createPublicClient({
chain: ronin,
transport: http()
})
async function getBalance(tokenAddress, walletAddress, blockNumber) {
const contract = {
address: tokenAddress,
abi: erc20Abi,
blockNumber
};
try {
const balanceWei = await publicClient.readContract({ ...contract, functionName: 'balanceOf', args: [walletAddress] });
const balanceEther = formatEther(balanceWei, "wei");
return { balanceEther, balanceWei };
} catch (error) {
console.error("Error fetching balance:", error);
return null;
}
}
async function getTreasuryBalancesAndTransfers(fromBlock, toBlock) {
// Create filters for ETH and AXS transfers
const ethFilter = await publicClient.createContractEventFilter({
abi: erc20Abi,
address: WETH_ADDRESS,
args: {
to: TREASURY_ADDRESS,
},
eventName: 'Transfer',
fromBlock,
toBlock
});
const axsFilter = await publicClient.createContractEventFilter({
abi: erc20Abi,
address: AXS_ADDRESS,
args: {
to: TREASURY_ADDRESS,
},
eventName: 'Transfer',
fromBlock,
toBlock
});
// Fetch logs for both filters
const ethLogs = await publicClient.getFilterLogs({ filter: ethFilter });
const axsLogs = await publicClient.getFilterLogs({ filter: axsFilter });
// Process logs to inflow balances and group by block
const blocks = {};
const uniqueBlockNumbers = new Set([...ethLogs, ...axsLogs].map(log => log.blockNumber));
// Prepare to fetch balances for each unique block
const balancePromises = Array.from(uniqueBlockNumbers).map(async blockNumber => {
const ethBalancePromise = getBalance(WETH_ADDRESS, TREASURY_ADDRESS, blockNumber);
const axsBalancePromise = getBalance(AXS_ADDRESS, TREASURY_ADDRESS, blockNumber);
const blockPromise = publicClient.getBlock({
includeTransactions: false,
blockNumber: blockNumber
});
const [blockRPC, ethBalance, axsBalance] = await Promise.all([blockPromise, ethBalancePromise, axsBalancePromise]);
return { blockNumber: blockNumber.toString(), blockTimestamp: blockRPC?.timestamp.toString(), ethBalance, axsBalance };
});
const results = await Promise.all(balancePromises);
// Map fetched results back to blocks
results.forEach(({ blockNumber, blockTimestamp, ethBalance, axsBalance }) => {
if (!blocks[blockNumber]) {
blocks[blockNumber] = {
blockNumber,
blockTimestamp,
balance: {
ethEther: ethBalance?.balanceEther || '0',
ethWei: ethBalance?.balanceWei.toString() || '0',
axsEther: axsBalance?.balanceEther || '0',
axsWei: axsBalance?.balanceWei.toString() || '0'
},
inflow: {
ethEther: '0',
ethWei: '0',
axsEther: '0',
axsWei: '0'
},
transfers: [],
};
}
});
[...ethLogs, ...axsLogs].forEach(log => {
const blockNumber = log.blockNumber;
if (!blocks[blockNumber]) {
throw new Error(`Block ${blockNumber} not found in blocks`);
}
const transfer = {
from: log.args.from,
to: log.args.to,
value: log.args.value.toString(),
valueEther: formatEther(log.args.value, "wei"),
token: log.address === WETH_ADDRESS ? 'WETH' : 'AXS',
hash: log.transactionHash,
source: 'other', // Placeholder for future use
address: log.address,
blockHash: log.blockHash,
logIndex: log.logIndex,
transactionIndex: log.transactionIndex,
topics: log.topics,
};
// Update inflow balances based on transfers
if (transfer.token === 'WETH') {
blocks[blockNumber].inflow.ethWei = (BigInt(blocks[blockNumber].inflow.ethWei) + BigInt(transfer.value)).toString();
blocks[blockNumber].inflow.ethEther = formatEther(BigInt(blocks[blockNumber].inflow.ethWei), "wei");
} else {
blocks[blockNumber].inflow.axsWei = (BigInt(blocks[blockNumber].inflow.axsWei) + BigInt(transfer.value)).toString();
blocks[blockNumber].inflow.axsEther = formatEther(BigInt(blocks[blockNumber].inflow.axsWei), "wei");
}
// Add transfer to block
blocks[blockNumber].transfers.push(transfer);
});
// Convert blocks object to array and sort by block number
return Object.keys(blocks).map(blockNumber => ({
blockNumber,
transfersCount: blocks[blockNumber].transfers.length,
...blocks[blockNumber]
})).sort((a, b) => a.blockNumber - b.blockNumber);
}
const main = async () => {
console.time("Total Operation Time");
const finalBlock = (await publicClient.getBlock()).number;
// let startBlock = 36370765n; // Treasury wallet creation block
let startBlock = 16377111n; // Treasury wallet creation block
if (finalBlock < startBlock) {
console.error(`Start block number must be less than or equal to the latest block number: ${finalBlock}`);
res.status(400).send('Invalid start block');
return;
}
const blockRange = 125n;
const filename = `transfers_since_${startBlock.toString()}.json`;
let data = [];
if (fs.existsSync(filename)) {
data = fs.readFileSync(filename, { encoding: 'utf8' });
data = JSON.parse(data);
if (!Array.isArray(data)) {
throw new Error('Invalid data format');
}
// console.log(`Loaded ${data.length} data from file`);
// console.log(data);
const lastBlockProcessed = data.length > 0 ? BigInt(data[data.length - 1].blockNumber) : startBlock;
startBlock = lastBlockProcessed;
// console.log(`Resuming from block ${startBlock.toString()}`);
}
// console.log(`Starting to fetch transfers from block ${startBlock.toString()} to ${finalBlock.toString()}`);
for (let blockNumber = startBlock;blockNumber <= finalBlock;blockNumber += blockRange) {
let endBlock = blockNumber + blockRange - 1n;
if (endBlock > endBlock) {
endBlock = endBlock;
}
try {
// returns an array of blocks with balances and transfers
// console.log(`Fetching data for block ${blockNumber.toString()} to ${endBlock.toString()}`);
const res = await getTreasuryBalancesAndTransfers(blockNumber, endBlock);
// console.log(`Fetched ${res.length} logs for block ${blockNumber.toString()} to ${endBlock.toString()}`);
if (res && res.length > 0) {
data.push(...res);
fs.writeFileSync(filename, JSON.stringify(data, null, 2));
}
} catch (error) {
console.error(`Failed to write to file: ${error}`);
// retry block
blockNumber -= blockRange;
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
console.timeEnd("Total Operation Time");
}
main().then(() => {
// console.log('Done');
}).catch(error => {
console.error(`Error: ${error}`);
});