Decoding Ethereum Smart Contract Events using ABIs

Blocknative Ethereum Web3

Application Binary Interfaces, or ABIs, are an integral part of how Ethereum smart contracts interact with the outside world. Without a smart contract’s ABI, a dapp or another smart contract has no way of knowing how to format calls to a contract or how to interpret the data it sends back. You can think of an ABI like a menu at a restaurant: without it, you wouldn't know what dishes the restaurant can prepare or how to order them. Similarly, without an ABI, a user or dapp wouldn't know what functions a contract has or how to call them.

ABIs are crucial when it comes to monitoring and decoding event data. When a smart contract emits an event, it publishes a log entry on the Ethereum blockchain. However, this log entry isn't immediately readable – it's a low-level encoding of the event data. An ABI includes the information needed to decode this data back into a human-readable format, making it possible to understand what the event represents.

This is especially valuable for understanding a transaction's impact, such as which other contracts or liquidity pools a transaction may have interacted with. In this blog, we’ll walk you through how to find a smart contract’s ABI and how to use it to monitor and decode smart contract transactions.

 

Decoding smart contract event data

Raw input data is not immediately comprehensible for humans, as it's a hexadecimal string. For example, undecoded transaction input data might look something like this:

```

0x7ff36ab5000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000989680000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000061b25ca00000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000061b25ff

```

When you're interacting with a smart contract, it's important to be able to understand what function is being called and what arguments are being passed to it. Decoding the input data can provide this information, which can help in verifying transactions and understanding your activity on the blockchain.

If you are monitoring a smart contract, or your transactions with a smart contract, you’ll need its ABI in order to fully understand what is occurring with your transaction.

After using an ABI, the hexadecimal input data could be decoded as:

```

Function: swapExactETHForTokens(uint256 amountOutMin, address[] path, address to, uint256 deadline)

Parameters:

amountOutMin: 6,000

path: [

   "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH

   "0x6B175474E89094C44Da98b954EedeAC495271d0F"   // DAI

]

to: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045

deadline: 1640995199

```

After decoding this sample transaction, we can now see that the user is calling the `swapExactETHForTokens` function on the Uniswap V2 Router contract. They are swapping ETH for at least `6,000` DAI tokens. The swap path provided goes through WETH (Wrapped ETH) to DAI. The tokens will be sent to `0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045 (vitalik.eth)`, and the transaction must be mined before the Unix timestamp `1640995199`.

 

Where to find a smart contract ABI

The ABI for a smart contract isn't directly stored on the Ethereum blockchain. Instead, developers typically publish the ABIs for their smart contracts alongside their source code. Some common places where you can find ABIs include:

  • Etherscan: Etherscan hosts verified source code for many smart contracts. When a contract's source code is verified, Etherscan generates the ABI from the source code and provides it in a readily usable format. You can find the ABI in the "Contract" tab of a contract's Etherscan page, under the "Contract ABI" section.
  • GitHub and other source code repositories: Developers may publish the source code for their contracts on GitHub or similar platforms. If the repository also includes the contract's compiled artifacts, the ABI will be included there.
  • NPM packages: Some projects distribute their contract ABIs and addresses via NPM (Node Package Manager). This is common for larger projects, such as OpenZeppelin, which provides reusable, secure smart contract components.
  • Official project websites and documentation: Projects often provide ABIs in their official documentation, especially if they expect other developers to interact with their contracts.

Automate Decoding for Advanced Transaction Monitoring and Simulations

After you acquire a smart contract's ABI, you'll need a way to apply it to the smart contract to decode the events you're monitoring. For this, you can upload the ABI for any smart contract directly into Blocknative’s free Mempool Explorer, which will automatically decode the input data for you.

In the Mempool Explorer screenshot below, you’ll see a pending transaction to the 1inch v4 Router contract. While you can see data such as the from and to address and gas data, any input data (as seen under "data" in the payload below) is left undecoded because the ABI isn't connected, As a result, you won't be able to look at the contract calls and method names. 

ABI 07

ABI 09

To locate the ABI, we'll search the “to” address that we're monitoring in Mempool Explorer (0x1111111254fb6c44bac0bed2854e76f90643097d) on Etherscan. From there we will look at the contract information to download the ABI.

ABI 08

Find the “Contract” tab and scroll down to find the ABI, which you’re going to copy. Open your favorite text editor application, make sure it’s in plain text mode and paste the code. Save the file as (for example) 1inch-exchange.JSON (the “dot json” is crucial).

Go back to Mempool Explorer and find the little ABI button. After you click on that, it's going to invite you to upload your ABI JSON file. When you hit open, it should turn green, which means it's a valid ABI and is now accepted. (If there were a problem with the ABI input, the ABI button would change from green to yellow to red as an indicator of the status of the ABIs decoding function.)

ABI 06

So now that we’ve uploaded this ABI, it will apply to all 1inch v4 Router transactions detected going forward. This enables us to see all the contract calls in the pending transactions with this smart contract.

ABI11

Now that you have the ABI, you can start to apply our advanced filters using the different controls now revealed, as shown in the screenshot below. 

ABI 04

You can build these filters into any monitoring API you’d like to receive notifications about the specific criteria important to you. You can create multiple subscriptions, each with its own API, each of which with different combinations of filters of your choosing. For example, maybe you'd like to monitor pending transactions that touch specific liquidity pools or tokens. Or maybe you'd like to use Blocknative's Simulation Platform that's built into Mempool Explorer to monitor net balance changes above a certain threshold for simulated pending transactions for a specific liquidity pool.

The possibilities are endless. The whole idea with Mempool Explorer’s monitoring subscriptions, filters, webhooks, and custom ABIs is to give you maximum flexibility and ease of use when working with live mempool data. That way, data is surfaced where you want it and is as actionable as possible. 

 

Sending Decoded Transaction Data Directly to Discord or Telegram

Now that you've learned how to decode smart contract events using ABIs, try connecting a webhook to Mempool Explorer and receive these notifications directly in your social app of choice using our step-by-step guide.

Or connect with our team directly to chat via Discord: https://discord.com/invite/zFaP9QMPZa 

Observe Ethereum

Blocknative's proven & powerful enterprise-grade infrastructure makes it easy for builders and traders to work with mempool data.

Visit ethernow.xyz

Want to keep reading?

Good choice! We have more articles.

blocknative-gas-estimator-extension-now-supports-multiple-blockchains!
Gas

Blocknative Gas Estimator Extension Now Supports Multiple Blockchains!

Big News: Blocknative Gas Estimator Extension Now Supports Multiple Blockchains! We're super..

understanding-blocknative's-gas-estimator:-why-do-some-predictions-show-the-same-values?
Gas

Understanding Blocknative's Gas Estimator: Why Do Some Predictions Show the Same Values?

Understanding Blocknative's Gas Estimator: Why Do Some Predictions Show the Same Values? If you've..

base-101:-how-gas-works-on-base
Gas

Base 101: How Gas Works On Base

Incubated and developed by the popular centralized exchange Coinbase, Base is an Ethereum Layer..

Connect with us. Build with us.

We love to connect with teams who are building with Blocknative. Tell us about your team and what you would like to learn.

"After first building our own infrastructure, we appreciate that mempool management is a difficult, expensive problem to solve at scale. That's why we partner with Blocknative to power the transaction notifications in our next-generation wallet."

Schedule a demo