You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
A beginner-friendly template for building applications (dApps) on Solana using Anchor and Next.js. This template implements a counter program that demonstrates essential Solana development concepts including PDAs (Program Derived Addresses), CPIs (Cross-Program Invocations), and state management.
demo.mp4
To try the demo, make sure to use a test wallet connected to devnet.
Phantom
Solflare
phantom.mp4
solflare.mp4
This template is for educational purposes and set up for devnet use only.
🎓 Educational Purpose
This template is designed for developers who want to learn:
How to build Solana programs using the Anchor framework
How to work with PDAs for state management and program signing
How to perform Cross-Program Invocations (CPIs)
How to create frontends that interact with Solana programs
How to handle wallet connections and transactions on a frontend
📝 Program Overview
The Solana program in this template demonstrates several core concepts through a simple counter application:
Program Derived Addresses (PDAs)
Counter PDA
Stores the counter's current value
Derived using the seed "counter"
Global state accessible to all users
Automatically initialized on first increment
Vault PDA
Holds SOL tokens from user transactions
Derived using:
Seed "vault"
User's public key
Each user gets their own vault
Demonstrates using PDAs for CPI signing
Instructions
Increment
Increases counter value by 1
Performs CPI to transfer 0.001 SOL from user to vault
Creates counter PDA if it doesn't exist
Demonstrates:
PDA initialization
System program CPI for SOL transfer
State management
Decrement
Decreases counter value by 1
Performs CPI to transfer 0.001 SOL from vault back to user
Demonstrates:
PDA signing (vault)
System program CPI with PDA as signer
Cross-Program Invocations (CPIs)
The program demonstrates CPIs through SOL transfers:
User → Vault (increment): Basic CPI to system program
Vault → User (decrement): CPI with PDA signing
🏗 Project Structure
├── program/ # Solana program (smart contract)│ ├── programs/ # Program source code│ ├── tests/ # Program tests│ └── Anchor.toml # Anchor configuration│└── frontend/ # Next.js frontend ├── app/ # app router page and layout ├── components/ # React components └── anchor-idl/ # Program IDL
🔧 Core Features
Solana Program
Counter state management using PDA
Vault system using user-specific PDAs
SOL transfer demonstration using CPIs
PDA initialization and signing
Frontend Application
Wallet adapter integration
Real-time counter updates
Transaction toast notifications
UI with Tailwind CSS and shadcn/ui
🚀 Getting Started
Prerequisites
Node.js 18+ and pnpm
Rust and Solana CLI tools
Anchor Framework
Installation
Clone the repository:
git clone<repository-url>
Install program dependencies:
cd programpnpm installanchor buildanchor keys syncanchor buildanchor deploy
IfOperation timed out happens duringanchor deploy, you can also use other RPC endpoint by passing--provider.cluster <your-custom-rpc> or you can overrideprovider.cluster inAnchor.toml
Install frontend dependencies:
cd frontendpnpm installcp ../program/idl/counter.json frontend/anchor-idl/idl.jsoncp ../program/target/types/counter.ts frontend/anchor-idl/idl.tspnpm dev
Actioncp is important, so that you won't access the program others may have deployed. You'll notice that if your initial counter value is not null or zero.
Development
Test the program:
cd programanchortest
Run the frontend:
cd frontendpnpm dev
💡 Learning Resources
Program (Smart Contract)
program/programs/counter/src/lib.rs: Core program logic
Instruction handling
PDA creation and management
CPI implementation
Frontend Components
frontend/components/counter/: Main dApp components