123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- ---
- title: Local Development
- description:
- Learn how to build Solana programs using the Anchor framework on your local
- machine.
- ---
- The Anchor framework is a tool that simplifies the process of building Solana
- programs. Whether you're new to blockchain development or an experienced
- programmer, Anchor simplifies the process of writing, testing, and deploying
- Solana programs.
- In this section, we'll walk through:
- - Creating a new Anchor project
- - Building and testing your program
- - Deploying to Solana clusters
- - Understanding the project file structure
- ## Prerequisites
- For detailed installation instructions, visit the
- [installation](/docs/installation) page.
- Before you begin, ensure you have the following installed:
- - Rust: The programming language for building Solana programs.
- - Solana CLI: Command-line tool for Solana development.
- - Anchor CLI: Command-line tool for the Anchor framework.
- To verify Anchor CLI installation, open your terminal and run:
- ```shell filename="Terminal"
- anchor --version
- ```
- Expected output:
- ```shell filename="Terminal"
- anchor-cli 0.31.1
- ```
- ## Getting Started
- This section covers the basic steps to create, build, and test your first local
- Anchor program.
- <Steps>
- <Step>
- ### Create a new Project
- To start a new project, use the `anchor init` command followed by your project's
- name. This command creates a new directory with the specified name and sets up a
- default program and test file.
- ```shell filename="Terminal"
- anchor init my-project
- ```
- Navigate to the new project directory and open it in your code editor.
- ```shell filename="Terminal" copy
- cd my-project
- ```
- The default Anchor program is located at `/programs/my-project/src/lib.rs`.
- <Accordions>
- <Accordion title="Default Program">
- The value in the `declare_id!` macro is the program ID, a unique identifier for
- your program.
- By default, it is the public key of the keypair generated in
- `/target/deploy/my_project-keypair.json`.
- ```rust filename="lib.rs"
- use anchor_lang::prelude::*;
- declare_id!("3ynNB373Q3VAzKp7m4x238po36hjAGFXFJB4ybN2iTyg");
- #[program]
- pub mod my_project {
- use super::*;
- pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
- msg!("Greetings from: {:?}", ctx.program_id);
- Ok(())
- }
- }
- #[derive(Accounts)]
- pub struct Initialize {}
- ```
- </Accordion>
- </Accordions>
- The default Typescript test file is located at `/tests/my-project.ts`.
- <Accordions>
- <Accordion title="Default Test File">
- This file demonstrates how to invoke the default program's `initialize`
- instruction in Typescript.
- ```ts filename="my-project.ts"
- import * as anchor from "@coral-xyz/anchor";
- import { Program } from "@coral-xyz/anchor";
- import { MyProject } from "../target/types/my_project";
- describe("my-project", () => {
- // Configure the client to use the local cluster.
- anchor.setProvider(anchor.AnchorProvider.env());
- const program = anchor.workspace.MyProject as Program<MyProject>;
- it("Is initialized!", async () => {
- // Add your test here.
- const tx = await program.methods.initialize().rpc();
- console.log("Your transaction signature", tx);
- });
- });
- ```
- </Accordion>
- </Accordions>
- If you prefer Rust for testing, initialize your project with the
- `--test-template rust` ([Anchor Rust client](/docs/clients/rust)) or
- `--test-template mollusk` ([Mollusk test library](/docs/testing/mollusk)) flag.
- ```shell
- anchor init --test-template rust my-program
- ```
- The Rust test file will be at `/tests/src/test_initialize.rs`.
- <Accordions>
- <Accordion title="Rust Test File">
- ```rust filename="test_initialize.rs"
- use std::str::FromStr;
- use anchor_client::{
- solana_sdk::{
- commitment_config::CommitmentConfig, pubkey::Pubkey, signature::read_keypair_file,
- },
- Client, Cluster,
- };
- #[test]
- fn test_initialize() {
- let program_id = "3ynNB373Q3VAzKp7m4x238po36hjAGFXFJB4ybN2iTyg";
- let anchor_wallet = std::env::var("ANCHOR_WALLET").unwrap();
- let payer = read_keypair_file(&anchor_wallet).unwrap();
- let client = Client::new_with_options(Cluster::Localnet, &payer, CommitmentConfig::confirmed());
- let program_id = Pubkey::from_str(program_id).unwrap();
- let program = client.program(program_id).unwrap();
- let tx = program
- .request()
- .accounts(my_program::accounts::Initialize {})
- .args(my_program::instruction::Initialize {})
- .send()
- .expect("");
- println!("Your transaction signature {}", tx);
- }
- ```
- </Accordion>
- </Accordions>
- </Step>
- <Step>
- ### Build the Program
- Build the program by running `anchor build`.
- ```shell filename="Terminal" copy
- anchor build
- ```
- The compiled program will be at `/target/deploy/my_project.so`. The content of
- this file is what gets stored on the Solana network (as an executable account)
- when you deploy your program.
- </Step>
- <Step>
- ### Test the Program
- To test the program, run `anchor test`.
- ```shell filename="Terminal" copy
- anchor test
- ```
- By default, the `Anchor.toml` config file specifies the `localnet` cluster. When
- developing on `localnet`, `anchor test` will automatically:
- 1. Start a local Solana validator
- 2. Build and deploy your program to the local cluster
- 3. Run the tests in the `tests` folder
- 4. Stop the local Solana validator
- Alternatively, you can manually start a local Solana validator and run tests
- against it. This is useful if you want to keep the validator running while you
- iterate on your program. It allows you to inspect accounts and transaction logs
- on the [Solana Explorer](https://explorer.solana.com/?cluster=custom) while
- developing locally.
- Open a new terminal and start a local Solana validator by running the
- `solana-test-validator` command.
- ```shell filename="Terminal" copy
- solana-test-validator
- ```
- In a separate terminal, run the tests against the local cluster. Use the
- `--skip-local-validator` flag to skip starting the local validator since it's
- already running.
- ```shell filename="Terminal" copy
- anchor test --skip-local-validator
- ```
- </Step>
- <Step>
- ### Deploy to Devnet
- By default, the `Anchor.toml` config file in an Anchor project specifies the
- localnet cluster.
- ```toml filename="Anchor.toml" {14}
- [toolchain]
- [features]
- resolution = true
- skip-lint = false
- [programs.localnet]
- my_program = "3ynNB373Q3VAzKp7m4x238po36hjAGFXFJB4ybN2iTyg"
- [registry]
- url = "https://api.apr.dev"
- [provider]
- cluster = "Localnet"
- wallet = "~/.config/solana/id.json"
- [scripts]
- test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
- ```
- To deploy your program to devnet, change the `cluster` value to `Devnet`.
- <Callout type="info">
- Note that deploying to devnet requires your wallet to have enough SOL to cover
- deployment cost. You can get devnet SOL using the
- [Web Faucet](https://faucet.solana.com/).
- </Callout>
- ```diff
- -cluster = "Localnet"
- +cluster = "Devnet"
- ```
- ```toml filename="Anchor.toml"
- [provider]
- cluster = "Devnet"
- wallet = "~/.config/solana/id.json"
- ```
- Now when you run `anchor deploy`, your program will be deployed to the devnet
- cluster. The `anchor test` command will also use the cluster specified in the
- `Anchor.toml` file.
- ```shell
- anchor deploy
- ```
- To deploy to mainnet, simply update the `Anchor.toml` file to specify the
- mainnet cluster.
- ```toml filename="Anchor.toml"
- [provider]
- cluster = "Mainnet"
- wallet = "~/.config/solana/id.json"
- ```
- </Step>
- <Step>
- ### Update the Program
- Solana programs can be updated by redeploying the program to the same program
- ID.
- To update a program, simply make changes to your program's code and run the
- `anchor build` command to generated an updated `.so` file.
- ```shell
- anchor build
- ```
- Then run the `anchor deploy` command to redeploy the updated program.
- ```shell
- anchor deploy
- ```
- </Step>
- <Step>
- ### Close the Program
- To reclaim the SOL allocated to a program account, you can close your Solana
- program.
- To close a program, use the `solana program close <PROGRAM_ID>` command. For
- example:
- ```shell
- solana program close 3ynNB373Q3VAzKp7m4x238po36hjAGFXFJB4ybN2iTyg --bypass-warning
- ```
- Note that once a program is closed, the program ID cannot be reused to deploy a
- new program.
- </Step>
- </Steps>
- ## Project File Structure
- Below is an overview of default file structure in an Anchor workspace:
- <Files>
- <Folder name=".anchor">
- <File name="program-logs" />
- </Folder>
- <Folder name="app" />
- <Folder name="migrations" />
- <Folder name="programs" defaultOpen={true}>
- <Folder name="[project-name]" defaultOpen={true}>
- <Folder name="src" defaultOpen={true}>
- <File name="lib.rs" />
- <File name="Cargo.toml" />
- <File name="Xargo.toml" />
- </Folder>
- </Folder>
- </Folder>
- <Folder name="target" defaultOpen={true}>
- <Folder name="deploy">
- <File name="[project-name]-keypair.json" />
- </Folder>
- <Folder name="idl">
- <File name="[project-name].json" />
- </Folder>
- <Folder name="types">
- <File name="[project-name].ts" />
- </Folder>
- </Folder>
- <Folder name="tests" defaultOpen={true}>
- <File name="[project-name].ts" />
- </Folder>
- <File name="Anchor.toml" />
- <File name="Cargo.toml" />
- <File name="package.json" />
- </Files>
- ### Programs Folder
- The `/programs` directory contains your project's Anchor programs. A single
- workspace can contain multiple programs.
- ### Tests Folder
- The `/tests` directory contains test files for your project. A default test file
- is created for you when you create your project.
- ### Target Folder
- The `/target` directory contains build outputs. The main subfolders include:
- - `/deploy`: Contains the keypair and program binary for your programs.
- - `/idl`: Contains the JSON IDL for your programs.
- - `/types`: Contains the TypeScript type for the IDL.
- ### Anchor.toml File
- The `Anchor.toml` file configures workspace settings for your project.
- ### .anchor Folder
- Includes a `program-logs` file that contains transaction logs from the last run
- of test files.
- ### App Folder
- The `/app` folder is an empty folder that can be optionally used for your
- frontend code.
|