-
Notifications
You must be signed in to change notification settings - Fork 454
/
Mempool.js
57 lines (48 loc) · 1.91 KB
/
Mempool.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
// provider.on("pending", listener)
import { ethers } from "ethers";
// 1. 创建provider和wallet,监听事件时候推荐用wss连接而不是http
console.log("\n1. 连接 wss RPC")
// 准备 alchemy API 可以参考https://github.com/AmazingAng/WTFSolidity/blob/main/Topics/Tools/TOOL04_Alchemy/readme.md
const ALCHEMY_MAINNET_WSSURL = 'wss://eth-mainnet.g.alchemy.com/v2/oKmOQKbneVkxgHZfibs-iFhIlIAl6HDN';
const provider = new ethers.WebSocketProvider(ALCHEMY_MAINNET_WSSURL);
let network = provider.getNetwork()
// network.then(res => console.log(`[${(new Date).toLocaleTimeString()}] 连接到 chain ID ${res.chainId}`));
console.log("\n2. 限制调用rpc接口速率")
// 2. 限制访问rpc速率,不然调用频率会超出限制,报错。
function throttle(fn, delay) {
let timer;
return function(){
if(!timer) {
fn.apply(this, arguments)
timer = setTimeout(()=>{
clearTimeout(timer)
timer = null
},delay)
}
}
}
const main = async () => {
let i = 0;
// 3. 监听pending交易,获取txHash
console.log("\n3. 监听pending交易,打印txHash。")
provider.on("pending", async (txHash) => {
if (txHash && i < 100) {
// 打印txHash
console.log(`[${(new Date).toLocaleTimeString()}] 监听Pending交易 ${i}: ${txHash} \r`);
i++
}
});
// 4. 监听pending交易,并获取交易详情
console.log("\n4. 监听pending交易,获取txHash,并输出交易详情。")
let j = 0
provider.on("pending", throttle(async (txHash) => {
if (txHash && j <= 100) {
// 获取tx详情
let tx = await provider.getTransaction(txHash);
console.log(`\n[${(new Date).toLocaleTimeString()}] 监听Pending交易 ${j}: ${txHash} \r`);
console.log(tx);
j++
}
}, 1000));
};
main()