How to decode transactions data logs with ETH ABI

Henry Tirla
3 min readDec 28, 2023

--

Gaining specific information from the blockchain is most efficiently achieved through logs, offering diverse applications such as copy trading, monitoring multiple wallets, and tracking capital inflows and outflows in a project.

One intriguing challenge I recently encountered involves decoding data logs typically found in transaction receipts, as illustrated below.

Major decentralized exchanges utilizes different function routers for different categories of transactions and with the intricate encoding of the blockchain data passed through these functions it can become encumbering to take on the task of decoding different types of functions signatures data logs from different contract addresses.

In this article, we will explore two straightforward examples demonstrating how to decode transaction data logs using eth-abi. This library offers essential utilities for converting Python values to and from the Solidity binary ABI format.


Example 1- Transaction

We will be focusing on decoding the data log of the second log event of this transaction hash as shown below

Example-1 Specific Data Log
from eth_abi import abi
from web3 import Web3
# ABI for the transferFrom function
transfer_from_abi = ['address', 'address', 'uint256']
web3 = Web3(Web3.HTTPProvider('Enter Your Alchemy HTTP Node URL'))

txn_hash="0xfdc9939cb6bd71bdec1f86eccd44dc7faa79f98c82dac2ae1910e383971fbe8d"

tx_log = web3.eth.get_transaction_receipt(txn_hash).logs
topics= tx_log[1]['topics'] # 2th log
first_topic = web3.to_hex(topics[1])
second_topic = web3.to_hex(topics[2])
third_topic = web3.to_hex(topics[3])

DATA = topics[1] + topics[2] + topics[3]
decodedABI = abi.decode(['address', 'address', 'uint256'], DATA)
print(decodedABI)
Output

I believe the code should be self explanatory and one key point worth emphasizing is that the variable DATA must be in bytes. This aspect personally confused me initially, as I expected it to be in Hex format, mirroring how it’s displayed on Etherscan.

It’s important to understand that Etherscan performs the conversion from bytes to Hex for user-friendly readability. This discrepancy between the internal representation in bytes and the displayed Hex format on Etherscan can be a potential source of confusion.

Example 2. Transaction

Here we are interested in decoding the data log of the 6th event as shown below;

Example 2.
Decoded Data information
from web3 import  Web3
from eth_abi import abi

tx_hash = '0xb9ae909908fc911f547845c853422832538ef97ff2c569ac403ecbd252840c6e'
web3 = Web3(Web3.HTTPProvider('Enter Your Alchemy HTTP Node URL'))
txn_log = web3.eth.get_transaction_receipt(tx_hash).logs
topics = txn_log[5]['topics'] # 6th log
first_topic = web3.to_hex(topics[1])
second_topic = web3.to_hex(topics[2])
third_topic = web3.to_hex(txn_log[5]['data'])

DATA = topics[1] + topics[2] + txn_log[5]['data']

decodedABI = abi.decode(['address', 'uint256', 'uint256', 'uint256','uint256','address'], DATA)
print(decodedABI)
Output

The second example is more complex than the first and we keen observation I believe you can see the pattern involve to decode data logs using ETH ABI.

Useful Resources

Eth ABI Documentation

ABI types and their encodings

Decentralized Exchange Trading Useful Trading Scripts

--

--