Skip to main content

Contract

The Contract module provides a typed abstraction for interacting with deployed Ethereum smart contracts from Swift. It combines ABI encoding/decoding with provider calls using Swift’s modern async/await and AsyncSequence patterns.

Overview

Contract instances provide four main interfaces:

Quick Start

API Reference

Contract Initializer

Creates a typed contract instance. Parameters:
  • address - Contract address (hex string)
  • abi - Contract ABI as array of AbiItem
  • provider - EIP-1193 compatible provider
Returns: Contract instance with read, write, estimateGas, and events interfaces.

Read Methods

Execute view/pure functions with async/await:

Write Methods

Send state-changing transactions:

Gas Estimation

Estimate gas before sending:

Events (AsyncSequence)

Stream events using Swift’s AsyncSequence:

Manual Encoding

Access the ABI directly for manual encoding:

Error Handling

Handle errors with Swift’s throwing functions:

Type Safety

Swift provides compile-time type checking:

Concurrency

Use Swift concurrency patterns: