15

I am using ethersjs.

I use the following code to get data of transaction

var init = function () {
  var wss = new ethers.providers.WebSocketProvider(url);
      wss.getTransaction(tx).then(function (transaction) {
         wss.on("pending", (tx) => {
           console.log(transaction.data);
           }
      });
  });

Which will log something similar to

0x7ff36ab50000000000000000000000000000000000000000000000bc18ba4144048bbab00000000000000000000000000000000000000000000000000000000000000080000000000000000000000000c0c5eb43e2df059e3be6e4fb0284c283caa5991900000000000000000000000000000000000000000000000000000000614d87a80000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c00000000000000000000000008ba0619b1e7a582e0bce5bbe9843322c954c340

Which when decoded should be like below

0   amountOutMin    uint256 3469769694220364004016
1   path    address[]   bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c
                        08ba0619b1e7a582e0bce5bbe9843322c954c340
2   to  address c0c5eb43e2df059e3be6e4fb0284c283caa59919
3   deadline    uint256 1632470952

I tried using parseTransaction as shown here but get function not defined. And also tried with decodeFunctionData but got same result that decodeFunctionData is not defined.

How can I get the information from the transaction data using ethers.js?

Thanks

1 Answer 1

23

You need the input types as well to be able to decode it.

const data = '0x7ff36ab50000000000000000000000000000000000000000000000bc18ba4144048bbab00000000000000000000000000000000000000000000000000000000000000080000000000000000000000000c0c5eb43e2df059e3be6e4fb0284c283caa5991900000000000000000000000000000000000000000000000000000000614d87a80000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c00000000000000000000000008ba0619b1e7a582e0bce5bbe9843322c954c340';

ethers.utils.defaultAbiCoder.decode(
  ['uint256', 'address[]', 'address', 'uint256'],
  ethers.utils.hexDataSlice(data, 4)
)
// gives: [e, ["0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", "0x08ba0619b1e7A582E0BCe5BBE9843322C954C340"], "0xC0C5eb43E2dF059e3Be6E4fb0284C283CAa59919", e] (4)

If plugging in the ABI is more convenient for you then you can use interface:

const iface = new ethers.utils.Interface(['function swapExactETHForTokens(uint256 amountOutMin, address[] path, address to, uint256 deadline)'])

iface.decodeFunctionData('swapExactETHForTokens', '0x7ff36ab50000000000000000000000000000000000000000000000bc18ba4144048bbab00000000000000000000000000000000000000000000000000000000000000080000000000000000000000000c0c5eb43e2df059e3be6e4fb0284c283caa5991900000000000000000000000000000000000000000000000000000000614d87a80000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c00000000000000000000000008ba0619b1e7a582e0bce5bbe9843322c954c340')
// gives: [e, ["0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", "0x08ba0619b1e7A582E0BCe5BBE9843322C954C340"], "0xC0C5eb43E2dF059e3Be6E4fb0284C283CAa59919", e] (4)

For Ethers v6 you only need to remove the utils for instance:

ethers.defaultAbiCoder.decode(
  ['uint256', 'address[]', 'address', 'uint256'],
  ethers.dataSlice(data, 4)
)
2
  • How does this look like in v6? ethers.utils.hexDataSlice does not exist in v6
    – Ini
    Commented Nov 27, 2023 at 0:35
  • 2
    It has been renamed to ethers.dataSlice Commented Nov 28, 2023 at 6:16

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.