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. It's recommended to use these commands to store an IDL on chain, at a
deterministic address, as a function of nothing but the program's ID. This
allows us to generate clients for a program using nothing but the program ID.
Idl Build
Generates the IDL for the program using the compilation method.
Idl Init
Creates an idl account, writing the given <target/idl/program.json>
file into
a program owned account. By default, the size of the account is double the size
of the IDL, allowing room for growth in case the idl needs to be upgraded in the
future.
Idl Fetch
Fetches an IDL from the configured blockchain. For example, make sure your
Anchor.toml
is pointing to the mainnet
cluster and run
Idl Authority
Outputs the IDL account's authority. This is the wallet that has the ability to update the IDL.
Idl Erase Authority
Erases the IDL account's authority so that upgrades can no longer occur. The configured wallet must be the current authority.
Idl Upgrade
Upgrades the IDL file on chain to the new target/idl/program.json
idl. The
configured wallet must be the current authority.
Sets a new authority on the IDL account. Both the new-authority
and
program-id
must be encoded in base 58.
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.
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.
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 localnetwork and runs the test.
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
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.