Skip to main content

Try it Live

Run ABI examples in the interactive playground

Abi

Complete Application Binary Interface (ABI) encoding and decoding with type safety for Ethereum smart contracts.
Start with ABI Fundamentals to learn about function selectors, encoding rules, and event structure.

Overview

Branded array of ABI items (functions, events, errors, constructors). Zero-overhead design supports both tree-shakeable methods and class instances, following the same pattern as Address.

Quick Start

import * as Abi from 'tevm/Abi';
import * as S from 'effect/Schema';
import * as AbiSchema from 'voltaire-effect/primitives/Abi';

// Define function ABI item
const transferAbi = {
  type: 'function',
  name: 'transfer',
  inputs: [
    { type: 'address', name: 'to' },
    { type: 'uint256', name: 'amount' }
  ],
  outputs: [{ type: 'bool' }],
  stateMutability: 'nonpayable'
} as const;

// Get function selector (4 bytes)
const selector = Abi.Function.getSelector(transferAbi);
// 0xa9059cbb

// Encode parameters
const encoded = Abi.Function.encodeParams(transferAbi, [
  '0x742d35Cc6634C0532925a3b844Bc9e7595f51e3e',
  1000000000000000000n
]);

Types

// Main ABI type: array of ABI items
export type Abi = ReadonlyArray<
  Function | Event | BrandedError | Constructor | Fallback | Receive
>

// Example ABI
const erc20Abi: Abi = [
  {
    type: "function",
    name: "transfer",
    stateMutability: "nonpayable",
    inputs: [
      { type: "address", name: "to" },
      { type: "uint256", name: "amount" }
    ],
    outputs: [{ type: "bool" }]
  },
  {
    type: "event",
    name: "Transfer",
    inputs: [
      { type: "address", name: "from", indexed: true },
      { type: "address", name: "to", indexed: true },
      { type: "uint256", name: "value" }
    ]
  }
]
Source: Abi.ts:49-51

API Documentation

Core Concepts

Fundamentals

Learn ABI encoding structure, selectors, and data layout

ABI Types

Function, Event, Error, Constructor type definitions

Encoding Guide

ABI encoding and decoding mechanics

Selectors & Signatures

Function selectors and event topic hashes

Usage Patterns

Common patterns: contract calls, log parsing

Top-Level Methods

format

Format ABI item to human-readable signature

formatWithArgs

Format with concrete argument values

getItem

Find ABI item by name and type

encode

Generic ABI encoding for any item type

decode

Generic ABI decoding for any item type

decodeData

Decode without selector prefix

parseLogs

Parse multiple event logs using full ABI

Function Methods

getSelector

Get function selector (4 bytes)

getSignature

Get human-readable function signature

encodeParams

Encode function input parameters

decodeParams

Decode function input parameters

encodeResult

Encode function return values

decodeResult

Decode function return values

GetSelector

Factory for custom keccak256

Event Methods

getSelector

Get event selector (32 bytes, topic0)

getSignature

Get human-readable event signature

encodeTopics

Encode indexed parameters to topics

decodeLog

Decode event log data and topics

GetSelector

Factory for custom keccak256

EncodeTopics

Factory for custom keccak256

Error Methods

getSelector

Get error selector (4 bytes)

getSignature

Get human-readable error signature

encodeParams

Encode error parameters

decodeParams

Decode error parameters

GetSelector

Factory for custom keccak256

Constructor Methods

encodeParams

Encode constructor parameters for deployment

decodeParams

Decode constructor parameters from bytecode

Item Utilities

isFunction

Type guard: check if item is function

isEvent

Type guard: check if item is event

isError

Type guard: check if item is error

isConstructor

Type guard: check if item is constructor

isFallback

Type guard: check if item is fallback

isReceive

Type guard: check if item is receive

format

Format ABI item to signature string

formatWithArgs

Format with concrete argument values

getItem

Get specific item from ABI by name/type

ABI JSON Format

Contract ABIs are typically JSON arrays describing the contract interface:
const abi = [
  {
    "type": "function",
    "name": "transfer",
    "stateMutability": "nonpayable",
    "inputs": [
      { "type": "address", "name": "to" },
      { "type": "uint256", "name": "amount" }
    ],
    "outputs": [{ "type": "bool" }]
  },
  {
    "type": "event",
    "name": "Transfer",
    "inputs": [
      { "type": "address", "name": "from", "indexed": true },
      { "type": "address", "name": "to", "indexed": true },
      { "type": "uint256", "name": "value", "indexed": false }
    ]
  },
  {
    "type": "error",
    "name": "InsufficientBalance",
    "inputs": [
      { "type": "uint256", "name": "balance" },
      { "type": "uint256", "name": "required" }
    ]
  }
]

Error Types

import {
  AbiEncodingError,
  AbiDecodingError,
  AbiParameterMismatchError,
  AbiItemNotFoundError,
  AbiInvalidSelectorError
} from '@tevm/primitives/Abi'

try {
  const encoded = encodeParameters(params, values)
} catch (error) {
  if (error instanceof AbiEncodingError) {
    // Invalid encoding (out of range, wrong type, etc.)
  } else if (error instanceof AbiParameterMismatchError) {
    // Parameter count mismatch
  }
}
Source: Errors.ts:1-35

Tree-Shaking

Import only what you need for optimal bundle size:
// Import specific namespace
import { Function } from '@tevm/primitives/Abi'

// Or import individual functions
import { encodeParameters, decodeParameters } from '@tevm/primitives/Abi'

Sub-Namespaces

The Abi module is organized into specialized sub-namespaces:
  • Function - Function encoding/decoding and selectors
  • Event - Event log encoding/decoding and topics
  • Error - Custom error encoding/decoding
  • Constructor - Constructor parameter encoding
  • Wasm - WASM-accelerated implementations
Each namespace provides focused functionality with tree-shakeable exports.
  • Abi (Effect) - Effect.ts integration with Schema validation
  • Keccak256 - Keccak256 hashing used for selectors and signatures
  • Address - 20-byte Ethereum addresses used in ABI encoding
  • Uint - Unsigned integer types used in ABI encoding
  • Bytes - Fixed and dynamic byte arrays in ABI encoding

Specification References