Skip to main content

Try it Live

Run ABI examples in the interactive playground
Conceptual Guide - For API reference and method documentation, see ABI API.
The Application Binary Interface (ABI) is Ethereum’s standard for encoding function calls, return values, events, and errors when interacting with smart contracts. This guide teaches ABI fundamentals using Tevm.

What is ABI?

ABI defines how to encode and decode data for contract interactions:
  • Function calls - Encode parameters into calldata
  • Function returns - Decode return values from execution
  • Events - Encode/decode log entries
  • Errors - Encode custom error data
Without ABI, contracts couldn’t communicate. It’s the protocol that lets you call transfer(address,uint256) or parse Transfer events.

Function Selectors

Every function call starts with a 4-byte selector derived from the function signature:

Why Selectors Matter

The EVM uses selectors to route function calls. Calldata format:
Example calling transfer(0x742d..., 1000):

Encoding Rules

ABI encoding packs data into 32-byte words with specific alignment rules.

Fixed-Size Types

Types with known size encode directly into 32-byte slots:

Dynamic Types

Dynamic types (string, bytes, arrays) use offset-based encoding:
Structure:
  1. Offset (32 bytes) - Where data starts relative to parameter start
  2. Length (32 bytes) - Number of bytes in data
  3. Data (padded to 32-byte multiple) - Actual content

Complete Function Call Example

Encode complete calldata for transfer(address,uint256):
Use this calldata when making contract calls via RPC or transaction.

Decoding Return Values

Decode function return data after execution:

Multiple Parameters

Mixed fixed and dynamic types require offset tracking:

Encoding Layout

Constructor Encoding

Encode constructor parameters for contract deployment:

Event Encoding

Events split data into indexed topics (for filtering) and non-indexed data (for details).

Event Structure

Encoding Event Topics

Topic 0 is always the event signature hash:

Decoding Event Logs

Parse log data and topics back to values:

Why Indexed Parameters?

Indexed parameters become topics, enabling efficient filtering:
Limit: Maximum 3 indexed parameters per event (topics 1-3).

Tuple Encoding

Tuples (structs) encode as grouped parameters:

Array of Tuples

Visual Encoding Example

Encoding register(address,string,uint256) with values:
  • address: 0x742d35Cc6634C0532925a3b844Bc9e7595f51e3e
  • string: "alice"
  • uint256: 25

Common Use Cases

Calling Contract Functions

Parsing Event Logs

Handling Contract Errors

Type Safety

Tevm provides full TypeScript type inference:

Error Handling

ABI operations can fail with specific error types:

Resources

Next Steps

  • Overview - Type definitions and API reference