Skip to main content

Overview

The Program class is the primary interface for interacting with Anchor programs. It provides a type-safe, IDL-driven API for sending transactions, fetching accounts, and listening to events.

Constructor

(idl, provider?, coder?, getCustomResolver?) => Program
Creates a new Program instance.

Static Methods

async (address, provider?) => Promise<Program>
Fetches the IDL from the blockchain and creates a Program instance.
async (address, provider?) => Promise<Idl | null>
Fetches the IDL from the blockchain without creating a Program instance.

Properties

PublicKey
The on-chain address of the program.
IDL
The IDL in camelCase format for TypeScript compatibility.
Idl
The original IDL without camelCase conversion (snake_case from Rust).
Provider
The wallet and network provider used by this program.
Coder
The coder used for serializing and deserializing data.

Namespaces

methods

MethodsNamespace<IDL>
The recommended builder API for constructing and sending transactions.
The methods namespace provides:
  • .accounts() - Set instruction accounts
  • .signers() - Add transaction signers
  • .remainingAccounts() - Add extra accounts
  • .preInstructions() - Add instructions before
  • .postInstructions() - Add instructions after
  • .rpc() - Send and confirm transaction
  • .instruction() - Build TransactionInstruction
  • .transaction() - Build Transaction
  • .simulate() - Simulate transaction
  • .view() - Call read-only instruction

account

AccountNamespace<IDL>
Provides access to account clients for fetching and subscribing to account data.
See AccountClient for full details.

rpc (deprecated)

RpcNamespace<IDL>
Legacy API for sending signed transactions. Use methods instead.
The rpc namespace is deprecated. Use program.methods.<method>(...args).rpc() instead.

transaction (deprecated)

TransactionNamespace<IDL>
Legacy API for building Transaction objects. Use methods instead.
The transaction namespace is deprecated. Use program.methods.<method>(...args).transaction() instead.

simulate (deprecated)

SimulateNamespace<IDL>
Legacy API for simulating transactions. Use methods instead.
The simulate namespace is deprecated. Use program.methods.<method>(...args).simulate() instead.

instruction (deprecated)

InstructionNamespace<IDL>
Legacy API for building TransactionInstruction objects. Use methods instead.
The instruction namespace is deprecated. Use program.methods.<method>(...args).instruction() instead.

Event Handling

(eventName, callback, commitment?) => number
Subscribe to program events emitted in transaction logs.
async (listener) => Promise<void>
Unsubscribe from a program event.

Usage Examples

Basic Initialization

Sending Transactions

Fetching Accounts

Building Complex Transactions

Listening to Events