Anchor Commands Cheatsheet

Anchor Commands Cheatsheet

ยท

4 min read

This is the workflow and commands used when hacking with Anchor

Init Project

export proj=mysolanaproject
anchor init $proj; cd $proj; anchor build

๐Ÿ’ƒ๐Ÿฝ A little Dance - Getting a new Program Address

From here, we will have to do a little dance that is necessary everytime you create a new Program with Anchor. We need to get a Program Address, every program have an address / id that is how the Blockchain can identify your Program versus someone else Program.

Anchor by default does not create you a new ProgramID but instead use a default one, this will be problematic when we want to deploy to the Blockchain. To generate a new Program Address, we need to do a first deploy and then change some config files.

Start by opening a second terminal and run a local node

# if you are on M1 and your Solana version is below v1.9.4, you need to add this option, this is tricky because even if it runs this will not work properly.
solana-test-validator --no-bpf-jit

# Otherwise just
solana-test-validator

Back to your main terminal

anchor deploy

After our first build we now have a keypair and we can use the public address as the ProgramID.

Run the following to change the Program Address of Anchor.toml

sed -i '' "s/Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS/$(solana address -k target/deploy/$proj-keypair.json)/g" Anchor.toml

Run the following to change the Program Address of programs/$proj/src/lib.rs

sed -i '' "s/Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS/$(solana address -k target/deploy/$proj-keypair.json)/g" programs/$proj/src/lib.rs

Let's build and deploy once again with our new identifier

anchor build && anchor deploy

If it all goes well you should see an additional address in target/idl/$proj.json

"metadata": {
  "address": "7fi9vz69eeButYvCvxFFbeVPcJBsyP1cT8bbxYwxPxma"
}

Testing

If you have previously built and deploy to a running ledger On a separate terminal session, start the local ledger.

solana-test-validator

Then, back on the main terminal session.

anchor build &&  anchor deploy && anchor test --skip-local-validator

It means if you are just tweaking your test files, you can just

anchor test --skip-local-validator

But if you need to do the whole cycle + run a fresh local ledger (solana-test-validator --reset) You can do the whole thing in one shot with, make sure that you don't have any local ledger running tho!

anchor test

"Serving your backend"

This command is quite useful when you focusing on your frontend and just need the latest version of your solana program running

anchor localnet

It is the same as

solana-test-validator --reset
anchor build
anchor deploy

IDL

Uploading idl for the first time

anchor idl init  -f target/idl/$proj.json `solana address -k target/deploy/$proj-keypair.json`

Upgrading IDL

anchor idl upgrade  -f target/idl/$proj.json `solana address -k target/deploy/$proj-keypair.json`

WTF Anchor

Are you tired of anchor magic and want to see what's happening under the hood for your program?

anchor expand

Logging

These commands can be used in your program

    msg!("static string");

    // Log a slice
    sol_log_slice(instruction_data);

    // Log a formatted message, use with caution can be expensive
    msg!("formatted {}: {:?}", "message", instruction_data);

    // Log a public key
    program_id.log();

    // Log all the program's input parameters
    sol_log_params(accounts, instruction_data);

    // Log the number of compute units remaining that the program can consume.
    sol_log_compute_units();

And can be found with

cat .anchor/program-logs/*

or

solana logs --url localhost

Deploying to DevNet

Switch Environment

solana config set --url devnet

Airdrop yourself some sol, why do you need sols? Well when you deploy to a Blockchain you are doing a Transaction and like all Transaction in the Blockchains, you need to pay for it.

solana airdrop 5

If the command doesn't work try again later or with less sol.

Where did the sol go? The sol went to your paper wallet, Solana has created a keypair to be used as a wallet in ~/.solana/id.json.

So you currently have two keypairs:

  1. target/deploy/$proj-keypair.json: this is a keypair per project
  2. ~/.config/solana.id.json: this is your global developer keypair, Solana CLI created a keypair for you to be used to deploy programs, when you deploy programs you are updating the blockchain, and thus you need to sign and pay the Transaction to do so, that is why you need a keypair.

You can see the difference between the balance of your wallets:

solana balance $(solana address -k ~/.config/solana.id.json)
solana balance $(solana address -k target/deploy/$proj-keypair.json)

Check your balance

solana balance

Check your config

solana config get
Config File: /Users/matanwrites/.config/solana/cli/config.yml
RPC URL: https://api.devnet.solana.com
WebSocket URL: wss://api.devnet.solana.com/ (computed)
Keypair Path: /Users/matanwrites/.config/solana/id.json
Commitment: confirmed

Deploy the Program

anchor build && anchor deploy --provider.cluster devnet

Deploy your idl to the chain

  1. Initialize your idl if this is the first time
anchor idl init  -f target/idl/$proj.json `solana address -k target/deploy/$proj-keypair.json` --provider.cluster devnet
  1. Upgarde your idl if this is the second time
anchor idl upgrade -f target/idl/$proj.json `solana address -k target/deploy/$proj-keypair.json --provider.cluster devnet

Did you find this article valuable?

Support mwrites by becoming a sponsor. Any amount is appreciated!

ย