-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodeTx.js
More file actions
61 lines (58 loc) · 2.29 KB
/
DecodeTx.js
File metadata and controls
61 lines (58 loc) · 2.29 KB
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
import { ethers } from "ethers";
const provider = new ethers.providers.WebSocketProvider('wss://mainnet.infura.io/ws/v3/ecdfcd0dfcb74b73b78aa17fad4dc83f')
let network = provider.getNetwork()
network.then(res => console.log(`[${(new Date).toLocaleTimeString()}]连接到chain-id:${res.chainId}`))
const contractABI = [
"function transfer(address, uint) public returns (bool)",
]
const iface = new ethers.utils.Interface(contractABI)
function throttle(fn, delay) {
let canCall = true
return function () {
if (canCall) {
fn.apply(this, arguments)
canCall = false
setTimeout(() => {
canCall = true
}, delay)
}
}
}
const functionSignature = 'transfer(address,uint)'
const selector = iface.getSighash(functionSignature)
console.log(`函数选择器是${selector}`)
let j = 0
provider.on('pending', async (txHash) => {
if (txHash) {
const tx = await provider.getTransaction(txHash)
j++
if (tx !== null && tx.data.indexOf(selector) !== -1) {
console.log(`[${(new Date).toLocaleTimeString()}]监听到第${j + 1}个pending交易:${txHash}`)
console.log(`打印解码交易详情:${JSON.stringify(iface.parseTransaction(tx), null, 2)}`)
console.log(`转账目标地址:${iface.parseTransaction(tx).args[0]}`)
console.log(`转账金额:${ethers.utils.formatEther(iface.parseTransaction(tx).args[1])}`)
provider.removeListener('pending', this)
}
}
}
)
/*provider.on("pending", throttle(async (txHash) => {
if (txHash) {
// 获取tx详情
let tx = await provider.getTransaction(txHash);
if (tx) {
// filter pendingTx.data
if (tx.data.indexOf(iface.getFunction("transfer").selector) !== -1) {
// 打印txHash
console.log(`\n[${(new Date).toLocaleTimeString()}] 监听Pending交易: ${txHash} \r`);
// 打印解码的交易详情
let parsedTx = iface.parseTransaction(tx)
console.log("pending交易详情解码:")
console.log(parsedTx);
// Input data解码
console.log("Input Data解码:")
console.log(parsedTx.args);
}
}
}
}, 100))*/