Prerequisites
Before you start, make sure you have the following installed on your machine:
- Rust and Cargo (
rustupinstaller recommended) - Solana CLI tools (
solana-install) - Anchor CLI (
cargo install --git https://github.com/coral-xyz/anchor anchor-cli) - Node.js and Yarn (for running tests)
- A Solana wallet with devnet SOL (use
solana airdrop 2)
You'll also want access to a GPT-4-class model — either via ChatGPT Plus or an API key — to use as your AI pair programmer throughout this guide.
Step 1: Initialize Your Anchor Project
Run the following command to scaffold a new Anchor project:
anchor init my_first_program
cd my_first_program
This generates a project with a programs/ folder containing your Rust smart contract, a tests/ folder for TypeScript tests, and a root-level Anchor.toml config file.
GPT tip: Paste the generated lib.rs file into ChatGPT and ask it to explain what each macro and attribute does. This gives you a mental model of Anchor's structure before you start modifying it.
Step 2: Define Your Program's Logic
Let's build a simple on-chain counter. Open programs/my_first_program/src/lib.rs and replace the default content with the following structure:
use anchor_lang::prelude::*;
declare_id!("YOUR_PROGRAM_ID_HERE");
#[program]
pub mod my_first_program {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
ctx.accounts.counter.count = 0;
Ok(())
}
pub fn increment(ctx: Context<Increment>) -> Result<()> {
ctx.accounts.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,
}
GPT tip: Ask GPT to explain the space = 8 + 8 calculation, why the discriminator takes 8 bytes, and what would happen if you allocated the wrong space. Understanding these details prevents hard-to-debug errors.
Step 3: Build and Deploy to Devnet
Configure your cluster and build the program:
solana config set --url devnet
anchor build
After building, copy the program ID from target/idl/my_first_program.json or by running solana address -k target/deploy/my_first_program-keypair.json. Paste it into the declare_id! macro and Anchor.toml. Then deploy:
anchor deploy
Step 4: Write and Run Tests
Open tests/my_first_program.ts. Here's where GPT really shines — ask it to generate a full test suite for your counter program. A good prompt: "Write an Anchor TypeScript test that initializes the counter account, calls increment three times, and asserts the final count equals 3."
Run your tests against devnet with:
anchor test --skip-local-validator
Step 5: Common Errors and How GPT Can Help Debug Them
| Error | Likely Cause |
|---|---|
AccountNotEnoughKeys | Missing required account in instruction call |
ConstraintMut | Account not marked as mutable in context struct |
InstructionFallbackNotFound | Calling a function not defined in the program |
InvalidProgramId | declare_id! doesn't match deployed program address |
Paste the full error output into GPT along with your relevant code section for targeted debugging advice. This workflow dramatically shortens debugging time for beginners.
Next Steps
From here, you can extend your counter with access control (only the original creator can increment), add PDAs (Program Derived Addresses) for more complex state management, or integrate your program with a React frontend using @coral-xyz/anchor and @solana/wallet-adapter. Each step is made faster and more approachable with an AI pair programmer by your side.