Options
All
  • Public
  • Public/Protected
  • All
Menu

Program provides the IDL deserialized client representation of an Anchor program.

This API is the one stop shop for all things related to communicating with on-chain programs. Among other things, one can send transactions, fetch deserialized accounts, decode instruction data, subscribe to account changes, and listen to events.

In addition to field accessors and methods, the object provides a set of dynamically generated properties, also known as namespaces, that map one-to-one to program methods and accounts. These namespaces generally can be used as follows:

Usage

program.<namespace>.<program-specific-method>

API specifics are namespace dependent. The examples used in the documentation below will refer to the two counter examples found here.

Type parameters

Hierarchy

  • Program

Index

Constructors

constructor

Properties

Readonly account

account: AccountNamespace<IDL>

The namespace provides handles to an AccountClient object for each account in the program.

Usage

program.account.<account-client>

Example

To fetch a Counter account from the above example,

const counter = await program.account.counter.fetch(address);

For the full API, see the AccountClient reference.

Readonly instruction

instruction: InstructionNamespace<IDL, IDL["instructions"][number]>

The namespace provides functions to build TransactionInstruction objects for each method of a program.

Usage

program.instruction.<method>(...args, ctx);

Parameters

  1. args - The positional arguments for the program. The type and number of these arguments depend on the program being used.
  2. ctx - Context non-argument parameters to pass to the method. Always the last parameter in the method call.

Example

To create an instruction for the increment method above,

const tx = await program.instruction.increment({
accounts: {
counter,
},
});
deprecated

Readonly methods

methods: MethodsNamespace<IDL, AllInstructions<IDL>>

The namespace provides a builder API for all APIs on the program. This is an alternative to using namespace the other namespaces..

Readonly rpc

rpc: RpcNamespace<IDL, AllInstructions<IDL>>

Async methods to send signed transactions to non-state methods on the program, returning a TransactionSignature.

Usage

rpc.<method>(...args, ctx);

Parameters

  1. args - The positional arguments for the program. The type and number of these arguments depend on the program being used.
  2. ctx - Context non-argument parameters to pass to the method. Always the last parameter in the method call.

Example

To send a transaction invoking the increment method above,

const txSignature = await program.rpc.increment({
accounts: {
counter,
authority,
},
});
deprecated

Readonly simulate

simulate: SimulateNamespace<IDL, AllInstructions<IDL>>

The namespace provides functions to simulate transactions for each method of a program, returning a list of deserialized events and raw program logs.

One can use this to read data calculated from a program on chain, by emitting an event in the program and reading the emitted event client side via the simulate namespace.

simulate

program.simulate.<method>(...args, ctx);

Parameters

  1. args - The positional arguments for the program. The type and number of these arguments depend on the program being used.
  2. ctx - Context non-argument parameters to pass to the method. Always the last parameter in the method call.

Example

To simulate the increment method above,

const events = await program.simulate.increment({
accounts: {
counter,
},
});
deprecated

Optional Readonly state

state?: StateClient<IDL>

A client for the program state. Similar to the base Program client, one can use this to send transactions and read accounts for the state abstraction.

Readonly transaction

transaction: TransactionNamespace<IDL, AllInstructions<IDL>>

The namespace provides functions to build Transaction objects for each method of a program.

Usage

program.transaction.<method>(...args, ctx);

Parameters

  1. args - The positional arguments for the program. The type and number of these arguments depend on the program being used.
  2. ctx - Context non-argument parameters to pass to the method. Always the last parameter in the method call.

Example

To create an instruction for the increment method above,

const tx = await program.transaction.increment({
accounts: {
counter,
},
});
deprecated

Optional Readonly views

views?: ViewNamespace<IDL, AllInstructions<IDL>>

Accessors

coder

idl

  • get idl(): IDL

programId

provider

Methods

addEventListener

  • addEventListener(eventName: string, callback: (event: any, slot: number) => void): number
  • Invokes the given callback every time the given event is emitted.

    Parameters

    • eventName: string

      The PascalCase name of the event, provided by the IDL.

    • callback: (event: any, slot: number) => void

      The function to invoke whenever the event is emitted from program logs.

        • (event: any, slot: number): void
        • Parameters

          • event: any
          • slot: number

          Returns void

    Returns number

removeEventListener

  • removeEventListener(listener: number): Promise<void>
  • Unsubscribes from the given eventName.

    Parameters

    • listener: number

    Returns Promise<void>

Static at

  • Generates a Program client by fetching the IDL from the network.

    In order to use this method, an IDL must have been previously initialized via the anchor CLI's anchor idl init command.

    Type parameters

    Parameters

    • address: Address
    • Optional provider: Provider

      The network and wallet context.

    Returns Promise<Program<IDL>>

Static fetchIdl

  • Fetches an idl from the blockchain.

    In order to use this method, an IDL must have been previously initialized via the anchor CLI's anchor idl init command.

    Type parameters

    Parameters

    • address: Address
    • Optional provider: Provider

      The network and wallet context.

    Returns Promise<null | IDL>

Generated using TypeDoc