0

Using the abi for a smart contract that I got, I was trying to decode the input parameters for a transaction receipt using web3.eth.abi.decodeParameters function. The logs.json file is as below:

{
  "blockHash": "0x05578d94e81075639f393dd1252deda65c6ab244e399073b6ff946d26af0dce7",
  "blockNumber": 8945008,
  "contractAddress": "0x0E3A2A1f2146d86A604adc220b4967A898D7Fe07",
  "cumulativeGasUsed": 9263902,
  "from": "0xef536c805482FeB3D838426ED48320f77FD5F82b",
  "gasUsed": 6928008,
  "logs": [
    {
      "address": "0x0E3A2A1f2146d86A604adc220b4967A898D7Fe07",
      "topics": [
        "0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0",
        "0x0000000000000000000000000000000000000000000000000000000000000000",
        "0x000000000000000000000000ef536c805482feb3d838426ed48320f77fd5f82b"
      ],
      "data": "0x",
      "blockNumber": 8945008,
      "transactionHash": "0xc599d6ea2e6d4bd9d908f887a46040338105c20d2e8a08bc457b75180a660f17",
      "transactionIndex": 28,
      "blockHash": "0x05578d94e81075639f393dd1252deda65c6ab244e399073b6ff946d26af0dce7",
      "logIndex": 38,
      "removed": false
    }
  ],
  "logsBloom": "0x00000002000000000000000000000000000000000000000000800000000000000002000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000001000000000000000000400000000000000000020000000000000000000800000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000001000000020000000000000000000000000000000000000000000000000000000000000000000",
  "status": 1,
  "to": null,
  "transactionHash": "0xc599d6ea2e6d4bd9d908f887a46040338105c20d2e8a08bc457b75180a660f17",
  "transactionIndex": 28
}

The abi of the function OwnershipTransferred which was called as the first topic is

{
    "anonymous": false,
    "inputs": [
      {
        "indexed": true,
        "internalType": "address",
        "name": "previousOwner",
        "type": "address"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "newOwner",
        "type": "address"
      }
    ],
    "name": "OwnershipTransferred",
    "type": "event"
  }
]

The line where I try to decode data is

let value = web3.eth.abi.decodeParameters(['address', 'address'], '0x');
console.log(value);

And finally the error that I get is

Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.
    at ABICoder.decodeParameters (/home/ethereumpc1/Desktop/abi_extraction/node_modules/web3-eth-abi/src/index.js:332:15)
    at Object.<anonymous> (/home/ethereumpc1/Desktop/abi_extraction/index.js:28:26)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Please tell me what I am doing wrong.

1

From the ABI the event has two parameters that are indexed. The indexed parameters are not included in data field but in the topics array.

      "topics": [
        "0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0",
        "0x0000000000000000000000000000000000000000000000000000000000000000",
        "0x000000000000000000000000ef536c805482feb3d838426ed48320f77fd5f82b"
      ],

The first element is the event signature, the next two are your parameters.

If you want to decode an event log entry I'll suggest to use web3.eth.abi.decodeLog instead. It should be easier and error proof if event abi changes.

web3.eth.abi.decodeLog(eventABI, data, topics);
| improve this answer | |
  • If a transaction didn't trigger an event and hence the creation of topics, then how can we decode the input data or data? – mahamzaidi Jun 21 at 8:49
  • @mahamzaidi That will be better as a separate question. It is possible to examine input field from transaction but it will require the full abi. – Ismael Jun 21 at 15:44
  • 1
    Yes I already asked separately. – mahamzaidi Jun 21 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.