|
| 1 | +# Commands |
| 2 | + |
| 3 | +A CLI is provided to support building and managing an Anchor workspace. |
| 4 | +For a comprehensive list of commands and options, run `anchor -h` on any |
| 5 | +of the following subcommands. |
| 6 | + |
| 7 | +``` |
| 8 | +anchor-cli |
| 9 | +
|
| 10 | +USAGE: |
| 11 | + anchor <SUBCOMMAND> |
| 12 | +
|
| 13 | +FLAGS: |
| 14 | + -h, --help Prints help information |
| 15 | + -V, --version Prints version information |
| 16 | +
|
| 17 | +SUBCOMMANDS: |
| 18 | + build Builds the workspace |
| 19 | + deploy Deploys each program in the workspace |
| 20 | + idl Commands for interacting with interface definitions |
| 21 | + init Initializes a workspace |
| 22 | + launch Deploys, initializes an IDL, and migrates all in one command |
| 23 | + migrate Runs the deploy migration script |
| 24 | + new Creates a new program |
| 25 | + test Runs integration tests against a localnetwork |
| 26 | + upgrade Upgrades a single program. The configured wallet must be the upgrade authority |
| 27 | +``` |
| 28 | + |
| 29 | +## Init |
| 30 | + |
| 31 | +``` |
| 32 | +anchor init |
| 33 | +``` |
| 34 | + |
| 35 | +Initializes a project workspace wit the following structure. |
| 36 | + |
| 37 | +* `Anchor.toml`: Anchor configuration file. |
| 38 | +* `programs/`: Directory for Solana program crates. |
| 39 | +* `app/`: Directory for your application frontend. |
| 40 | +* `tests/`: Directory for JavaScript integration tests. |
| 41 | +* `migrations/deploy.js`: Deploy script. |
| 42 | + |
| 43 | +## Build |
| 44 | + |
| 45 | +``` |
| 46 | +anchor build |
| 47 | +``` |
| 48 | + |
| 49 | +Builds programs in the workspace targeting Solana's BPF runtime and emitting IDLs in the `target/idl` directory. |
| 50 | + |
| 51 | +## Deploy |
| 52 | + |
| 53 | +``` |
| 54 | +anchor deploy |
| 55 | +``` |
| 56 | + |
| 57 | +Deploys all programs in the workspace to the configured cluster. |
| 58 | + |
| 59 | +::: tip Note |
| 60 | +This is different from the `solana program deploy` command, because everytime it's run |
| 61 | +it will generate a *new* program address. |
| 62 | +::: |
| 63 | + |
| 64 | +## Upgrade |
| 65 | + |
| 66 | +``` |
| 67 | +anchor upgrade <target/deplooy/program.so> --program-id <program-id> |
| 68 | +``` |
| 69 | + |
| 70 | +Uses Solana's upgradeable BPF loader to upgrade the on chain program code. |
| 71 | + |
| 72 | +## Test |
| 73 | + |
| 74 | +``` |
| 75 | +anchor test |
| 76 | +``` |
| 77 | + |
| 78 | +Run an integration test suit against the configured cluster, deploying new versions |
| 79 | +of all workspace programs before running them. |
| 80 | + |
| 81 | +If the configured network is a localnet, then automatically starts the localnetwork and runs |
| 82 | +the test. |
| 83 | + |
| 84 | +::: tip Note |
| 85 | +The Anchor workflow [recommends](https://www.parity.io/paritys-checklist-for-secure-smart-contract-development/) |
| 86 | +to test your program using integration tests in a language other |
| 87 | +than Rust to make sure that bugs related to syntax misunderstandings |
| 88 | +are coverable with tests and not just replicated in tests. |
| 89 | +::: |
| 90 | + |
| 91 | +## Migrate |
| 92 | + |
| 93 | +``` |
| 94 | +anchor migrate |
| 95 | +``` |
| 96 | + |
| 97 | +Runs the deploy script located at `migrations/deploy.js`, injecting a provider configured |
| 98 | +form the workspace's `Anchor.toml`. For example, |
| 99 | + |
| 100 | +```javascript |
| 101 | +// File: migrations/deploys.js |
| 102 | + |
| 103 | +const anchor = require("@project-serum/anchor"); |
| 104 | + |
| 105 | +module.exports = async function (provider) { |
| 106 | + anchor.setProvider(provider); |
| 107 | + |
| 108 | + // Add your deploy script here. |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +Migrations are a new feature |
| 113 | +and only support this simple deploy script at the moment. |
| 114 | + |
| 115 | +## Idl |
| 116 | + |
| 117 | +The `idl` subcommand provides commands for interacting with interface definition files. |
| 118 | +It's recommended to use these commands to store an IDL on chain, at a deterministic |
| 119 | +address, as a function of nothing but the the program's ID. This |
| 120 | +allow us to generate clients for a program using nothing but the program ID. |
| 121 | + |
| 122 | +### Idl Init |
| 123 | + |
| 124 | +``` |
| 125 | +anchor idl init -f <target/idl/program.json> <program-id> |
| 126 | +``` |
| 127 | + |
| 128 | +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, |
| 129 | +allowing room for growth in case the idl needs to be upgraded in the future. |
| 130 | + |
| 131 | +### Idl Fetch |
| 132 | + |
| 133 | +``` |
| 134 | +anchor idl fetch -o <out-file.json> <program-id> |
| 135 | +``` |
| 136 | + |
| 137 | +Fetches an IDL from the configured blockchain. For example, make sure |
| 138 | +your `Anchor.toml` is pointing to the `mainnet` cluster and run |
| 139 | + |
| 140 | +``` |
| 141 | +anchor idl fetch GrAkKfEpTKQuVHG2Y97Y2FF4i7y7Q5AHLK94JBy7Y5yv |
| 142 | +``` |
| 143 | + |
| 144 | +### Idl Authority |
| 145 | + |
| 146 | +``` |
| 147 | +anchor idl authority <program-id> |
| 148 | +``` |
| 149 | + |
| 150 | +Outputs the IDL account's authority. This is the wallet that has the ability to |
| 151 | +update the IDL. |
| 152 | + |
| 153 | +### Idl Erase Authority |
| 154 | + |
| 155 | +``` |
| 156 | +anchor idl erase-authority -p <program-id> |
| 157 | +``` |
| 158 | + |
| 159 | +Erases the IDL account's authority so that upgrades can no longer occur. The |
| 160 | +configured wallet must be the current authority. |
| 161 | + |
| 162 | +### Idl Upgrade |
| 163 | + |
| 164 | +``` |
| 165 | +anchor idl upgrade <program-id> -f <target/idl/program.json> |
| 166 | +``` |
| 167 | + |
| 168 | +Upgrades the IDL file on chain to the new `target/idl/program.json` idl. |
| 169 | +The configured wallet must be the current authority. |
| 170 | + |
| 171 | +``` |
| 172 | +anchor idl set-authority -n <new-authority> -p <program-id> |
| 173 | +``` |
| 174 | + |
| 175 | +Sets a new authority on the IDL account. Both the `new-authority` and `program-id` |
| 176 | +must be encoded in base 58. |
| 177 | + |
| 178 | +## Launch |
| 179 | + |
| 180 | +``` |
| 181 | +anchor launch |
| 182 | +``` |
| 183 | + |
| 184 | +Builds, deploys and migrates, all in one command. This is particularly |
| 185 | +useful when simultaneously developing an app against a Localnet or Devnet. For mainnet, it's |
| 186 | +recommended to run each command separately, since transactions can sometimes be |
| 187 | +unreliable depending on the Solana RPC node being used. |
| 188 | + |
| 189 | +## New |
| 190 | + |
| 191 | +``` |
| 192 | +anchor new <program-name> |
| 193 | +``` |
| 194 | + |
| 195 | +Creates a new program in the workspace's `programs/` directory initialized with boilerplate. |
0 commit comments