1

I need to decode transaction input on the client side. Libraries like ethereum-input-data-decoder needs NodeJS dependencies like fs.

Now I decode the one type of contract methods as follows:

const getERC20TransferByHash = async (hash) => {
  const tx = await web3.eth.getTransaction(hash)
  if (
    tx !== null &&
    (tx.input.length === 138 || tx.input.slice(2, 10) === 'a9059cbb')
  ) {
    const receiver = web3.utils.toChecksumAddress('0x' + tx.input.slice(34, 74))
    const amount = web3.utils.toBN('0x' + tx.input.slice(74))
    const contractAddress = tx.to
    const sender = web3.utils.toChecksumAddress(tx.from)
    return { receiver, amount, contractAddress, hash, sender }
  }
  return {}
}

Decoding by web3.utils.toAscii(transaction.input) also don't work.

Is there a single way to make it easy?

1 Answer 1

6

Can you try decodeParameters? Like:

const erc20TransferABI = [{
        type: "address",
        name: "receiver"
    },{
        type: "uint256",
        name: "amount"
    }];
const decoded = web3.eth.abi.decodeParameters(
    erc20TransferABI,
    tx.input.slice(10)); // Or (11), not sure

Even perhaps, take erc20TransferABI from the ABI file itself.

4
  • The first 10 bytes is the signature function? Oct 4, 2021 at 19:56
  • The first 4 bytes, which appear as 8 characters when represented as a string. Then if you also need to remove "0x" from the string, then that makes 10 characters to remove in all. Oct 5, 2021 at 9:09
  • Update the answer.Instead of objects with keys name and type, you need paste array with types as string (address, uint256). Oct 5, 2021 at 9:42
  • What version of Web3 are you using? Perhaps this array or detailed object way for the ABI could be due to versions. Oct 5, 2021 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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