forked from R4j4t-Singh/BlockListener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drainAccount.js
206 lines (176 loc) · 5.1 KB
/
drainAccount.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
const { ethers } = require("ethers");
const { PrismaClient } = require("@prisma/client");
const EthDater = require("ethereum-block-by-date");
const express = require("express");
const app = express();
const prisma = new PrismaClient();
const { top10Token_ETHEREUM, top10Token_POLYGON } = require("./constant");
const ETHEREUM_RPC_URL = process.env.ETHEREUM_RPC_URL;
const POLYGON_RPC_URL = process.env.POLYGON_RPC_URL;
const retryDelay = 10000;
let abi = ["function balanceOf(address account)"];
let iface = new ethers.utils.Interface(abi);
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, id) {
const provider = new ethers.providers.JsonRpcProvider(url);
const dater = new EthDater(provider);
const batchSize = 100;
const transactions = await prisma.Transactions.findMany({
where: {
id: { gt: id },
network: network,
},
orderBy: {
id: "asc",
},
take: batchSize,
});
const tokenUpdate = [];
for (const transaction of transactions) {
id = transaction.id;
const { from, to, timeStamp, blockNumber } = transaction;
const startBlock = parseInt(blockNumber) - 1;
let contract = await prisma.ContractAddresses.findFirst({
where: {
address: {
equals: to,
mode: "insensitive",
},
network: network,
},
});
if (contract == null) {
contract = await prisma.ContractAddresses.create({
data: {
address: to,
network: network,
},
});
}
let obj = JSON.parse(contract.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(id);
// console.log(tokenValue + "\n");
tokenUpdate.push({
to,
tokenValue,
});
}
tokenUpdate.forEach(async (update) => {
try {
await prisma.ContractAddresses.updateMany({
where: {
address: {
equals: update.to,
mode: "insensitive",
},
network: network,
},
data: {
tokenValue: update.tokenValue,
},
});
} catch (error) {
console.log(error);
}
});
setTimeout(function() {
drainedAccounts(network, url, top10Token, id);
}, 1 * 60 * 1000);
}
drainedAccounts("ETHEREUM_MAINNET", ETHEREUM_RPC_URL, top10Token_ETHEREUM, -1);
drainedAccounts("POLYGON_MAINNET", POLYGON_RPC_URL, top10Token_POLYGON, -1);
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}