您当前的位置: 首页 >  web3

【03】

暂无认证

  • 6浏览

    0关注

    196博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

web3获取所有事件日志与解码

【03】 发布时间:2022-08-04 17:07:24 ,浏览量:6

获取所有事件&解析参数

封装获取所有事件的方法

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.........')
关注
打赏
1657344724
查看更多评论
立即登录/注册

微信扫码登录

0.0418s