TypeScript
Learn how to use Anchor's TypeScript client library to interact with Solana programs
Anchor provides a Typescript client library (@coral-xyz/anchor) that simplifies the process of interacting with Solana programs from the client in JavaScript or TypeScript.
The @coral-xyz/anchor
library is only compatible with the legacy version
(v1) of @solana/web3.js
and @solana/spl-token
. It is not compatible with
the new version (v2) of @solana/web3.js
.
Client Program
To interact with an Anchor program using @coral-xyz/anchor
, you'll need to
create a
Program
instance using the program's IDL file.
Creating an instance of the Program
requires the program's IDL and an
AnchorProvider
.
An AnchorProvider
is an abstraction that combines two things:
Connection
- the connection to a Solana cluster (i.e. localhost, devnet, mainnet)Wallet
- (optional) a default wallet used to pay for and sign transactions
When integrating with a frontend using the
Solana wallet adapter, you'll need
to set up the AnchorProvider
and Program
.
In the code snippet above:
idl.json
is the IDL file generated by Anchor, found at/target/idl/<program-name>.json
in an Anchor project.idlType.ts
is the IDL type (for use with TypeScript), found at/target/types/<program-name>.ts
in an Anchor project.
Alternatively, you can create an Program
instance using only the IDL and the
Connection
to a Solana cluster. This means there is no default Wallet
, but
allows you to use the Program
to fetch accounts or build instructions without
a connected wallet.
Invoke Instructions
Once the Program
is set up using a program's IDL file, you can use the Anchor
MethodsBuilder
to:
- Build individual instructions
- Build transactions
- Build and send transactions
The basic format looks like the following:
program.methods
- This is the builder API for creating instruction calls from
the program's IDL
Anchor provides multiple methods for building program instructions:
The
rpc()
method
sends a signed transaction
with the specified instruction and returns a TransactionSignature
.
When using .rpc
, the Wallet
from the Provider
is automatically included as
a signer.
Fetch Accounts
The Program
client simplifies the process of fetching and deserializing
accounts created by your Anchor program.
Use program.account
followed by the name of the account type defined in the
IDL. Anchor provides multiple methods for fetching accounts.
Use
all()
to fetch all existing accounts for a specific account type.
Example
The example below demonstrates how to use @coral-xyz/anchor
to interact with a
simple Anchor program. The program has two instructions:
initialize
– Creates and initializes a counter account to store a valueincrement
– Increments the value stored on the counter account
Below is an example folder structure for a TypeScript client that interacts with the Anchor program:
The /idl
directory in the example includes two files:
example.json
: The IDL file for the programexample.ts
: A TypeScript type definition file generated for the IDL
The tabs below include the example.json
and example.ts
files as a reference
of what these files look like.
When you run anchor build
in an Anchor project, the Anchor CLI automatically
generates:
-
The IDL file (
.json
) in thetarget/idl
folder (ex.target/idl/example.json
) -
The TypeScript type definitions (
.ts
) in thetarget/types
folder (ex.target/types/example.ts
)
The example.ts
file below includes the script to interact with the program.