Skip to content

soladdev/solana-staking-contract

Repository files navigation

Solana Staking Contract

A production-ready, Anchor-based staking program for the Solana blockchain with configurable rewards, multi-tier referral system, and flexible admin controls.


Features

  • Token Staking — Stake SPL tokens with configurable minimum amount and lock-up period
  • Progressive Rewards — Monthly reward percentages that increase over time (up to 5 years)
  • 3-Tier Referral System — Earn 5%, 3%, and 2% on referred users' stakes (direct, second, third referrer)
  • Admin Controls — Pause staking/withdrawals, adjust parameters, manage vault liquidity
  • PDAs & Security — Program Derived Addresses for vault and state isolation

Architecture

┌─────────────────────────────────────────────────────────────────┐
│                     Staking Contract (Anchor)                     │
├─────────────────────────────────────────────────────────────────┤
│  GlobalState (PDA)          │  StakingState (per-user PDA)       │
│  • Admin, Mint, Vault       │  • Owner, stakes[], referrers      │
│  • Reward params            │  • 60 monthly stake entries        │
│  • Stake/Withdraw toggles   │  • Referral earnings               │
└─────────────────────────────────────────────────────────────────┘

Tech Stack

Component Technology
Framework Anchor 0.31.1
Language Rust
Blockchain Solana
Client TypeScript, @coral-xyz/anchor, @solana/spl-token

Prerequisites


Installation

# Clone the repository
git clone https://github.com/vladmeer/staking-sc.git
cd Staking-Contract

# Install dependencies
yarn install

# Build the program
anchor build

Configuration

Edit Anchor.toml to set:

Key Description
[programs.localnet] Program ID for local deployment
[provider] RPC URL and wallet path
cluster devnet, mainnet, or local RPC

Build & Deploy

# Build
anchor build

# Deploy to configured cluster
anchor deploy

# Run tests
anchor test
# or
yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts

Program Instructions

User Instructions

Instruction Description
stake Stake tokens. Optionally pass referrer staking states for referral rewards.
user_withdraw Withdraw staked tokens after the minimum lock-up period.
refer Register a referred user (admin-only). Links referrer → referred user.

Admin Instructions

Instruction Description
initialize Initialize the program with mint, min stake, period, and reward params.
change_admin Transfer admin authority.
change_stake_period Update minimum stake period (months).
change_min_stake_amount Update minimum stake amount.
change_reward_start_month Month when rewards begin.
change_reward_start_percent Initial reward percentage.
change_reward_increase_percent_per_month Monthly reward increase.
change_stake_status Enable/disable new stakes.
change_withdraw_status Enable/disable withdrawals.
admin_deposit Add tokens to the vault.
admin_withdraw Remove tokens from the vault.

Account Structure

GlobalState (PDA: ["GLOBAL_SEED"])

  • admin — Program admin
  • mint — Staking token mint
  • total_staked_amount, total_staked_num
  • reward_start_month, reward_start_percent, reward_increase_percent_per_month
  • minimum_stake_amount, minimum_stake_period
  • stake_enabled, withdraw_status

StakingState (PDA: ["STAKE_SEED", owner])

  • owner — Staker
  • stakes[60] — Per-month stake entries (amount, stake_num)
  • first_referrer, second_referrer, third_referrer
  • total_staked_amount, withdrawn_amount

Referral System

  • Direct referrer: 5% of referred user's stake
  • Second referrer: 3% of referred user's stake
  • Third referrer: 2% of referred user's stake

Referrers must have an active staking position. Referrals are registered via refer (admin) before the referred user stakes.


Project Structure

Staking-Contract/
├── programs/
│   └── staking-contract/
│       └── src/
│           ├── lib.rs              # Program entry
│           ├── state.rs            # Account structs
│           ├── constant.rs         # Seeds, limits
│           ├── errors.rs           # Custom errors
│           ├── utils.rs            # Rewards, time helpers
│           └── instructions/       # Instruction handlers
├── tests/
│   └── staking-contract.ts
├── migrations/
│   └── deploy.ts
├── Anchor.toml
└── package.json

Testing

Tests use Mocha and Chai. Configure keys/admin.json and keys/user.json for test signers.

anchor test

License

ISC

About

A production-ready, Anchor-based staking program for the Solana blockchain with configurable rewards, multi-tier referral system, and flexible admin controls.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors