获取所有事件&解析参数
封装获取所有事件的方法
const getEvents = (address, topic0, fromBlock = 1) => {
return new Promise((resolve, reject) => {
axios({
method: 'get',
url: `https://api.hecoinfo.com/api?module=logs&action=getLogs
&fromBlock=${fromBlock}
&toBlock=latest
&address=${address}
&topic0=${topic0}
&apikey=982S8JF95E4K4J46SMA2Y4I93UZH5WRMIC`
}).then(async res => {
let result = res.data.result
if (result.length >= 1000) {
result = result.concat(await getEvents(address, topic0, Number(result[result.length - 1].blockNumber+1)))
} else {
resolve(result)
}
}).catch((e) => {
reject()
})
})
}
使用
import {getWeb3 as getClientWeb3, Contract as ClientContract} from '@chainstarter/multicall-client.js'
//事件名称
const eventName = 'CreatePropose'
const web3 = getClientWeb3(ChainId.HECO)
// 事件abi
const eventAbi = abis.find(item => item.name === eventName && item.type === 'event')
// 获取事件topic0
const topic0 = web3.eth.abi.encodeEventSignature(eventAbi)
// 获取eventName所有事件
const datas = await getEvents(voteMain.address, topic0, 1)
const eventsData= []
// 解析参数
for (let i = 0; i {
console.log(res)
})
获取所有事件同理,先获取最新的块高度,因为每次最多只能查5000个块,所以每次fromBlock与toBlock递增查询,当需要查询很多块的时候,这将不适用,不建议使用,但是用来监听最新块,用这个方法还是挺好的
方法参数解码const funcName = 'claim'
const funcAbi = abis.find(item => item.name === funcName && item.type === 'function')
const encodeFunS = web3.eth.abi.encodeFunctionSignature(funcAbi)
const paramsData = '0x' + decodeParams.replace(encodeFunS, '')
const data = web3.eth.abi.decodeParameters(funcAbi.inputs, '0x0.........')