-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
244 lines (213 loc) · 6.08 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
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 top100Contracts = [
{
address: "0xb54D6F958C3940db47ccfD65125a2A31D9FCb756",
tokenValue: "{}",
interactedAddresses: [],
},
{
address: "0x1278C74c3B2f8c3BcA0089b4E128fAf023615ECf",
tokenValue: "{}",
interactedAddresses: [],
},
{
address: "0xCd16dF514a501596a8E24fE1dC9c9be9c9091285",
tokenValue: "{}",
interactedAddresses: [],
},
{
address: "0xb6c02600D9956EDd226E87bB6F82cEa1ead8822F",
tokenValue: "{}",
interactedAddresses: [],
},
{
address: "0x704179beB09282EaEf98CA8aaa443C1E273eBBc2",
tokenValue: "{}",
interactedAddresses: [],
},
{
address: "0x0faf504bee22AF6E92D6697Af2EAfB9941a1712D",
tokenValue: "{}",
interactedAddresses: [],
},
{
address: "0xD4957F86CC075D769a77832d5ec3A375E247c45c",
tokenValue: "{}",
interactedAddresses: [],
},
{
address: "0xe2d792d64A36797f8d3E0F150B82d1E35Da76136",
tokenValue: "{}",
interactedAddresses: [],
},
{
address: "0xC3f16f2a1C4469F931148e88622A45bF60804b68",
tokenValue: "{}",
interactedAddresses: [],
},
{
address: "0xB2e3EEd25825E8c3946e403B8E8D943976E484E4",
tokenValue: "{}",
interactedAddresses: [],
},
];
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;
}
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 = [];
for (const transaction of transactions) {
const { from, to, timeStamp, blockNumber } = transaction;
const startBlock = parseInt(blockNumber) - 1;
count++;
console.log(count);
const index = top100Contracts.findIndex(
(contract) => contract.address == to
);
if (index == -1) {
continue;
}
if (!top100Contracts[index].interactedAddresses.includes(from)) {
if (top100Contracts[index].interactedAddresses.length == 100) {
continue;
}
top100Contracts[index].interactedAddresses.push(from);
}
let obj = JSON.parse(top100Contracts[index].tokenValue);
let retries = 0;
const maxRetries = 3;
let endBlock;
while (retries < maxRetries) {
try {
endBlock = (await dater.getDate((timeStamp + 10 * 60) * 1000)).block;
const [startingBalance, endingBalance] = await Promise.all([
provider.getBalance(from, startBlock),
provider.getBalance(from, parseInt(endBlock)),
]);
const startingBalanceInt = parseInt(startingBalance._hex);
const endingBalanceInt = parseInt(endingBalance._hex);
obj = changetokenValue(
"ETH",
startingBalanceInt,
endingBalanceInt,
obj
);
break;
} catch (err) {
console.log(err);
retries++;
await delay(retryDelay);
}
}
const callData = iface.encodeFunctionData("balanceOf", [from]);
const tokenPromises = top10Token.map(async (token) => {
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
);
break;
} catch (err) {
console.log(err);
retries++;
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,
});
}
tokenUpdate.forEach(async (update) => {
const index = top100Contracts.findIndex(
(contract) => contract.address == update.to
);
top100Contracts[index].tokenValue = update.tokenValue;
});
console.log(top100Contracts);
}
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
drainedAccounts("POLYGON_MAINNET", POLYGON_RPC_URL, top10Token_POLYGON);