cover-img

How to decode transactions data using Ethers.js

Decoding Ethereum transaction input data

18 April, 2022

13

13

1

Contributors

This post is a continuation of my previous article How to listen to pending transactions using Ethers.js. If you haven't read it, I suggest reading it first before crushing this article. The last post was about how we can listen to or monitor pending transactions in the txpool or mempool. This post will explain how we can understand the transactions in the mempool. For example, what input does the sender send to the address of a smart contract?

The previous article used an example that scans data or input from ALL existing pending transactions and looks for which data contains the hash signature 0xbaa2abde. We can use any event signature. For example, we can also use 0xf305d719 to get the addLiquidityETH event.


0xbaa2abde is the signature hash for the Uniswap V2 removeLiquidity event. So in plain language, those codes will give all the transaction hashes of the transactions that attempted to send the removeLiquidity event. We can use any event signature. For example, we can also use 0xf305d719 to get the addLiquidityETH event. The code above would then look like this:


The if (pendingTx.data.indexOf("0xf305d719") !== -1) condition that we put, will return only transaction which contain "0xf305d719".

The next question is how can we know which wallet address sent the transaction, to which contract address the transaction was sent (we know there are lots of clones of Uniswap V2), and from what tokens the liquidity will be removed, in what amount, the gas limit, and so on. And the condition that we put if (pendingTx.data.indexOf("0xf305d719") !== -1), will return only transaction which contain "0xf305d719".

await connect.provider.getTransaction(txHash) will return transaction object like this:


Looking at the object that we get from provider.getTransaction(), there are other values besides "data". We can see the from, to, value, gasLimit, and gasPrice. These values are valuable information for us. Without even decoding the value of "data", we already have much critical information about a transaction. If we want to filter only for Uniswap V2, we can add another filter using the to value. The to value contains the target address. So if the transaction is sent to the Uniswap V2 contract address and the data contain a certain hash, we will further process the data.

How to decode Ethereum transaction input data?

We can decode Ethereum transaction input data using smart contract ABI and Ethers.js Interface. Here's a reference article about ABI. The interface is needed to abstract the encoding and decoding required to interact with contracts on the Ethereum network.

Where to find smart contract ABI?

  1. Go to EtherScan
  2. Enter the Smart Contract address
  3. Click on the Contract in the tab section heading
  4. Scroll down to find the Smart Contract ABI
  5. Click the Copy icon

Example ABI:


Create a new Interface instance

To be able to decode or parse transaction data using Ethers.js, we need to create a new Interface instance. Then, we must use the correct ABI and match the data with the signature hash.


Example of parsed transaction:


As we can see, without parsing or decoding the transaction data, we cannot see which token the liquidity is being added because that information is inside the transaction data. However, after we parse the transaction data, we can see the token address, the amount of token the sender desired, and the minimum token that the sender is willing to receive.

That's roughly the way to decode or parse transactions from Ethereum. I hope this article can be useful for those in need.

blockchain

web3

ethereum

ethers.js

13

13

1

blockchain

web3

ethereum

ethers.js

Edo
Analytics professional with 5+ years of experience in the context of Modern Data Stack, working primarily in startups during their early and growth st

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.