Anchor CLI
Anchor CLI reference documentation
A CLI is provided to support building and managing an Anchor workspace. For a
comprehensive list of commands and options, run anchor -h on any of the
following subcommands.
Account
Fetches an account with the given public key and deserializes the data to JSON using the type name provided. If this command is run from within a workspace, the workspace's IDL files will be used to get the data types. Otherwise, the path to the IDL file must be provided.
The program-name is the name of the program where the account struct resides,
usually under programs/<program-name>. program-name should be provided in a
case-sensitive manner exactly as the folder name, usually in kebab-case.
The AccountTypeName is the name of the account struct, usually in PascalCase.
The account_pubkey refers to the Pubkey of the account to deserialize, in
Base58.
Example Usage:
anchor account anchor-escrow.EscrowAccount 3PNkzWKXCsbjijbasnx55NEpJe8DFXvEEbJKdRKpDcfK,
deserializes an account in the given pubkey with the account struct
EscrowAccount defined in the anchor-escrow program.
Deserializes the account with the data types provided in the given IDL file even if inside a workspace.
Build
Builds programs in the workspace targeting Solana's BPF runtime and emitting
IDLs in the target/idl directory.
Runs the build inside a docker image so that the output binary is deterministic
(assuming a Cargo.lock file is used). This command must be run from within a
single crate subdirectory within the workspace. For example,
programs/<my-program>/.
Tip
It's possible to pass arguments to the underlying cargo build-sbf command with -- <ARGS>. For example:
Cluster
Cluster list
This lists cluster endpoints:
Deploy
Deploys all programs in the workspace to the configured cluster.
Tip
This is different from the solana program deploy command, because every time
it's run it will generate a new program address.
Expand
If run inside a program folder, expands the macros of the program.
If run in the workspace but outside a program folder, expands the macros of the workspace.
If run with the --program-name option, expand only the given program.
Idl
The idl subcommand provides commands for interacting with interface definition
files. Anchor uses the Program Metadata
system to store IDLs on-chain at a deterministic address derived from the
program's ID. This allows clients to be generated for a program using nothing
but the program ID.
IDL management uses the @solana-program/program-metadata
package instead of legacy IDL instructions. This results in smaller program
binaries and a more standardized approach to on-chain metadata.
Idl Build
Generates the IDL for the program using the compilation method.
Idl Init
Creates a metadata account containing the IDL for the given program. The IDL file is written to an account derived from the program ID.
Use the --non-canonical flag to create a third-party (non-canonical) metadata
account. This is useful when you want to store metadata for a program you don't
own.
The program-id argument is optional — when omitted, idl.address is used.
Idl Fetch
Fetches an IDL from the configured blockchain. For example, make sure your
Anchor.toml is pointing to the mainnet cluster and run
Use the --non-canonical flag to fetch third-party metadata:
Idl Upgrade
Upgrades the IDL file on chain to the new target/idl/program.json idl. The
configured wallet must be the current authority. The program-id argument is
optional — when omitted, idl.address is used.
Idl Close
Closes the metadata account and recovers the rent. By default, closes the "idl"
seed account. Use --seed to specify a different seed:
Idl Create Buffer
Creates a buffer account for metadata. This is useful for large IDLs that need to be written across multiple transactions.
Idl Set Buffer Authority
Sets a new authority on a buffer account.
Idl Write Buffer
Writes metadata to the program using a pre-created buffer account. Use
--seed to specify the metadata seed (defaults to "idl"):
Use --close-buffer to automatically close the buffer account after writing:
Codama
anchor codama convert converts an Anchor IDL into a Codama IDL. anchor codama generate converts the IDL and invokes the Codama renderer packages for
the requested languages. The supported language values are js, js-umi,
rust, and go.
Codama client generation can also run automatically after anchor build when
[clients] auto = true is set in Anchor.toml.
Codama
anchor codama convert converts an Anchor IDL into a Codama IDL. anchor codama generate converts the IDL and invokes the Codama renderer packages for
the requested languages. The supported language values are js, js-umi,
rust, and go.
Codama client generation can also run automatically after anchor build when
[clients] auto = true is set in Anchor.toml.
Init
Initializes a project workspace with the following structure.
Anchor.toml: Anchor configuration file.Cargo.toml: Rust workspace configuration file.package.json: JavaScript dependencies file.programs/: Directory for Solana program crates.app/: Directory for your application frontend.tests/: Directory for JavaScript integration tests.migrations/deploy.js: Deploy script.
By default, programs are initialized with a modular structure (multiple files) to promote better code organization. This is the recommended approach for production code.
Template Options:
The modular template organizes code into separate files for instructions, state, constants, and errors, making it easier to navigate and maintain as your program grows.
Anchor Version:
The selected Anchor version controls the generated Rust program and Rust test
template dependencies. V2 templates use the anchor-next git dependencies until
the v2 crates are published.
Keys
Program keypair commands.
Keys List
List all of the program keys.
Keys Sync
Sync program declare_id! pubkeys with the program's actual pubkey.
Migrate
Runs the deploy script located at migrations/deploy.js, injecting a provider
configured from the workspace's Anchor.toml. For example,
Migrations are a new feature and only support this simple deploy script at the moment.
New
Creates a new program in the workspace's programs/ directory initialized with
boilerplate.
By default, uses the modular structure template (recommended). You can
specify a different template with the --template flag:
You can also select the Anchor Rust template version:
Shell
Starts a node js shell with an Anchor client setup according to the local config. This client can be used to interact with deployed Solana programs in the workspace.
Test
Run an integration test suit against the configured cluster, deploying new versions of all workspace programs before running them.
If the configured network is a localnet, then automatically starts the
local network and runs the test. By default, Surfpool
is used as the local network backend. To use solana-test-validator instead,
pass --validator legacy.
Note
Be sure to shutdown any other local validators, otherwise anchor test will fail to run.
If you'd prefer to run the program against your local validator use
anchor test --skip-local-validator.
When running tests we stream program logs to
.anchor/program-logs/<address>.<program-name>.log
Use --profile with Rust/LiteSVM-style tests that enable the generated
profile feature to collect SBF register traces and render flamegraph SVGs
under target/anchor-v2-profile/.
Debugger
Runs tests with profiling enabled and opens an instruction-level TUI over the
captured SBF traces. --skip-run reuses existing traces, and --gdb uses the
sbpf gdb-stub trace path.
Coverage
Generates LCOV source coverage from SBF register traces. By default traces are
collected under target/coverage/traces.
Upgrade
Uses Solana's upgradeable BPF loader to upgrade the on chain program code.
Verify
Verifies the on-chain bytecode matches the locally compiled artifact.