【问题标题】:web3.eth.abi.decodeLog returns incorrect log parameter valuesweb3.eth.abi.decodeLog 返回不正确的日志参数值
【发布时间】:2018-10-21 08:06:32
【问题描述】:

我有一个以太坊合约,其事件定义如下:

event Apple(address indexed a, address b, address c);

事件被触发,我可以在交易收据中看到日志。

通过 web3,当我尝试从收据解析日志时,我能够检索事件参数,但看起来 a 的值始终相同。

// 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)
)

web3.eth.abi.decodeLog(eventJsonInterface.inputs, log.data, log.topics)

我最终得到的是:

Result {
  '0': '0x42087b16F33E688a9e73BFeef94F8F2bd2BfC98f',
  '1': '0xfc36bFe712f30F75DF0BA9A60A109Ad51ac7Ca38',
  '2': '0x6915d2f3D512F7CfEF968f653D1cA3ed4489798C',
  __length__: 3,
  a: '0x42087b16F33E688a9e73BFeef94F8F2bd2BfC98f',
  b: '0xfc36bFe712f30F75DF0BA9A60A109Ad51ac7Ca38',
  c: '0x6915d2f3D512F7CfEF968f653D1cA3ed4489798C' }

其中a 在触发的事件中始终是相同的地址。我正在为每笔交易生成一个新合约,a 是这个新合约的地址(我已经通过从生成的合约中触发一个单独的事件来验证它是正确的,该事件也发出a 的值),所以event Applea 解析值绝对不正确。

以前有人遇到过这种情况吗?

我正在使用 web3 1.0.0-beta.33

【问题讨论】:

  • 你能分享发出事件的代码吗?

标签: ethereum solidity web3


【解决方案1】:

在仔细查看了 web3 文档后,我意识到当你使用 decodeLog 时,你不能在主题数组中传递事件的编码名称。我相信非匿名事件将事件名称作为主题数组中的第一项。

来自https://web3js.readthedocs.io/en/1.0/web3-eth-abi.html#decodelog

topics - Array:带有日志索引参数topics的数组, 如果是非匿名事件,则没有主题 [0],否则使用 主题[0]。

听起来传递的主题应该只引用索引参数。在我对 topic[0] 进行切片后,我开始得到正确的结果。

// 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)
)

web3.eth.abi.decodeLog(eventJsonInterface.inputs, log.data, log.topics.slice(1))

【讨论】:

    猜你喜欢
    • 2020-08-29
    • 1970-01-01
    • 2021-12-14
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多