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';

// 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

Top-Level Methods

Function Methods

Event Methods

Error Methods

Constructor Methods

Item Utilities

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.
  • 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