Consensys / abi-decoder

Nodejs and Javascript library for decoding data params and events from ethereum transactions
GNU General Public License v3.0
633 stars 218 forks source link

Cannot decode tx data #56

Open kilicbaran opened 4 years ago

kilicbaran commented 4 years ago

This is somewhat an edge case but the ERC20 token transfer for this transaction cannot be decoded. I am not sure whether I should open an issue here or @ethersproject/abi.

Sample Code

const abiDecoder = require('abi-decoder');
const testABI = [{
    "constant": false,
    "inputs": [
        {
            "name": "_to",
            "type": "address"
        },
        {
            "name": "_value",
            "type": "uint256"
        }
    ],
    "name": "transfer",
    "outputs": [
        {
            "name": "",
            "type": "bool"
        }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}];
abiDecoder.addABI(testABI);
const testData = "0xa9059cbb0000000000000000000000043f47038d2b20566700a38cd718a537103f7696c9000000000000000000000000000000000000000000000000000000000903c63c";
const decodedData = abiDecoder.decodeMethod(testData);
console.log(decodedData);

Exception

/home/ubuntu/node_modules/@ethersproject/abi/lib/coders/array.js:139
                get: function () { throw value; }
                                   ^

_to: value out of range (argument="value", value=20, code=INVALID_ARGUMENT, version=bytes/5.0.1)
    at Logger.makeError (/home/ubuntu/node_modules/@ethersproject/logger/lib/index.js:179:21)
    at Logger.throwError (/home/ubuntu/node_modules/@ethersproject/logger/lib/index.js:188:20)
    at Logger.throwArgumentError (/home/ubuntu/node_modules/@ethersproject/logger/lib/index.js:191:21)
    at Object.hexZeroPad (/home/ubuntu/node_modules/@ethersproject/bytes/lib/index.js:263:16)
    at AddressCoder.decode (/home/ubuntu/node_modules/@ethersproject/abi/lib/coders/address.js:34:45)
    at /home/ubuntu/node_modules/@ethersproject/abi/lib/coders/array.js:93:31
    at Array.forEach (<anonymous>)
    at Object.unpack (/home/ubuntu/node_modules/@ethersproject/abi/lib/coders/array.js:71:12)
    at TupleCoder.decode (/home/ubuntu/node_modules/@ethersproject/abi/lib/coders/tuple.js:39:49)
    at AbiCoder.decode (/home/ubuntu/node_modules/@ethersproject/abi/lib/abi-coder.js:93:22) {
  reason: 'value out of range',
  code: 'INVALID_ARGUMENT',
  argument: 'value',
  value: 20,
  baseType: 'address',
  name: '_to',
  type: 'address'
}

I think that there is a typo in the input data for the address parameter and this causes a problem. There is a 4 instead of 0 (I added spaces before and after to make it more readable.)

Function signiture: 0xa9059cbb Address: 00000000000000000000000 4 3f47038d2b20566700a38cd718a537103f7696c9 Value: 000000000000000000000000000000000000000000000000000000000903c63c

Alex314Ru commented 4 years ago

I'm facing the same issue with this transaction at this moment.

aminlatifi commented 4 years ago

I have the same problem! Actually w had developed our decoder and were not using abi-decoder. Recently our code wasn't working and I tried abi-decoder as an alternative solution to see what happens! The same problem happens again.

Finally I fill all data with zero except the part I need the its value, it meets my requirement.

  const [log] = logs;
  const { data } = log;
  // Just keep homeTx
  log.data = `0x${'0'.repeat(128)}${data.substring(130, 130 + 64)}${'0'.repeat(
    data.length - 2 - 128 - 64,
  )};
dapplion commented 4 years ago

The transaction linked above is not constructed correctly (has a 0x04 byte prefixed to the address) but that shouldn't make the decode crash. Actually the code that is crashing is @ethersproject/abi so you may want to open an issue there saying that @ethersproject/abi should not assume that address parameters are prefixed by zeros

ricmoo commented 4 years ago

I've added support in ethers 5.0.12 for supporting legacy Solidity 0.4 external event data.

For more details, please see https://github.com/ethers-io/ethers.js/issues/891.