You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

web3.eth.abi.decodeLog解析日志返回错误参数值求助

问题分析与解决方案

看起来你遇到的问题是因为web3.js decodeLog 方法的参数传递错误导致的,核心问题是indexed参数对应的topics没有正确过滤掉事件签名。

问题根源

在以太坊事件日志的结构里:

  • log.topics[0] 是事件的签名(也就是你通过eventJsonInterface.signature匹配到的那个值)
  • 剩下的log.topics[1...] 才是被标记为indexed的参数值(你的事件里只有a是indexed的,所以log.topics[1]才是a的正确值)

你当前调用decodeLog时,直接把完整的log.topics数组传了进去,这会让web3把事件签名当成第一个indexed参数的值,自然解析出的a就完全错误了。

修正后的代码

把第三个参数改成log.topics.slice(1),也就是去掉第一个元素(事件签名):

// compiled is the built contract. address is the contract address
const contract = new web3.eth.Contract(compiled.abi, address)
const eventJsonInterface = _.find(
  contract._jsonInterface,
  o => o.name === 'Apple' && o.type === 'event',
)
const log = _.find(
  receipt.logs,
  l => l.topics.includes(eventJsonInterface.signature)
)
// 关键修改:用slice(1)去掉事件签名topic,只传入indexed参数对应的topics
const decoded = web3.eth.abi.decodeLog(eventJsonInterface.inputs, log.data, log.topics.slice(1))

额外建议

你使用的web3版本是1.0.0-beta.33,这个版本属于早期测试版,存在不少已知的兼容性bug,建议升级到1.x系列的最新稳定版,能避免很多类似的奇怪问题。

内容的提问来源于stack exchange,提问作者Andy Pang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:08:14