web3.js 解析日志参数 data



decodeParameters
web3.eth.abi.decodeParameters(typesArray, hexString);
将ABI编码参数解码为其对应JavaScript类型。

参数
typesArray – Array|Object: 类型数组或者:ref:JSON 接口 输出数组。 参考 solidity 文档 查看类型列表。(译者注,Solidity 中文文档可以在这里查看 <https://learnblockchain.cn/docs/solidity/types.html>_ 。

hexString – String: 要解码的ABI字节码。

返回值
Object – 已解码参数的结果对象。

示例

web3.eth.abi.decodeParameters(['string', 'uint256'], '0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000');
> Result { '0': 'Hello!%!', '1': '234' }

web3.eth.abi.decodeParameters([{
    type: 'string',
    name: 'myString'
},{
    type: 'uint256',
    name: 'myNumber'
}], '0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000');
> Result {
    '0': 'Hello!%!',
    '1': '234',
    myString: 'Hello!%!',
    myNumber: '234'
}

web3.eth.abi.decodeParameters([
  'uint8[]',
  {
    "ParentStruct": {
      "propertyOne": 'uint256',
      "propertyTwo": 'uint256',
      "childStruct": {
        "propertyOne": 'uint256',
        "propertyTwo": 'uint256'
      }
    }
  }
], '0x00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000018');
> Result {
    '0': ['42', '24'],
    '1': {
        '0': '42',
        '1': '56',
        '2':
            {
                '0': '45',
                '1': '78',
                'propertyOne': '45',
                'propertyTwo': '78'
            },
        'childStruct':
            {
                '0': '45',
                '1': '78',
                'propertyOne': '45',
                'propertyTwo': '78'
            },
        'propertyOne': '42',
        'propertyTwo': '56'
    }
}