forked from R4j4t-Singh/BlockListener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
316 lines (279 loc) · 8.92 KB
/
test.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
const { ethers } = require("ethers");
const { PrismaClient } = require("@prisma/client");
const EthDater = require("ethereum-block-by-date");
require("dotenv").config();
const prisma = new PrismaClient();
const { top10Token_POLYGON } = require("./constant");
const winston = require('winston');
var fs = require('fs');
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
new winston.transports.File({ filename: 'combined.log' }),
],
});
const top100Contracts = [];
const POLYGON_RPC_URL = process.env.POLYGON_RPC_URL;
let abi = ["function balanceOf(address account)"];
let iface = new ethers.utils.Interface(abi);
const retryDelay = 10000;
function changetokenValue(name, startingBalance, endingBalance, obj) {
var netChange;
if (obj[name] == undefined) {
if (startingBalance != 0) {
netChange = ((endingBalance - startingBalance) / startingBalance) * 100;
} else {
netChange = 0;
}
obj[name] = {
initialValue: startingBalance,
diffvalue: endingBalance - startingBalance,
netChange: netChange,
};
} else {
const initialValue = obj[name]["initialValue"] + startingBalance;
const diffvalue = obj[name]["diffvalue"] + endingBalance - startingBalance;
if (initialValue != 0) {
netChange = (diffvalue / initialValue) * 100;
} else {
netChange = 0;
}
obj[name] = {
initialValue: initialValue,
diffvalue: diffvalue,
netChange: netChange,
};
}
return obj;
}
function getBlockTimeSeconds(network) {
if (network == 'POLYGON_MAINNET') {
return 2;
} else if (network == 'ETHEREUM_MAINNET') {
return 15;
} else {
throw new Error('Invalid network');
}
}
async function drainedAccounts(network, url, top10Token) {
const currTime = 1688803871;
const time2DaysAgo = 1688631070;
var count = 0;
const provider = new ethers.providers.JsonRpcProvider(url);
const dater = new EthDater(provider);
const transactions = await prisma.Transactions.findMany({
where: {
// to: {
// in: top100Contracts.map((contract) => contract.address),
// },
network: network,
timeStamp: {
gte: time2DaysAgo,
lte: currTime,
},
},
});
console.log(transactions.length);
const tokenUpdate = [];
let intervalFile = setInterval(() => {
fs.writeFile('drain-analysis.json', JSON.stringify(top100Contracts), 'utf8', (callback) => {
console.log('file updated', callback)
});
}, 30000);
let allPromises = [];
let completed = 0;
let _completed = {"1": 0, "2": 0, "3": 0, "4": 0, "5": 0};
let pendingPromises = 0;
for (let i = 0; i< transactions.length; i++) {
let transaction = transactions[i];
async function analyse(transaction) {
const { from, to, timeStamp, blockNumber } = transaction;
const startBlock = parseInt(blockNumber) - 1;
count++;
console.log(count);
let myI = count;
let index = top100Contracts.findIndex(
(contract) => contract.address == to
);
if (index == -1) {
// completed++;
// _completed["1"]++;
// _completed["2"]++;
// _completed["3"]++;
// _completed["4"]++;
// _completed["5"]++;
// return;
top100Contracts.push({
address: to,
tokenValue: {},
interactedAddresses: [],
});
index = top100Contracts.length - 1;
}
if (!top100Contracts[index].interactedAddresses.includes(from)) {
if (top100Contracts[index].interactedAddresses.length == 100) {
completed++;
_completed["1"]++;
_completed["2"]++;
_completed["3"]++;
_completed["4"]++;
_completed["5"]++;
return;
}
top100Contracts[index].interactedAddresses.push(from);
} else {
completed++;
_completed["1"]++;
_completed["2"]++;
_completed["3"]++;
_completed["4"]++;
_completed["5"]++;
return;
}
let obj = top100Contracts[index].tokenValue;
let _retries = 0;
const maxRetries = 3;
let endBlock;
while (_retries < maxRetries) {
try {
_completed["4"]++;
// endBlock = (await dater.getDate((timeStamp + 10 * 60) * 1000)).block;
endBlock = startBlock + (10 * 60 / getBlockTimeSeconds(network))
_completed["1"]++;
const [startingBalance, endingBalance] = await Promise.all([
provider.getBalance(from, startBlock),
provider.getBalance(from, parseInt(endBlock)),
]);
_completed["2"]++;
const startingBalanceInt = parseInt(startingBalance._hex);
const endingBalanceInt = parseInt(endingBalance._hex);
_completed["3"]++;
obj = changetokenValue(
"ETH",
startingBalanceInt,
endingBalanceInt,
obj
);
if (obj["ETH"]["netChange"] < -1) {
logger.info(`-- Negative netChange: ${to} - ETH ${JSON.stringify(obj["ETH"])}`);
}
break;
} catch (err) {
_retries++;
if (_retries >= maxRetries) {
console.log(err);
} else {
await delay(retryDelay);
}
}
}
_completed["5"]++;
const callData = iface.encodeFunctionData("balanceOf", [from]);
const tokenPromises = top10Token.map(async (token) => {
let retries = 0;
while (retries < maxRetries) {
try {
const [startingBalance, endingBalance] = await Promise.all([
provider.call({
to: token.address,
data: callData,
blockTag: startBlock,
}),
provider.call({
to: token.address,
data: callData,
blockTag: parseInt(endBlock),
}),
]);
const startingBalanceInt = parseInt(
ethers.BigNumber.from(startingBalance)
);
const endingBalanceInt = parseInt(
ethers.BigNumber.from(endingBalance)
);
obj = changetokenValue(
token.name,
startingBalanceInt,
endingBalanceInt,
obj
);
if (obj[token.name]["netChange"] < -1) {
logger.info(`-- Negative netChange: ${to} - ${token.name} ${JSON.stringify(obj[token.name])}`);
}
break;
} catch (err) {
retries++;
if (retries >= maxRetries) {
console.log(err);
} else {
await delay(retryDelay);
}
}
}
});
await Promise.all(tokenPromises);
const _tokenValue = JSON.stringify(obj);
console.log(
top100Contracts[index].address + " " + from + "\n" + _tokenValue + "\n"
);
tokenUpdate.push({
to,
tokenValue: obj,
});
console.log('myI', myI);
completed++;
console.log('completed', completed);
}
async function analyseWrapper(transaction) {
try {
pendingPromises++;
await analyse(transaction);
logger.info("contracts length: " + top100Contracts.length);
pendingPromises--;
} catch (err) {
console.log('analyseWrapper', err);
pendingPromises--;
}
}
while (true) {
console.log('checking next run', completed, count, transactions.length, pendingPromises);
if ((count - completed) < 100) {
// await new Promise((resolve) => setTimeout(resolve, 100));
allPromises.push(analyseWrapper(transaction));
break;
} else {
await new Promise((resolve) => setTimeout(resolve, 100));
}
}
};
const interval = setInterval(() => {
console.log(new Date(), 'completed', completed, _completed, 'count', count, 'pendingPromises', pendingPromises);
}, 1000);
console.log('all tx initiated')
await Promise.all(allPromises);
console.log('all tx completed')
clearInterval(interval);
tokenUpdate.forEach(async (update) => {
const index = top100Contracts.findIndex(
(contract) => contract.address == update.to
);
top100Contracts[index].tokenValue = update.tokenValue;
});
console.log(JSON.stringify(top100Contracts));
for(let i=0; i<top100Contracts.length; i++) {
const contract = top100Contracts[i];
Object.keys(contract.tokenValue).forEach(async (key) => {
if (contract.tokenValue[key].netChange < 0) {
console.log(contract.address, key, contract.tokenValue[key]);
logger.info(`>> Negative netChange: ${contract.address} - ${key} ${JSON.stringify(contract.tokenValue[key])}`);
}
});
}
clearInterval(intervalFile);
}
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
drainedAccounts("POLYGON_MAINNET", POLYGON_RPC_URL, top10Token_POLYGON);