How to decode an Ethereum Transaction
In this guide, we will go through the process of decoding an Ethereum transaction using Loop Decoder. For the simplicity of the example, we assume that that contract ABIs involved in the transaction are verified on Etherscan.
We recomend to copy all snipepts to a typescript project and run it at the end of this guide.
Prerequisites
Create a new project
Optionally, you can create a new project to follow along, or skip to Required packages.
- Generate an empty project using npm:
- Initialize typescript:
- Ensure that you have
strict
flag set totrue
intsconfig.json
:
-
Create a new file
index.ts
where you will add the code snipets from this guide. -
Add a new script to
package.json
:
- Run the example:
Required packages
For this guide, you will need to have the following packages installed:
Data Sources
Loop Decoder requires some data sources to be able to decode transactions. We will need an RPC provider, a data source to fetch Contracts ABIs and a data source to fetch contract meta-information, such as token name, decimals, symbol, etc.
RPC Provider
We will start by creating a function which will return an object with PublicClient based on the chain ID. For the sake of this example, we will only support mainnet.
ABI loader
To avoid making unecessary calls to third-party APIs, Loop Decoder uses an API that allows cache. For this example, we will keep it simple and use an in-memory cache. We will also use some strategies to download contract ABIs from Etherscan and 4byte.directory. You can find more information about the strategies in the Strategies reference.
Create a cache for contract ABI:
Contract Metadata loader
Create an in-memory cache for contract meta-information. Using ERC20RPCStrategyResolver
we will automatically retrieve token meta information from the contract such as token name, decimals, symbol, etc.
Finally, you can create a new instance of the LoopDecoder class:
Decoding a Transaction
Now that we have all the necessary components, we can start decoding a transaction. For this example, we will use the following transaction: