The Anchor Staking Program is a Solana-based decentralized staking protocol built using the Anchor framework. This program enables users to stake NFTs from verified collections and earn rewards based on configurable parameters. The protocol implements a robust staking mechanism with freeze periods, reward distribution, and comprehensive state management.
Traditional staking mechanisms often lack flexibility and transparency, particularly when dealing with NFT-based assets. Users need a secure, verifiable way to stake their NFTs while maintaining control over their assets and earning predictable rewards. Additionally, protocol administrators require granular control over staking parameters without compromising security or decentralization.
This staking program addresses these challenges by providing:
- NFT Collection Verification: Ensures only NFTs from verified collections can be staked through on-chain metadata validation
- Configurable Reward System: Flexible points-per-stake mechanism with customizable freeze periods
- Secure State Management: Program-derived addresses (PDAs) ensure secure storage of configuration and user data
- Transparent Reward Distribution: On-chain reward minting with deterministic calculation
- User-Centric Design: Individual user accounts track staking history and accumulated rewards
-
StakeConfig: Global configuration account storing staking parameters
- Points per stake
- Maximum stake limit
- Freeze period duration
- Reward mint authority
-
User Accounts: Individual accounts tracking user-specific staking data
-
Stake Accounts: Records for individual staking positions
- Initialize Config: Sets up the global staking configuration with custom parameters
- Initialize User: Creates user-specific accounts for tracking stakes and rewards
- Stake: Handles NFT staking with collection verification and metadata validation
- Collection verification through Metaplex metadata program
- Master edition validation to prevent duplicate staking
- PDA-based authority for reward minting
- Freeze period enforcement to prevent gaming
- Associated token account validation
Before setting up the project, ensure you have the following installed:
- Rust: v1.70.0 or higher
- Solana CLI: v1.18.0 or higher
- Anchor CLI: v0.31.0
- Node.js: v18.0.0 or higher
- Yarn: v1.22.0 or higher
git clone https://github.com/ANISH-SR/anchor_staking.git
cd anchor_staking# Install Node.js dependencies
yarn install
# Build the Anchor program
anchor build# Set cluster to localnet for development
solana config set --url localhost
# Create a new keypair if needed
solana-keygen new# In a separate terminal, start the local Solana validator
solana-test-validator# Deploy to localnet
anchor deploy
# The program ID will be displayed after deployment
# Update Anchor.toml and lib.rs with the new program ID if neededExecute the test suite to verify the program functionality:
anchor testFor running tests with verbose output:
yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.tsThe program exposes the following instructions:
await program.methods
.initializeConfig(pointsPerStake, maxStake, freezePeriod)
.accounts({
user: userPublicKey,
mint: mintPublicKey,
collectionMint: collectionMintPublicKey,
// ... other accounts
})
.rpc();await program.methods
.initializeUser()
.accounts({
user: userPublicKey,
// ... other accounts
})
.rpc();await program.methods
.stake()
.accounts({
user: userPublicKey,
mint: nftMintPublicKey,
// ... other accounts
})
.rpc();anchor_staking/
├── programs/
│ └── staking/
│ ├── src/
│ │ ├── instructions/
│ │ │ ├── initialize_config.rs
│ │ │ ├── initialize_user.rs
│ │ │ ├── stake.rs
│ │ │ └── mod.rs
│ │ ├── state/
│ │ │ ├── stake_config.rs
│ │ │ ├── stake_account.rs
│ │ │ ├── user_account.rs
│ │ │ └── mod.rs
│ │ └── lib.rs
│ └── Cargo.toml
├── tests/
│ └── staking.ts
├── migrations/
├── Anchor.toml
├── Cargo.toml
├── package.json
└── README.md
The Anchor.toml file contains the project configuration:
- Cluster: Set to
localnetfor development - Wallet: Points to your Solana keypair
- Program ID:
5cEEs947E9a2TCoutXHV3ZLRt12MYJs7sx4TyrBREpgx
For production deployment, configure the following:
export ANCHOR_PROVIDER_URL=<your-rpc-url>
export ANCHOR_WALLET=<path-to-keypair>anchor buildanchor build
# Types are automatically generated in target/types/# Check formatting
yarn lint
# Fix formatting issues
yarn lint:fixThe test suite covers:
- Configuration initialization with various parameters
- User account creation and management
- NFT staking with collection verification
- Reward calculation and distribution
- Edge cases and error handling
# Configure for devnet
solana config set --url devnet
# Airdrop SOL for deployment
solana airdrop 2
# Deploy
anchor deploy --provider.cluster devnet# Configure for mainnet
solana config set --url mainnet-beta
# Deploy with your funded keypair
anchor deploy --provider.cluster mainnet-beta- All accounts use PDA derivation for deterministic addressing
- Collection verification prevents unauthorized NFT staking
- Freeze periods prevent rapid stake/unstake cycles
- Token account validation ensures proper ownership
- Metadata program integration provides cryptographic proof of collection membership
Contributions are welcome. Please follow these guidelines:
- Fork the repository
- Create a feature branch
- Commit your changes with clear messages
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
ISC
For questions or support, please open an issue on the GitHub repository.
Built with the Anchor framework and Solana blockchain technology. Special thanks to the Solana and Anchor communities for their comprehensive documentation and support.