web3.eth.abi.decodeLog 返回错误的日志参数值

And*_*ang 5 javascript ethereum solidity web3js

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

event Apple(address indexed a, address b, address c);
Run Code Online (Sandbox Code Playgroud)

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

通过 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)
Run Code Online (Sandbox Code Playgroud)

我最终得到的是:

Result {
  '0': '0x42087b16F33E688a9e73BFeef94F8F2bd2BfC98f',
  '1': '0xfc36bFe712f30F75DF0BA9A60A109Ad51ac7Ca38',
  '2': '0x6915d2f3D512F7CfEF968f653D1cA3ed4489798C',
  __length__: 3,
  a: '0x42087b16F33E688a9e73BFeef94F8F2bd2BfC98f',
  b: '0xfc36bFe712f30F75DF0BA9A60A109Ad51ac7Ca38',
  c: '0x6915d2f3D512F7CfEF968f653D1cA3ed4489798C' }
Run Code Online (Sandbox Code Playgroud)

其中a触发的事件之间始终是相同的地址。我为每笔交易生成一个新合约,a 是这个新合约的地址(我已经通过从生成的合约中触发一个单独的事件来验证其正确性,该事件也发出 的值a),因此解析值aforevent Apple肯定是不正确的。

以前有人遇到过这个吗?

我正在使用 web3 1.0.0-beta.33

And*_*ang 6

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

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

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

听起来传递的主题应该只引用索引参数。在对主题[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))
Run Code Online (Sandbox Code Playgroud)