Quickstart Guide
Build your first Solana program with Anchor in minutes
This quickstart guide will help you create your first Solana program using the Anchor framework. We'll build a simple counter program that stores and increments a number on the blockchain.
Prerequisites: Make sure you've completed the Installation Guide before proceeding.
Step 1: Create a New Anchor Project
Open your terminal and create a new Anchor project:
anchor init my-counter
cd my-counter
This creates a new project with the following structure:
my-counter/
├── Anchor.toml # Anchor configuration
├── Cargo.toml # Rust dependencies
├── programs/ # Your Solana programs
│ └── my-counter/
│ └── src/
│ └── lib.rs
├── tests/ # JavaScript tests
│ └── my-counter.ts
└── migrations/ # Deploy scripts
Step 2: Write the Program
Open programs/my-counter/src/lib.rs and replace the content with:
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod my_counter {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
let counter = &mut ctx.accounts.counter;
counter.count = 0;
Ok(())
}
pub fn increment(ctx: Context<Increment>) -> Result<()> {
let counter = &mut ctx.accounts.counter;
counter.count += 1;
Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init, payer = user, space = 8 + 8)]
pub counter: Account<'info, Counter>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct Increment<'info> {
#[account(mut)]
pub counter: Account<'info, Counter>,
}
#[account]
pub struct Counter {
pub count: u64,
}
Understanding the Code
- declare_id! - Declares the program's on-chain address
- #[program] - Defines the program module with instruction handlers
- initialize - Creates a new counter account with count = 0
- increment - Increases the counter by 1
- #[account] - Defines the Counter data structure stored on-chain
Step 3: Build the Program
Build your program to make sure everything compiles correctly:
anchor build
This will compile your Rust program into a BPF (Berkeley Packet Filter) binary that can run on Solana.
Step 4: Get Your Program ID
After building, get your program's public key:
solana address -k target/deploy/my_counter-keypair.json
Copy this address and update it in two places:
- Replace the ID in
declare_id!inlib.rs - Replace the ID in
Anchor.toml
Then rebuild:
anchor build
Step 5: Run Tests
Anchor generates test files automatically. Run the tests:
anchor test
This will:
- Start a local Solana validator
- Deploy your program
- Run the tests defined in
tests/my-counter.ts
Step 6: Write a Simple Test
Open tests/my-counter.ts and add a test:
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { MyCounter } from "../target/types/my_counter";
describe("my-counter", () => {
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
const program = anchor.workspace.MyCounter as Program<MyCounter>;
const counter = anchor.web3.Keypair.generate();
it("Initializes the counter", async () => {
await program.methods
.initialize()
.accounts({
counter: counter.publicKey,
user: provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
})
.signers([counter])
.rpc();
const account = await program.account.counter.fetch(counter.publicKey);
console.log("Count:", account.count.toString());
});
it("Increments the counter", async () => {
await program.methods
.increment()
.accounts({
counter: counter.publicKey,
})
.rpc();
const account = await program.account.counter.fetch(counter.publicKey);
console.log("Count after increment:", account.count.toString());
});
});
Step 7: Deploy to Devnet
Once tests pass, deploy to devnet:
# Make sure you're on devnet
solana config set --url devnet
# Get some SOL for deployment
solana airdrop 2
# Deploy the program
anchor deploy
Congratulations! 🎉
You've successfully created and deployed your first Solana program! Your counter program is now live on devnet.
What you learned:
- How to create an Anchor project
- Writing program instructions (initialize, increment)
- Defining accounts and data structures
- Testing programs locally
- Deploying to devnet