-
Notifications
You must be signed in to change notification settings - Fork 0
/
monit_winner_addr.bak.js
415 lines (383 loc) · 12.8 KB
/
monit_winner_addr.bak.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
const { sleep } = require("./utils/utils");
const fetch = require("node-fetch");
const TelegramBot = require("node-telegram-bot-api");
const HttpsProxyAgent = require("https-proxy-agent");
const ethers = require("ethers");
const fs = require("fs");
const app = {
data: {},
contracts: {},
};
// 其他配置文件
const configs = require("./configs/config.json");
const wssUrl = configs["providerWssUrl"];
const provider = new ethers.providers.WebSocketProvider(wssUrl);
// etherscan api keys
const apiKeys = configs.apiKeys;
// proxies
const proxies = configs.proxies;
const proxyAuth = configs.proxyAuth;
// telegram token
const telegramToken = configs.telegramToken;
const channelId = configs.telegramChannelId;
const teleBot = new TelegramBot(telegramToken);
let retryNum = 0;
const maxRetryNum = configs.maxRetryNum;
let db;
const dbFile = __dirname + "/dbs/mint.db";
const initDb = async () => {
console.log("Start initializing database.");
const sqlite3 = require("sqlite3").verbose();
const { open } = require("sqlite");
db = await open({
filename: dbFile,
driver: sqlite3.Database,
});
console.log("Finish initializing database.");
};
const runSql = async (sql) => {
db.run(sql, (err) => {
if (null != err) {
console.log(err);
process.exit();
}
});
};
const loadWinners = async () => {
const sql =
"SELECT address, latest_blknum FROM tb_monitoring_address WHERE status=0 ORDER BY id ASC";
const rows = await db.all(sql);
const addrs = [];
rows.map((row) => {
addrs.push({
addr: row.address,
latest_blknum: row.latest_blknum,
});
});
return addrs;
};
const monitorNFTTx = async (winner) => {
app.data.hash = {};
app.data.dataset = [];
try {
await fetchTokenNFTTx(winner);
await findNewAction(winner);
// 清空重试次数
retryNum = 0;
} catch (e) {
retryNum++;
if (retryNum > maxRetryNum) {
// 报警
process.exit();
}
await sleep(2000);
console.log(e);
console.log("exception retry");
await monitorNFTTx(winner);
}
};
const fetchTokenNFTTx = async (winner) => {
// 获取请求用的key
const index = Math.floor(Math.random() * apiKeys.length);
const apiKey = apiKeys[index];
// 获取请求用的代理
const pindex = Math.floor(Math.random() * proxies.length);
const proxy = proxies[pindex];
const url =
"https://api.etherscan.io/api/?module=account&action=tokennfttx&address=" +
winner.addr +
"&page=1&offset=50&sort=desc&apikey=" +
apiKey;
await fetch(url, {
method: "GET",
redirect: "follow",
timeout: 10000,
agent: new HttpsProxyAgent(
`http://${proxyAuth.username}:${proxyAuth.password}@${proxy.ip}:${proxy.port}`
),
})
.then((response) => {
return response.json();
})
.then((data) => {
// console.log(data);
if (data["status"] != 1) {
console.log("Connection Error");
}
data["result"].map((result) => {
// 只留新的交易
if (result.blockNumber <= winner.latest_blknum) {
return;
}
// 过滤掉ENS
if (
result.contractAddress ==
"0x57f1887a8bf19b14fc0df6fd9b2acc9af147ea85"
) {
return;
}
if (app.data.hash.hasOwnProperty(result.hash)) {
app.data.hash[result.hash]["nft"].push(result);
} else {
app.data.hash[result.hash] = {};
app.data.hash[result.hash]["nft"] = [];
app.data.hash[result.hash]["nft"].push(result);
}
});
});
};
const findNewAction = async (winner) => {
for (hash in app.data.hash) {
const consumed = [];
app.data.hash[hash]["nft"].map((nft) => {
const concat_ = (
nft.timeStamp +
nft.from +
nft.to +
nft.tokenName +
nft.tokenID
).toLowerCase();
if (consumed.includes(concat_)) {
nft["duplicate"] = true;
} else {
nft["duplicate"] = false;
consumed.push(concat_);
}
});
}
for (hash in app.data.hash) {
let obj = {};
let type = "";
const uniqueNfts = app.data.hash[hash]["nft"].filter((nft) => {
return !nft["duplicate"];
});
uniqueNfts.map((nft) => {
if (winner.addr == nft.to.toLowerCase()) {
if (
nft.from.toLowerCase() ==
"0x0000000000000000000000000000000000000000"
) {
type = "Mint";
} else if (nft.from.toLowerCase() != winner.addr) {
if (nft.gasUsed > 30000) {
type = "Mint";
} else {
type = "TransferIn";
}
}
} else if (winner.addr == nft.from.toLowerCase()) {
if (
nft.to.toLowerCase() ==
"0x0000000000000000000000000000000000000000"
) {
type = "Burn";
} else if (nft.to.toLowerCase() != winner.addr) {
if (nft.gasUsed > 30000) {
type = "Sell";
} else {
type = "TransferOut";
}
}
}
obj = {
blockNumber: nft["blockNumber"],
hash: hash,
transactionType: type,
standard: "ERC721",
addr: winner.addr,
from: nft["from"],
to: nft["to"],
contractAddress: nft["contractAddress"],
tokenID: nft["tokenID"],
tokenName: nft["tokenName"],
};
app.data.dataset.push(obj);
});
}
await sendAlarm();
};
const sendAlarm = async () => {
let message = "";
for (let i = 0; i < app.data.dataset.length; i++) {
const nft = app.data.dataset[i];
const contractAddr = nft.contractAddress;
if (nft.transactionType != "Mint") {
message += `<a href="https://etherscan.io/address/${
nft.addr
}">${nft.addr.slice(
-6
)}</a> has new <a href="https://etherscan.io/tx/${
nft.hash
}">tx</a>, type=${
nft.transactionType
}, collection=<a href="https://etherscan.io/token/${
nft.contractAddress
}">${nft.tokenName} (${nft.contractAddress.slice(
0,
6
)})</a>, tokenId=${nft.tokenID}\n`;
} else {
if (app.contracts[contractAddr] == null) {
app.contracts[contractAddr] = {
abi: "",
maxSupply: 0,
totalSupply: 0,
txInfo: null,
};
try {
// 获取contract的abi
await fetchAbi(contractAddr);
const abi = app.contracts[contractAddr].abi;
// 初始化contract,获取maxSupply, totalSupply
const contract = new ethers.Contract(
contractAddr,
abi,
provider
);
const maxSupply = await fetchMaxSupply(contract, abi);
const totalSupply = await contract.totalSupply();
app.contracts[contractAddr]["maxSupply"] =
maxSupply.toNumber();
app.contracts[contractAddr]["totalSupply"] =
totalSupply.toNumber();
// 获取tx细节
app.contracts[contractAddr]["txInfo"] = await fetchTxInfo(
nft.hash,
abi
);
// write JSON string to a file
const cfgFile = __dirname + `/configs/${contractAddr}.json`;
if (fs.existsSync(cfgFile)) {
fs.unlinkSync(cfgFile);
}
const cfg = JSON.stringify(
app.contracts[contractAddr],
null,
4
);
fs.writeFile(cfgFile, cfg, (err) => {
if (err) {
console.log(err);
}
});
} catch (e) {
console.log(e);
}
}
message += `(${app.contracts[contractAddr]["totalSupply"]}/${
app.contracts[contractAddr]["maxSupply"]
})<a href="https://etherscan.io/address/${
nft.addr
}">${nft.addr.slice(
-6
)}</a> has new <a href="https://etherscan.io/tx/${
nft.hash
}">tx</a>, type=${
nft.transactionType
}, collection=<a href="https://etherscan.io/token/${
nft.contractAddress
}">${nft.tokenName}(${nft.contractAddress.slice(
0,
6
)})</a>, tokenId=${nft.tokenID}\n`;
}
// 更新lastest block number
await runSql(
`update tb_monitoring_address set latest_blknum=${nft.blockNumber} where address='${nft.addr}'`
);
}
if (message != "") {
teleBot.sendMessage(channelId, message, {
parse_mode: "HTML",
});
}
// mint相关信息写入文件中, 文件名用contractAddress
};
const fetchAbi = async (contractAddr) => {
// 获取请求用的key
const index = Math.floor(Math.random() * apiKeys.length);
const apiKey = apiKeys[index];
// 获取请求用的代理
const pindex = Math.floor(Math.random() * proxies.length);
const proxy = proxies[pindex];
const url =
"https://api.etherscan.io/api?module=contract&action=getabi&address=" +
contractAddr +
"&apikey=" +
apiKey;
await fetch(url, {
method: "GET",
redirect: "follow",
timeout: 10000,
agent: new HttpsProxyAgent(
`http://${proxyAuth.username}:${proxyAuth.password}@${proxy.ip}:${proxy.port}`
),
})
.then((response) => {
return response.json();
})
.then((data) => {
if (data["status"] != 1) {
console.log("Connection Error");
}
app.contracts[contractAddr] = {
abi: data["result"],
};
});
};
const fetchMaxSupply = async (contract, abi) => {
let maxSupply = ethers.BigNumber.from(0);
if (abi.indexOf("MAX_SUPPLY") != -1) {
maxSupply = await contract.MAX_SUPPLY();
} else if (abi.indexOf("MAX_TOKENS") != -1) {
maxSupply = await contract.MAX_TOKENS();
} else if (abi.indexOf("maxSupply") != -1) {
maxSupply = await contract.maxSupply();
} else if (abi.indexOf("maxToken") != -1) {
maxSupply = await contract.maxToken();
} else if (abi.indexOf("collectionSize") != -1) {
maxSupply = await contract.collectionSize();
}
return maxSupply;
};
const fetchTxInfo = async (hash, abi) => {
const tx = await provider.getTransaction(hash);
const iface = new ethers.utils.Interface(abi);
const functionName = iface.getFunction(tx.data.slice(0, 10)).name;
const decodedArgs = iface.decodeFunctionData(tx.data.slice(0, 10), tx.data);
let arrArgs = [];
let mapArgs = {};
for (let k in decodedArgs) {
if (isInteger(k)) {
arrArgs.push(decodedArgs[k]);
} else {
mapArgs[k] = decodedArgs[k];
}
}
return {
gasLimit: tx.gasLimit,
maxPriorityFeePerGas: tx.maxPriorityFeePerGas,
maxFeePerGas: tx.maxFeePerGas,
value: tx.value,
functionName,
arrArgs,
mapArgs,
};
};
const isInteger = (obj) => {
return obj % 1 === 0;
};
const main = async () => {
await initDb();
const winners = await loadWinners();
// console.log(winners);
// 遍历地址,计算地址盈亏
for (let i = 0; i < winners.length; i++) {
const winner = winners[i];
console.log(`Start monitoring ${winner.addr}`);
await monitorNFTTx(winner);
console.log(`Finish monitoring ${winner.addr}`);
await sleep(2000);
}
};
main();