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, or or you can copy the whole example from this file: Full Example Code. Do not forget to replace the placeholder YourApiKeyToken with your own free Etherscan API key.
Prerequisites
Create a new project
Optionally, you can create a new project to follow along, or skip to Required packages.
Install Bun:
First, make sure you have Bun installed on your system. If you haven’t installed it yet, you can do so using npm:
Generate and initialize a new project:
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 and add your free Etherscan API key instead of the placeholder YourApiKeyToken:
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: