forked from Dapp-Learning-DAO/Dapp-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmempool.js
37 lines (32 loc) · 1.02 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
const { ethers } = require('ethers');
require('dotenv').config();
async function main() {
const provider = new ethers.WebSocketProvider(`wss://sepolia.infura.io/ws/v3/${process.env.INFURA_ID}`);
function limitRPCRequest(fn, delay) {
let timer;
return function(){
if(!timer) {
fn.call(this, ...arguments)
timer = setTimeout(()=>{
clearTimeout(timer)
timer = null
},delay)
}
}
}
console.log("begin to listen to pending transactions")
let j = 0
var finishFlag = false
provider.on("pending", limitRPCRequest(async (txHash) => {
if (txHash && j <= 20) {
// get the detail of transactions
let tx = await provider.getTransaction(txHash);
console.log(`[${(new Date).toLocaleTimeString()}] : ${txHash}`);
console.log(tx);
j++
}
}, 1000));
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()