Decoding an Ethereum Transaction (2024)

If you want to do anything interesting in Ethereum, you will have to interact with smart contracts. Whether you want to send ERC20 tokens like LINK or DAI, trade non fungible tokens like digital art, or earn interest on your crypto and interact with other DeFi products, a smart contract is always involved.

However, smart contracts are becoming increasingly complex. From proxy contracts to allow for upgradeability, to multi-send contracts that allow for the batching of transactions, what we are seeing is a rapid evolution of features that allow for the movement of one's assets.

You've also got DeFi transactions, they are often not generated by you or your code so you need to verify it's doing what you think it is. Additionally, understanding a method call is useful for:

  • Seeing and knowing that the contract methods calls are what you expect
  • Seeing the parameter types and values that allow you to understand how the contracts work
  • Analysing a contract to produce stats on method calls
  • Tracking interactions with key addresses
  • Writing your own rules to decide on which transactions to sign (more on that later)
  • Having fun, if you like that sort of thing

In part one of our new DeFi blog series, we will demonstrate and introduce what goes on inside an Ethereum transaction and how you can use Trustology’s custodial wallet platform to interact with smart contracts in a much more secure way. This knowledge will provide you with a good foundation in subsequent posts when we talk about our Firewall, Webhooks, and other DeFi services that we offer.

Let's dig in.

We’re going to start with a Gnosis Safe contract and a transaction a user would send that the Gnosis safe Dapp would generate.

Gnosis Safe is a popular wallet contract implemented by Gnosis. A multi-user organisation that wants to use Gnosis Safe will first have to define a list of accounts and a required threshold of signatures needed to send a transaction. Users will submit the transaction to the Safe, which will authorise the execution of the transaction only when the threshold required is reached. This way, the users have a tighter control of their funds.

The Gnosis safe has lots of features including offline signing (which saves on gas fees) but for now we’re going to take a look at the basic method for executing a transaction.

First port of call: Start with Etherscan

Let's start with a transaction as seen by Etherscan and see if we can get the output they do.

Decoding an Ethereum Transaction (1)

The transaction above is from a user that has interacted with the Gnosis Safe smart contract on the Ethereum mainnet and we can see the details directly on Etherscan.

Let’s start by looking at the “data” field. This is the field that Ethereum uses to decide what method to call on the “to” (or “Interacted with” in Etherscan) contract.

Here’s the full data payload below. As you can see, a lot of hex values. This looks difficult to understand but with a little skill and knowledge we can break this up into the important parts.

Decoding an Ethereum Transaction (2)

Start at the beginning

All “data” fields on an Ethereum transaction contain the name of the method that is to be called. This is the first thing we should look at. Etherscan shows this for us in the “Input Data” section:

Decoding an Ethereum Transaction (3)

In this section we can see what Etherscan has given us:

Function: execTransaction(address to, uint256 value, bytes data, uint8 operation, uint256 safeTxGas, uint256 dataGas, uint256 gasPrice, address gasToken, address refundReceiver, bytes signatures)

So, how did they get this?

Well, the first thing we need is to use the first 4 bytes (first 8 hex characters) of the input data, which is:6a761202

This hex value is derived from taking the method name and its argument types, removing any whitespace, taking the `keccak hash of the result, and then taking the first 4 bytes of it and displaying it in hex. With me so far?

Note: The parameter names are not included in the hash, only the types. This means that different contracts can have the same methodId's but call their parameters differently and potentially have different logic. Thus it is sensible to keep track of the contract which the method belongs to. There is a handy website that tracks these. Check out https://www.4byte.directory/ and enter the hash 6a761202. At time of writing there is only 1 registered method.

We have the source of the Gnosis contract so we know what the method name and the parameters are. We grab them from here.

In JavaScript, the following will output the keccak hash of our method plus parameters:

// import a keccak decoder or write your own
import { keccak } from "../decoder/keccak";

const method = `execTransaction(address to, uint256 value, bytes data, uint8 operation, uint256 safeTxGas, uint256 dataGas, uint256 gasPrice, address gasToken, address refundReceiver, bytes signatures)`

// regex pattern to remove the word before a comma or closing bracket
export const removeArgsFromMethod = (method: string) => {
return method.replace(/\s\w+(,|\))/g, (_, commaOrBracket) => commaOrBracket);
};

// remove the argument names and remove any spaces
const preparedMethod = removeArgsFromMethod(method).replace(/s/g, "")

// keccak hash of the method
const keccakHashOfMethod = keccak(Buffer.from(preparedMethod))

// first 4 bytes
const methodId = keccakHashOfMethod.slice(0,4).toString("hex") // 6a761202

Note: If you don’t have the source code of the smart contract then you won’t be able to generate the hash to check and it will be more difficult calculate the exact parameter types.

The output from that Javascript is 6a761202 which matches the first 4 bytes (8 hex characters) of the data payload. Great. We’ve identified that the method being called is in fact “execTransaction” with the 10 parameters.

Now, if you go back to Etherscan and click on the `Decode Input Data button you can see how Etherscan has taken the data payload and broken it up into the 10 parameters including the types. Again, how have they done this?

Decoding an Ethereum Transaction (4)

Get a handle on parameters

Our next job is to break up all the parameters that have been passed to the execTransaction and see what they are.

Before we do that a quick note on bytes to hex conversion. Very simply 1 byte of data = 2 characters of Hex. So, whenever you see a string of hex characters, you can divide the number of characters by 2 to get the size in bytes. Any "0x" at the beginning is ignored in the actual calculation.

Now, on to decoding the method parameters.

Let's have a look at the data again but this time we split it into 32 byte (64 Hex characters) strings. Why? Because ethereum uses 32 bytes blocks and nearly all primitive types are 32 bytes. There are a couple of exceptions though, such as the "bytes" type which we'll see later.

So, once we strip off the methodId (0x6a761202) and then break the remaining data into 32 bytes (or 64 characters) chunks we can start to see something more interesting.

MethodID: 0x6a761202

Decoding an Ethereum Transaction (5)

These are the values of the input parameters of the execTransaction function. It's not quite straightforward so let's work through each 32 byte value in turn. Column 3 of the table above shows where each of the values maps to the parameters of the execTransaction function.

For example, the first parameter (the `"to" parameter):

000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7

is of type address.

Ethereum addresses are 20 byte values so it has been zero-padded to fit into a 32 byte value. To get the actual address, we just need to extract the last 20 bytes and prefix 0x.

In this case it becomes:

`0xdac17f958d2ee523a2206206994597c13d831ec7

(Hmm...this is the Tether contract)

In fact, removing the zero padding is how to decode most of the simple parameters like uint8, unit256, etc...

The next parameter is the “value”. This, we assume, is how much value needs to be transferred from the Gnosis safe. Its value is zero. (This is the value of Ether remember)

The next parameter is interesting, it's of types bytes. bytes is a variable-length so how long is it? The value is:

0000000000000000000000000000000000000000000000000000000000000140

This value represents the offset in hex of bytes from the `methodId` of where the actual data begins. Think of it as a pointer, redirecting you to where you should look for this information. So we need to convert 0x140 bytes into hex characters.

0x140 (bytes in hex) = 320 bytes in decimal

320 bytes in decimal x 2 = 640 characters in hex

so the data begins 320 bytes from the start, and thus it is 640 hex characters along, or 10 rows along. In JavaScript, it will look as follows:

const numberOfBytesInHex = 140

const numberOfHexCharacters = parseInt(numberOfBytesInHex, 16) * 2; // = 640

Going to the 10th row the first 64 characters represent the length of the data (this requires the same conversion done above).

0000000000000000000000000000000000000000000000000000000000000044

is decoded into a length of 136 hex characters.

So finally on the 11th row the data begins and ends after 136 characters. Which means our data field is the following:

a9059cbb000000000000000000000000ba339b8271afea713008314487dd98f8d941720f
00000000000000000000000000000000000000000000000000000002540be400

So in summary, for a bytes field, the first 32 bytes is a pointer to where the data starts, once there, the next 32 bytes tells you how long the data is. Then you read that many bytes. If the data doesn’t end in a 32 byte block, it will be padded so whatever is next, will start in the next 32 byte block.

Looking at the value again:

a9059cbb000000000000000000000000ba339b8271afea713008314487dd98f8d941720f
00000000000000000000000000000000000000000000000000000002540be400

It looks a little familiar... It has a 4 byte methodId and then 2 x 32 byte chunks (2 lots of 64 hex characters). It’s another method call!

In fact, we can recognise immediately that this transaction is in itself an ERC20 transfer transaction as it has the famous a9059cbb method signature.

(Tip: go to https://emn178.github.io/online-tools/keccak_256.html and type in transfer(address,uint256) and you will see the first 8 characters are in fact a9059cbb`)

So, we can see that this Gnosis transaction is to call another contract of address 0xdAC17F958D2ee523a2206206994597C13D831ec7 with the “data” of:

a9059cbb000000000000000000000000ba339b8271afea713008314487dd98f8d941720f
00000000000000000000000000000000000000000000000000000002540be400

Which is an ERC20 “transfer” call. We could then use what we learnt above to recursively calculate the details of this data call by extracting the methodId bytes and breaking up the data and looking at the parameters.

Take a look and see if you can work out the recipient and value of tokens sent to the Tether contract.

Conclusion

There was a lot of information here and we didn't go through every single parameter, but hopefully, you can see it was very useful to be able to break the transaction data down and by working through a data payload methodically you can always understand what's happening.

We saw how for a Gnosis contract transaction we could break it down into the constituent parts and we saw the methodId and the parameter types and how to deconstruct them into useful arguments.

We also saw how that, in this case, there was effectively a transaction within a transaction. The Gnosis safe transaction was just wrapping an ERC20 transfer. This was effectively moving USDT tokens from the Gnosis safe to somewhere else.

In our next blog, we’ll see how we can take our understanding of Ethereum transactions to break down a Defi contract call to apply rules to decide if we want to sign / send the transaction. This is something that Trustology uses in its new Defi firewall to protect customers from signing / sending transactions that don’t meet certain criteria. The DeFi Firewall is the first in a suite of services Trustology aims to launch for institutional users looking to support new tokens in DeFi or explore yield bearing opportunities. Notifications and flows are next on our list to complement our current firewall solution. Currently, we are the only custodial wallet to integrate with MetaMask, which lets institutions tap DeFi innovation from the security of an insured custodial wallet platform.

To access the code demonstrated in this blog and more, please click here.

To enquire about our DeFi services chat to us.

About Trustology

Backed by ConsenSys and Two Sigma Ventures, and founded in 2017, Trustology was established on the premise of enabling greater freedom to transact in a fair and efficient manner. By bringing the world of blockchain technology and cryptoassets to new market participants, we believe this will help make this happen.

That’s why we built TrustVault — a fast, user-friendly, insured and highly-secure custodial wallet service for institutions and individuals designed to address the security and ownership shortcomings of existing solutions today. Learn more about us at trustology.io.

Recommended reading:

If you want to know more about Ethereum transactions there are some good articles, you start with:

As an expert in Ethereum and smart contracts, I can provide a comprehensive breakdown of the concepts mentioned in the article. The author discusses Ethereum transactions, focusing on a Gnosis Safe contract and a transaction generated by the Gnosis Safe Dapp. Here's an in-depth analysis:

  1. Smart Contracts:

    • Definition: Self-executing contracts with the terms of the agreement directly written into code.
    • Importance: Essential for various activities in Ethereum, including token transfers, NFT trading, and DeFi interactions.
  2. Gnosis Safe:

    • Definition: A popular wallet contract implemented by Gnosis, designed for multi-user organizations.
    • Features: Defines a list of accounts and a required threshold of signatures for transaction authorization.
    • Use Case: Offers a secure way for users to control and manage funds collectively.
  3. Ethereum Transactions:

    • Definition: Actions performed on the Ethereum blockchain, involving the execution of smart contracts.
    • Components: Transactions include data fields that determine the method to be called on the target contract.
  4. Etherscan:

    • Definition: A blockchain explorer that provides detailed information about transactions, blocks, and addresses on the Ethereum network.
    • Usage: Allows users to inspect transaction details, including input data and method calls.
  5. Method Calls and Data Field:

    • Definition: In Ethereum transactions, the data field contains the encoded information about the method to be executed on the target contract.
    • Example: The article explores a method called execTransaction in the Gnosis Safe contract.
  6. MethodID:

    • Definition: The first 4 bytes (8 hex characters) of the input data, representing the method name and argument types.
    • Purpose: Used to identify and differentiate between different methods in a smart contract.
  7. Parameter Types and Values:

    • Definition: Information about the types and values of parameters passed to a smart contract method.
    • Importance: Understanding parameter types and values is crucial for interacting with and analyzing smart contracts.
  8. Bytes and Hex Conversion:

    • Definition: Explanation of how bytes in data fields are represented in hexadecimal format.
    • Significance: Helps in decoding and understanding the structure of complex data in Ethereum transactions.
  9. Decoding Parameters:

    • Definition: The process of breaking down the parameters passed to a method in a smart contract.
    • Purpose: Essential for understanding and interpreting the intended actions of a transaction.
  10. ERC20 Transfer Transaction:

    • Definition: A specific type of Ethereum transaction for transferring ERC20 tokens.
    • Identification: Recognized by the method signature, such as a9059cbb, representing an ERC20 transfer.
  11. Recursive Data Analysis:

    • Definition: The process of analyzing nested or embedded transactions within a transaction.
    • Application: Used to understand complex transactions, such as those involving multiple smart contracts.
  12. Trustology’s Custodial Wallet Platform:

    • Definition: A custodial wallet service providing a secure way to interact with smart contracts.
    • Features: Offers enhanced security, including offline signing, and is introduced as a foundation for subsequent DeFi services.

In conclusion, the article serves as a practical guide for understanding Ethereum transactions, decoding method calls, and analyzing data fields. The expert guidance provided lays the groundwork for exploring advanced topics like Trustology’s DeFi services in subsequent posts.

Decoding an Ethereum Transaction (2024)

FAQs

How to decode Ethereum data? ›

2 Answers
  1. You need the smart contract ABI files to decode the transaction.
  2. You need a framework and library that can take ABI files and your Data payload and decode it for you.
  3. One library, for example, is Web.py and its decode_function_input function but there are others.
Sep 22, 2023

How do you read an Ethereum transaction? ›

Steps to Track an Ethereum Transaction
  1. Step 1: Obtain the Transaction Hash. To begin tracking your Ethereum transaction, you will need to obtain the transaction hash. ...
  2. Step 2: Visit an Ethereum Blockchain Explorer. ...
  3. Step 3: Enter the Transaction Hash. ...
  4. Step 4: Review the Transaction Details.
Jun 9, 2023

How are Ethereum transactions verified? ›

The account sending the transaction verifies it by signing which creates a transaction hash. The transaction is broadcasted across the network using an Ethereum node. The transaction execution is idle until the transaction is mined and added to the block or replaced/canceled.

What do you understand by Ethereum transaction? ›

Before diving into the details of checking Ethereum transactions, let's first understand what they are. An Ethereum transaction represents the transfer of value or information from one Ethereum address to another. Each transaction is recorded on the Ethereum blockchain, making it transparent and immutable.

How do I read Etherscan transaction details? ›

To check wallet balances and history on Etherscan, go to Etherscan.io, navigate to the search bar, paste the wallet address or ENS domain, and press Enter. On the next page, you can see the ETH balance, total token value, and transaction history.

What code is Ethereum written in? ›

Solidity is a programming language for implementing smart contracts on various blockchain platforms, most notably, Ethereum. Solidity is licensed under GNU General Public License v3. 0.

How do I read a blockchain transaction? ›

Block explorers are web-based tools that allow users to navigate through the blockchain's transaction history. For public blockchains, these explorers provide a user-friendly interface to access information like transaction details, wallet balances, and block timestamps.

Can you trace Ethereum transaction? ›

A blockchain explorer is a tool that allows you to view all the details of a particular transaction on the Ethereum blockchain. By entering the transaction hash into a blockchain explorer, you can view the status of the transaction, the amount of ETH involved, and the addresses of the sender and receiver.

What is an example of an ETH transaction? ›

An Ethereum transaction refers to an action initiated by an EOA (externally-owned account), in other words an account managed by a human, not a contract. For example, if Bob sends Alice 1 ETH , Bob's account must be debited and Alice's must be deducted. This state-changing action takes place within a transaction.

How do crypto miners verify transactions? ›

Validated transactions are grouped into a block to be “mined.” The PoW algorithm (SHA-256) generates a 64-character hash for the block. Using computational power, miners compete to generate a target hash below the block hash, solving the problem. The winning miner verifies and adds the block and receives the reward.

What are different types of Ethereum transactions? ›

Broadly speaking there are three types transactions supported on Ethereum:
  • Transfer of Ether from one party to another.
  • Creation of a smart contract.
  • Transacting with a smart contract.

How do miners verify transactions? ›

They will validate that the transaction exists by solving complex mathematical equations. Once the majority of miners agree on the solving of the math equations, the transaction is entered in a new block. The information contained in this block is synchronized with all the nodes of the network.

How do you explain Ethereum to a beginner? ›

Ethereum is an open-source software platform that developers can use to create cryptocurrencies and other digital applications. Ethereum is also the name used to describe the cryptocurrency Ether.

What is the difference between a message and a transaction in Ethereum? ›

Transactions are recorded into each block of the blockchain. This means that a transaction represents either a Message or a new contract. This means that a Message is the data and amount of Ether that is passed between two accounts. A message is created by contracts interacting with each other, or by a transaction.

How does Ethereum work for dummies? ›

The Bottom Line. Ethereum is a decentralized blockchain platform. It enables developers to build and deploy smart contracts. Ethereum utilizes its native cryptocurrency, Ether (ETH), for transactions and incentivizing network participants to secure the network through a proof of stake (PoS) consensus mechanism.

Can you code Ethereum? ›

Ethereum allows developers to write applications that run on the blockchain with smart contracts, which encapsulate all of the business logic of these applications. They enable us to read and write data to the blockchain, as well as execute code.

Can you trace Ethereum? ›

It is possible to trace Ethereum transactions, but the level of detail that can be obtained will depend on the specific circ*mstances. Ethereum transactions are stored on a public blockchain, so anyone can see the transactions that have been made.

What data is in an Ethereum block? ›

In addition to transactions, blocks in Ethereum also contain other types of data such as smart contract code and the results of that code being executed. Each block also includes a timestamp and information about the miner who mined the block.

How to read Ethereum smart contract? ›

Deciphering a smart contract

An individual can generally decipher the substance of the transaction by looking at the information in a particular transaction hash. A good place to start when looking at a smart contract transaction is the transactions overview page.

Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 6327

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.