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.
anchor-cli
USAGE:
anchor <SUBCOMMAND>
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
SUBCOMMANDS:
build Builds the workspace
cluster Cluster commands
deploy Deploys each program in the workspace
help Prints this message or the help of the given subcommand(s)
idl Commands for interacting with interface definitions
init Initializes a workspace
launch Deploys, initializes an IDL, and migrates all in one command
migrate Runs the deploy migration script
new Creates a new program
test Runs integration tests against a localnetwork
upgrade Upgrades a single program. The configured wallet must be the upgrade authority
verify Verifies the on-chain bytecode matches the locally compiled artifact. Run this
command inside a program subdirectory, i.e., in the dir containing the program's
Cargo.toml
anchor init
Initializes a project workspace wit the following structure.
Anchor.toml
: Anchor configuration file.Cargo.toml
: Rust workspace configuration file.programs/
: Directory for Solana program crates.app/
: Directory for your application frontend.tests/
: Directory for JavaScript integration tests.migrations/deploy.js
: Deploy script.
anchor build
Builds programs in the workspace targeting Solana's BPF runtime and emitting IDLs in the target/idl
directory.
anchor deploy
Deploys all programs in the workspace to the configured cluster.
::: tip Note
This is different from the solana program deploy
command, because everytime it's run
it will generate a new program address.
:::
anchor upgrade <target/deploy/program.so> --program-id <program-id>
Uses Solana's upgradeable BPF loader to upgrade the on chain program code.
anchor 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.
::: tip Note The Anchor workflow recommends to test your program using integration tests in a language other than Rust to make sure that bugs related to syntax misunderstandings are coverable with tests and not just replicated in tests. :::
anchor migrate
Runs the deploy script located at migrations/deploy.js
, injecting a provider configured
form the workspace's Anchor.toml
. For example,
// File: migrations/deploys.js
const anchor = require("@project-serum/anchor");
module.exports = async function (provider) {
anchor.setProvider(provider);
// Add your deploy script here.
}
Migrations are a new feature and only support this simple deploy script at the moment.
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 the program's ID. This
allow us to generate clients for a program using nothing but the program ID.
anchor idl init -f <target/idl/program.json> <program-id>
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.
anchor idl fetch -o <out-file.json> <program-id>
Fetches an IDL from the configured blockchain. For example, make sure
your Anchor.toml
is pointing to the mainnet
cluster and run
anchor idl fetch GrAkKfEpTKQuVHG2Y97Y2FF4i7y7Q5AHLK94JBy7Y5yv
anchor idl authority <program-id>
Outputs the IDL account's authority. This is the wallet that has the ability to update the IDL.
anchor idl erase-authority -p <program-id>
Erases the IDL account's authority so that upgrades can no longer occur. The configured wallet must be the current authority.
anchor idl upgrade <program-id> -f <target/idl/program.json>
Upgrades the IDL file on chain to the new target/idl/program.json
idl.
The configured wallet must be the current authority.
anchor idl set-authority -n <new-authority> -p <program-id>
Sets a new authority on the IDL account. Both the new-authority
and program-id
must be encoded in base 58.
anchor launch
Builds, deploys and migrates, all in one command. This is particularly useful when simultaneously developing an app against a Localnet or Devnet. For mainnet, it's recommended to run each command separately, since transactions can sometimes be unreliable depending on the Solana RPC node being used.
anchor new <program-name>
Creates a new program in the workspace's programs/
directory initialized with boilerplate.
anchor cluster list
This lists cluster endpoints:
Cluster Endpoints:
* Mainnet - https://solana-api.projectserum.com
* Mainnet - https://api.mainnet-beta.solana.com
* Devnet - https://api.devnet.solana.com
* Testnet - https://api.testnet.solana.com
Verifies the on-chain bytecode matches the locally compiled artifact.