What you’ll build
You’ll create a counter program that:- Creates a counter account to store data
- Increments the counter value
- Validates account ownership and authority
- Demonstrates Anchor’s account constraints
Prerequisites
Before you begin, ensure you have installed:- Rust
- Solana CLI
- Anchor CLI
- Node.js and Yarn
Build the counter program
Understanding the code
Let’s break down the key components:Program structure
Space calculation
When initializing accounts, you must specify the space:- 8 bytes: Account discriminator (automatically added by Anchor)
- 32 bytes:
Pubkeyfor authority - 8 bytes:
u64for count
Account constraints
Anchor provides powerful constraints for security:init- Create and initialize an accountmut- Account data can be modifiedhas_one- Validate account relationshipsconstraint- Custom validation logicsigner- Account must sign the transaction
Next steps
Congratulations! You’ve built and deployed your first Anchor program. Here’s what to explore next:Program structure
Learn about Anchor macros and program architecture
Account constraints
Explore all available account validation constraints
TypeScript client
Build frontend applications with the Anchor TS client
Examples
Browse more example programs and tutorials