Solana Smart Contracts
WITH ANCHOR
Two-branch walkthrough for building, testing, and deploying a Solana program with Anchor. Focused on programs, accounts, and safe iteration—not on wallet UI complexity.
Repository & Branch Map
First stable edit of the Anchor template: simple counter program, initialize + increment, no frontend coupling. Perfect for absolute beginners.
Polished version with refined lib.rs, testp.ts, and the extra app.html. Pull the full branch—files differ from main.
Prerequisites & Checks
- Rust
- Solana CLI
- Anchor CLI
- Node.js 18+
- npm
- Linux / WSL preferred
- Consistent PATH
solana --version
anchor --version
node --version
npm --version
Solana CLI Wallet & Network
Create or reuse CLI wallet for fees, deploys, and tests.
solana address
Default path: ~/.config/solana/id.json
Anchor manages the local validator. Configure once:
solana config get
Project Setup
cd trial
Generates Anchor.toml, programs/, tests/, package.json scaffolding.
- programs/trial/src/lib.rs (program)
- tests/testp.ts (client tests)
- Anchor.toml (program IDs, provider)
Program Versions
- Counter account
- initialize + increment
- Great for first exposure
- Refined lib.rs + testp.ts
- Multiple increments, cleaner flow
- Includes app.html for context
Program ID Discipline
After every deploy, update both:
- declare_id!("NEW_PROGRAM_ID") in lib.rs
- Anchor.toml [programs.localnet].trial
Missing this causes DeclaredProgramIdMismatch.
Run & Test
Reset ledger and stray validators before tests:
rm -rf .anchor/test-ledger
Expect: Counter init to 0, then increments to 2, all tests passing.
Deploy (optional)
After deployment, copy the new Program ID, update declare_id! and Anchor.toml, then rerun tests if needed.
Common Issues
Use a fresh keypair; clear .anchor/test-ledger.
Avoid --skip-local-validator; let anchor manage it.
Update declare_id! and Anchor.toml after deploy.
Learning Outcomes
- Explain Solana programs vs. accounts and why programs are stateless.
- Use Anchor to scaffold, write, and test on-chain logic safely.
- Manage Program IDs, deployments, and local validators confidently.
- Describe how a frontend could interact without diving into wallet UX yet.