book-openAsyncPublicContract

Async contract wrapper with transparent read-only access

Asynchronous contract wrapper for read-only transparent access to Seismic contracts.

Overview

AsyncPublicContract is the async version of PublicContract, providing non-blocking read-only access to contract state. It exposes only the .tread namespace for standard async eth_call operations. All methods return coroutines that must be awaited. Use this class in async applications when you only need to read public contract data.

Definition

class AsyncPublicContract:
    def __init__(
        self,
        w3: AsyncWeb3,
        address: ChecksumAddress,
        abi: list[dict[str, Any]],
    ) -> None:
        ...

Constructor Parameters

Parameter
Type
Required
Description

w3

AsyncWeb3

Yes

Asynchronous AsyncWeb3 instance connected to RPC endpoint

address

ChecksumAddress

Yes

Contract address (checksummed Ethereum address)

abi

list[dict[str, Any]]

Yes

Contract ABI (list of function entries)

Namespace

.tread - Transparent Read

Executes standard async eth_call with unencrypted calldata. This is the only namespace available on AsyncPublicContract.

Returns: Coroutine[HexBytes] (raw result bytes)

Optional Parameters: None (pass positional arguments only)

Examples

Basic Read Operations

Concurrent Reads

Batch Balance Queries

Decoding Results

Complex Return Types

Array Results

Instantiation via Client

Pagination with Concurrency

Monitoring Contract State

Error Handling

With Timeout

Context Manager Pattern

Multi-Contract Queries

Retry Logic

Notes

  • All methods return coroutines: Must use await with every .tread call

  • Concurrent reads: Use asyncio.gather() for parallel queries

  • Read-only: No write operations available

  • No encryption required: Does not use EncryptionState or private keys

  • No authentication: Standard unsigned async eth_call operations

  • View functions only: Can only call view or pure functions

  • Gas not consumed: eth_call is free (doesn't create transactions)

  • Connection pooling: AsyncWeb3 efficiently manages connection reuse

  • Error handling: Use try/except around await calls for RPC errors

  • Timeout support: Use asyncio.wait_for() to limit operation duration

Performance Tips

  • Batch related queries: Use asyncio.gather() to execute multiple reads in parallel

  • Avoid sequential waits: Don't await in loops; collect coroutines and use gather()

  • Connection limits: Be aware of RPC provider's concurrent request limits

  • Retry logic: Implement retries for transient network failures

  • Caching: Cache frequently accessed immutable data (like token decimals)

Use Cases

High-Throughput Queries

Perfect for:

  • Scanning contract events and state concurrently

  • Building real-time dashboards

  • Aggregating data from multiple contracts

  • Monitoring blockchain state

  • Batch processing of contract reads

  • Websocket-based applications

When to Use AsyncShieldedContract Instead

Use AsyncShieldedContract when you need:

  • Write operations (state changes)

  • Encrypted reads of shielded state

  • Authenticated operations

  • Transaction signing

  • Access to private/shielded data

See Also

Last updated