@anchor-lang/core) that simplifies the process of interacting with Solana programs from JavaScript or TypeScript applications.
The
@anchor-lang/core 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.Installation
Install the Anchor client library along with Solana web3.js:Core Concepts
The Anchor TypeScript client is built around three main concepts:Program
The main interface for interacting with on-chain programs
Provider
Combines connection and wallet for signing and sending transactions
Workspace
Auto-discovers programs in Anchor projects (Node.js only)
Creating a Program Instance
To interact with an Anchor program, you need to create aProgram instance using the program’s IDL file.
Constructor Signature
Frontend/React Example
When integrating with a frontend using the Solana wallet adapter:Without Wallet
You can create aProgram instance without a wallet for read-only operations:
Fetch IDL from Chain
If an IDL has been deployed to the blockchain usinganchor idl init:
AnchorProvider
TheAnchorProvider combines a Solana connection and wallet to handle transaction signing and sending.
API
Example: Node.js Provider
Workspace (Node.js Only)
In Anchor projects, you can use theworkspace object to automatically discover and load programs:
- Reads IDL files from
target/idl/ - Parses
Anchor.tomlfor program configuration - Creates typed
Programinstances
Invoking Instructions
Theprogram.methods namespace provides a builder API for creating and sending transactions.
Builder Pattern
1
Start with .methods
Call the instruction name with its arguments
2
Provide accounts with .accounts()
Pass required accounts (PDAs and common accounts are often auto-resolved)
3
Add signers with .signers() (optional)
Include additional signers beyond the wallet
4
Execute
Choose how to execute:
.rpc(), .transaction(), or .instruction()Execution Methods
- .rpc()
- .transaction()
- .instruction()
- .simulate()
Builds, signs, and sends the transaction in one call. Returns the transaction signature.Best for: Simple, single-instruction transactions
Additional Options
The methods builder supports additional configuration:Fetching Accounts
Theprogram.account namespace provides methods to fetch and deserialize program accounts.
Fetch Single Account
Fetch Multiple Accounts
Fetch All Accounts
Fetch with Filters
Usememcmp (memory compare) to filter accounts. Remember: the first 8 bytes are the account discriminator.
Event Listeners
Subscribe to program events emitted via logs:Program Properties
TheProgram instance exposes useful properties:
Complete Example
Here’s a complete example demonstrating common operations:example.ts
TypeScript Type Safety
When you runanchor build, Anchor generates TypeScript types in target/types/:
API Reference
Program Class
MethodsBuilder Class
Next Steps
Rust Client
Learn about the Rust client library
Program IDL
Understand the IDL format