-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwatchDog.js
295 lines (270 loc) · 8.29 KB
/
watchDog.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
import Web3 from 'web3';
import axios from 'axios';
import chalk from 'chalk';
import { DiscordRequest } from './utils.js';
const web3 = new Web3(`wss://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_AK}`);
const contractMap = new Map();
const queryMap = new Map();
const MIN = 3;
const NOTIFY_DEALY = MIN * 60 * 1000;
const QUERY_LIMIT = 30;
class WatchDog {
constructor() {
this.instance = axios.create({
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36',
'X-API-KEY': process.env.OS_AK
}
});
this.queryCount = 0;
}
init(channelId) {
this.channelId = channelId;
}
start() {
this.sendMsg('脚本启动🚀....');
this.subscribe();
this.timer = setInterval(() => {
if (contractMap.size > 0) {
// 取前3进行展示,其余删除
Array.from(contractMap.values())
.sort((a, b) => b.count - a.count)
.slice(0, 1)
.forEach(e => this.notify(e));
console.log(chalk.yellow('清理合约缓存...'));
contractMap.clear();
}
}, NOTIFY_DEALY);
}
getState() {
return `
🤖️机器人状态
合约缓存情况: ${contractMap.size} 条
abi缓存情况: ${queryMap.size} 条
abi请求数: ${this.queryCount} 条
`
}
stop() {
this.sendMsg('脚本关闭❌....');
this.subscription && this.subscription.unsubscribe(function (error, success) {
if (success) {
console.log('Successfully unsubscribed!');
}
});
clearInterval(this.timer);
}
async info() {
const top = Array.from((contractMap?.values() || []))
.sort((a, b) => b.count - a.count)
.slice(0, 1)?.[0];
if (top) {
this.sendMsg(this.getState() + await this.getMessage(top));
} else {
this.sendMsg(this.getState());
}
}
async sendMsg(msg) {
const endpoint = `/channels/${this.channelId}/messages`;
await DiscordRequest(endpoint, {
method: 'POST', body: {
content: '',
tts: false,
embeds: [{
title: '图狗播报',
description: msg
}]
}
});
}
/**
* 根据地址查询合约abi
* @param {*} address
* @returns abi[]
*/
async getAbi(address) {
console.log('查询合约:', address);
const old = queryMap.get(address);
if (old) {
console.log(chalk.yellow('使用缓存合约'));
return old
}
const that = this;
that.queryCount += 1;
console.log(chalk.green('发送查询合约请求'));
return await this.instance.get(`https://api.etherscan.io/api?module=contract&action=getabi&address=${address}&apikey=MT4K2JBC4VRH5JHFADE81PAN7RJCIE8HMM`).then(data => {
try {
const contractABI = JSON.parse(data.data.result);
if (queryMap.size >= 1000) {
queryMap.clear();
console.log(chalk.yellow('清理abi缓存...'));
}
queryMap.set(address, contractABI);
return contractABI;
} catch (e) {
console.log(chalk.red('查询abi失败', e));
queryMap.set(address, null);
return null;
}
}).catch(err => {
that.sendMsg('查询abi接口异常');
console.log(chalk.red('查询abi接口异常', err));
return null;
}).finally(() => {
that.queryCount -= 1;
});
}
async getOpenSeaInfoByContract(address) {
const that = this;
console.log('OS上的信息:', address);
return await this.instance.get(`https://api.opensea.io/api/v1/asset_contract/${address}`).then(data => {
return data.data || {};
}).catch(err=>{
that.sendMsg('查询opensea接口异常');
return {};
});
}
/**
* 是否达到最大请求数
*/
isFullQueryCount() {
return this.queryCount >= QUERY_LIMIT;
}
/**
* 过滤出abi中的mint方法
* @param {*} address
* @returns string[]
*/
async filterMintFunc(address) {
return await this.getAbi(address).then(data => {
if (!data) return [];
var contract = new web3.eth.Contract(data, address);
const { _jsonInterface } = contract;
const mintFuncs = _jsonInterface.filter(func => {
return /mint/.test((func.name || '').toLowerCase());
});
return mintFuncs;
});
}
/**
* methodId是否是调用的mint函数
* @param {*} abis
* @param {*} methodId
* @returns bool
*/
isCallMint(abis, methodId) {
return abis.map(e => e.signature).includes(methodId);
}
/**
* 根据methodId返回methodName
* @param {*} abis
* @param {*} methodId
* @returns string
*/
getMethodNameById(abis, methodId) {
return abis.find(e => e.signature === methodId)?.name;
}
/**
* 该交易是否免费
* @param {*} txData
* @returns bool
*/
isFree(txData) {
const { value, gasPrice, input } = txData;
const methodId = input.slice(0, 10);
return +value === 0 && +gasPrice > 0 && methodId.length === 10;
}
/**
* 获取methodId
* @param {*} txData
* @returns string
*/
getMethodId(txData) {
const { input } = txData;
return input.slice(0, 10);
}
/**
* 展示调用函数名
* @param {*} name
*/
showFuncName(name) {
name && console.log(`调用合约函数 ${name}`);
}
async getMessage(data) {
const osInfo = await this.getOpenSeaInfoByContract(data.to);
console.log(osInfo);
return `freemint项目${MIN}min继续发生mint:
名称: ${osInfo?.collection?.name || '未知项目'}
官网: ${osInfo?.collection?.external_link || '无官网信息'}
合约address: ${data.to}
mint函数: Function ${data.methodName} 调用次数 ${data.count} 次
合约: https://etherscan.io/address/${data.to}#code
OpenSea: ${osInfo?.collection?.slug ? `https://opensea.io/collection/${osInfo?.collection?.slug}` : '未知'}
税点: ${osInfo?.seller_fee_basis_points ? `${osInfo.seller_fee_basis_points / 100}%` : '未知'}
`;
}
async notify(data) {
this.sendMsg(await this.getMessage(data));
}
subscribe() {
const that = this;
this.subscription = web3.eth.subscribe('newBlockHeaders', async function (error, blockHeader) {
if (!error) {
const { number } = blockHeader;
const blockData = await web3.eth.getBlock(number);
if (blockData) {
const { transactions } = blockData;
// 达到了最大请求数,需要等待请求数降下来
if (that.isFullQueryCount()) {
console.log('请求数到达限制,跳过');
return;
}
// 过滤异常交易
if (!transactions || transactions.length === 0) return;
for (let txHash of transactions) {
const txData = await web3.eth.getTransaction(txHash);
// 过滤非免费和无数据交易
if (!txData) continue;
if (!that.isFree(txData)) continue;
const { to } = txData;
const methodId = that.getMethodId(txData);
// 获取缓存数据
const old = contractMap.get(to);
let methodName = '';
if (old) {
// 过滤掉不是mint的事件
if (!that.isCallMint(old.abis, methodId)) continue;
contractMap.set(to, {
count: old.count + 1,
abis: old.abis,
methodName: old.methodName,
to: old.to,
});
methodName = that.getMethodNameById(old.abis, methodId);
} else {
const abis = await that.filterMintFunc(to);
// 过滤掉无abi的情况
if (!abis || !abis.length) continue;
// 过滤掉不是mint的事件
if (!that.isCallMint(abis, methodId)) continue;
methodName = that.getMethodNameById(abis, methodId);
// 存入缓存数据
contractMap.set(to, {
count: 1,
abis,
methodName,
to,
});
}
that.showFuncName(methodName);
console.log('疑似白嫖:', txData);
}
}
return;
}
that.sendMsg('脚本异常....', error);
console.error(error);
});
}
}
export default new WatchDog();