A collection of Solana smart contracts demonstrating different programming patterns and use cases on the Solana blockchain.
A native Solana program implementing a simple counter with multiple arithmetic operations.
Features:
- Initialize counter with value 1
- Double the current count
- Halve the current count
- Add a custom amount to the counter
- Subtract a custom amount from the counter
- Uses Borsh serialization for efficient data handling
- Implements overflow protection with saturating arithmetic
Tech Stack:
- Native Solana Program (no framework)
- Borsh for serialization
- TypeScript client with Bun runtime
Checkout: counter/
An SPL token escrow program built with Anchor framework for secure token transfers between parties.
Features:
- Initialize escrow with specified token amount
- Lock tokens in a vault controlled by a PDA (Program Derived Address)
- Allow designated receiver to claim escrowed tokens
- Automatic vault creation using Associated Token Accounts
- Secure transfer validation with has_one constraint
Tech Stack:
- Anchor Framework v0.32.1
- SPL Token Program
- Associated Token Program
Checkout: escrow/
- Rust (latest stable version)
- Solana CLI v1.14+
- Anchor Framework v0.32.1 (for escrow program)
- Bun (for counter client)
- Node.js 18+ (alternative to Bun)
- Clone the repository:
git clone <repository-url>
cd solana-programs- Configure Solana CLI for your desired network:
# For devnet
solana config set --url devnet
# For localnet
solana config set --url localhostcd counter
cargo build-sbfThe compiled program will be in target/deploy/contracts.so
cd escrow
anchor buildThe compiled program will be in target/deploy/escrow.so
cd counter
solana program deploy target/deploy/contracts.socd escrow
anchor deployThe counter program includes a TypeScript client for testing:
cd counter/src/client
bun install
bun testcd escrow
anchor testsolana-programs/
├── counter/
│ ├── src/
│ │ ├── lib.rs # Program entry point
│ │ ├── programs/
│ │ │ └── main.rs # Counter logic implementation
│ │ └── client/ # TypeScript client
│ │ ├── index.ts # Client interface
│ │ └── tests/ # Client tests
│ └── Cargo.toml
├── escrow/
│ ├── programs/
│ │ └── escrow/
│ │ ├── src/
│ │ │ └── lib.rs # Escrow program logic
│ │ └── Cargo.toml
│ └── Cargo.toml # Workspace config
└── vault/ # In development
The counter program demonstrates native Solana program development without frameworks. It showcases:
- Custom instruction serialization using Borsh
- Account data management
- Signer validation
- State mutations
Instructions:
Init: Initializes counter to 1Double: Multiplies counter by 2Half: Divides counter by 2Add { amount }: Adds specified amountSubtract { amount }: Subtracts specified amount
The escrow program uses the Anchor framework to simplify development and implements:
- PDA-based vault authority for secure token custody
- SPL Token transfers using Cross-Program Invocations (CPI)
- Account validation using Anchor constraints
- Automatic space calculation for account creation
Instructions:
initialize_escrow: Creates escrow and deposits tokens into vaultclaim_escrow: Allows receiver to claim escrowed tokens
The counter program is written in pure Rust without frameworks, providing insight into low-level Solana program development.
Key concepts demonstrated:
- Program entrypoint definition
- Account iteration and validation
- Borsh serialization/deserialization
- Error handling with ProgramError
Built with Anchor, showcasing modern Solana development practices:
- Declarative account validation
- Automatic space calculation
- Type-safe instruction handlers
- Built-in security checks
- Counter: Implements saturating arithmetic to prevent overflow/underflow
- Escrow: Uses PDA for vault authority to prevent unauthorized access
- Escrow: Validates receiver using
has_oneconstraint - All programs require proper signer validation
The workspace is configured with release optimizations:
- Link-Time Optimization (LTO) enabled
- Single codegen unit for maximum optimization
- Overflow checks enabled in release builds