Skip to content

swaploard/staking

Repository files navigation

Solana Staking Platform

A comprehensive staking platform built on the Solana blockchain, enabling users to stake SOL tokens, earn rewards, and manage staking positions through a modern web interface.

Table of Contents

Project Overview

This project is a complete staking ecosystem consisting of three main components:

  1. Smart Contracts - Rust/Anchor programs that manage on-chain staking logic
  2. Indexer - A reliable off-chain service that synchronizes blockchain state with a PostgreSQL database
  3. Web Client - A Next.js frontend for users to interact with staking pools and manage positions

Key Features

  • Multiple Staking Pools - Support for different staking pools with varying APY rates and terms
  • Real-time Data - Indexed blockchain state for instant data queries
  • Position Management - Track and manage active staking positions
  • Reward Tracking - Monitor pending and claimed rewards
  • Responsive UI - Desktop and mobile optimized interface
  • Wallet Integration - Support for popular Solana wallets (Phantom, Solflare, etc.)
  • Exactly-Once Processing - Guaranteed reliable transaction ingestion with recovery mechanisms

Tech Stack

Frontend

  • Framework: Next.js 16+ with React 19+
  • UI Components: Radix UI
  • Styling: TailwindCSS with PostCSS
  • Web3: Coral-xyz Anchor SDK, Solana Web3.js
  • Wallet Adapter: Solana Wallet Adapter React

Backend/Indexer

  • Language: TypeScript with Node.js
  • Runtime: tsx for development
  • Database: PostgreSQL with Prisma ORM
  • Blockchain Interaction: Coral-xyz Anchor, Solana Web3.js
  • Logging: Pino logger
  • Architecture: Background job-based with RPC failover and retry mechanisms

Smart Contracts

  • Language: Rust
  • Framework: Anchor (Solana program development framework)
  • SPL Tokens: Solana Program Library for token operations
  • Compilation: Solana BPF (sBPF)

Build & Tooling

  • Package Manager: Yarn/pnpm
  • Rust Toolchain: Managed via rust-toolchain.toml
  • Testing: Mocha + TypeScript, ts-mocha
  • Code Quality: Prettier for formatting

Architecture

High-Level Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Solana Blockchain                        β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚ Staking Smart Contracts (Anchor Program)             β”‚   β”‚
β”‚  β”‚ - Pool creation & management                         β”‚   β”‚
β”‚  β”‚ - Stake deposits & withdrawals                       β”‚   β”‚
β”‚  β”‚ - Reward calculations & distributions                β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β–²                                           β”‚
         β”‚ RPC Calls                                 β”‚ On-chain Events
         β”‚                                           β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚          Indexer Service (Node.js/TS)              β”‚
    β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
    β”‚ β”‚ Components:                                    β”‚ β”‚
    β”‚ β”‚ - Account Sync Job                             β”‚ β”‚
    β”‚ β”‚ - Instruction Ingestion                        β”‚ β”‚
    β”‚ β”‚ - Gap Filler                                   β”‚ β”‚
    β”‚ β”‚ - Finalization & Reconciliation                β”‚ β”‚
    β”‚ β”‚ - RPC Failover & Retry Logic                   β”‚ β”‚
    β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ Prisma ORM
         β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚     PostgreSQL Database                            β”‚
    β”‚ - Pool data & state                                β”‚
    β”‚ - User positions & balances                        β”‚
    β”‚ - Transaction history                              β”‚
    β”‚ - Account information                              β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β–² SQL Queries
         β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚    Web Client (Next.js + React)                    β”‚
    β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
    β”‚ β”‚ Pages:                                         β”‚ β”‚
    β”‚ β”‚ - Dashboard (overview & balance)               β”‚ β”‚
    β”‚ β”‚ - Pools (discover & browse)                    β”‚ β”‚
    β”‚ β”‚ - Positions (manage stakes)                    β”‚ β”‚
    β”‚ β”‚ - Fund Rewards (admin interface)               β”‚ β”‚
    β”‚ β”‚ - Transaction History                          β”‚ β”‚
    β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
    β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
    β”‚ β”‚ Features:                                      β”‚ β”‚
    β”‚ β”‚ - Wallet connection & signing                  β”‚ β”‚
    β”‚ β”‚ - Real-time data updates                       β”‚ β”‚
    β”‚ β”‚ - State management & hooks                     β”‚ β”‚
    β”‚ β”‚ - Responsive UI components                     β”‚ β”‚
    β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Indexer Reliability Features

The indexer implements several reliability mechanisms:

  • Exactly-Once Processing: Claim-first commit protocol with PostgreSQL unique constraints
  • Crash Recovery: Timeout-based recovery with automatic lock resets
  • RPC Failover: Rotating fallbacks with exponential backoff for 429 and 5xx errors
  • Component-Based Jobs: Modular architecture with separate jobs for different sync tasks

Project Structure

.
β”œβ”€β”€ programs/staking/              # Smart contracts
β”‚   β”œβ”€β”€ Cargo.toml                 # Rust package configuration
β”‚   └── src/                       # Contract source code
β”‚
β”œβ”€β”€ indexer/                       # Off-chain data indexer
β”‚   β”œβ”€β”€ package.json               # Node.js dependencies
β”‚   β”œβ”€β”€ prisma/
β”‚   β”‚   β”œβ”€β”€ schema.prisma          # Database schema
β”‚   β”‚   └── migrations/            # Database migrations
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ index.ts               # Entry point
β”‚   β”‚   β”œβ”€β”€ logger.ts              # Logging setup
β”‚   β”‚   β”œβ”€β”€ alerts/                # Alert mechanisms
β”‚   β”‚   β”œβ”€β”€ ingestion/             # Data ingestion jobs
β”‚   β”‚   β”œβ”€β”€ jobs/                  # Background jobs
β”‚   β”‚   β”œβ”€β”€ program/               # Program interaction logic
β”‚   β”‚   └── rpc/                   # RPC client & failover
β”‚   └── README.md                  # Indexer-specific docs
β”‚
β”œβ”€β”€ staking_client/                # Next.js web frontend
β”‚   β”œβ”€β”€ package.json               # React dependencies
β”‚   β”œβ”€β”€ tsconfig.json              # TypeScript config
β”‚   β”œβ”€β”€ next.config.mjs            # Next.js configuration
β”‚   β”œβ”€β”€ app/                       # Next.js App Router
β”‚   β”‚   β”œβ”€β”€ layout.tsx             # Root layout
β”‚   β”‚   β”œβ”€β”€ page.tsx               # Home page
β”‚   β”‚   β”œβ”€β”€ api/                   # API routes
β”‚   β”‚   β”œβ”€β”€ pools/                 # Pools page & routes
β”‚   β”‚   β”œβ”€β”€ create_pool/           # Pool creation
β”‚   β”‚   └── fund-rewards/          # Reward funding
β”‚   β”œβ”€β”€ components/                # Reusable React components
β”‚   β”‚   β”œβ”€β”€ common/                # Common components
β”‚   β”‚   β”œβ”€β”€ dashboard/             # Dashboard components
β”‚   β”‚   β”œβ”€β”€ ui/                    # UI primitives
β”‚   β”‚   β”œβ”€β”€ providers/             # Context providers
β”‚   β”‚   └── actions/               # Server actions
β”‚   β”œβ”€β”€ hooks/                     # Custom React hooks
β”‚   β”‚   β”œβ”€β”€ usePools.ts            # Pool data fetching
β”‚   β”‚   β”œβ”€β”€ useUserPositions.ts    # User positions
β”‚   β”‚   β”œβ”€β”€ useWalletActivity.ts   # Activity tracking
β”‚   β”‚   └── use-mobile.ts          # Responsive design
β”‚   β”œβ”€β”€ lib/                       # Utility functions
β”‚   β”‚   β”œβ”€β”€ anchor.ts              # Anchor setup
β”‚   β”‚   β”œβ”€β”€ types.ts               # TypeScript types
β”‚   β”‚   β”œβ”€β”€ utils.ts               # Helpers
β”‚   β”‚   β”œβ”€β”€ instructions/          # Transaction builders
β”‚   β”‚   β”œβ”€β”€ adapters/              # Data adapters
β”‚   β”‚   └── hooks/                 # Library hooks
β”‚   β”œβ”€β”€ prisma/
β”‚   β”‚   └── schema.prisma          # Client-side database schema
β”‚   β”œβ”€β”€ public/                    # Static assets
β”‚   β”œβ”€β”€ styles/                    # Global styles
β”‚   └── README.md                  # Client-specific docs
β”‚
β”œβ”€β”€ tests/                         # Integration tests
β”‚   β”œβ”€β”€ staking.ts                 # Main staking tests
β”‚   └── create_pool.ts             # Pool creation tests
β”‚
β”œβ”€β”€ trident-tests/                 # Fuzz testing with Trident
β”‚   β”œβ”€β”€ Cargo.toml
β”‚   β”œβ”€β”€ Trident.toml
β”‚   └── fuzz_0/                    # Fuzz test programs
β”‚
β”œβ”€β”€ migrations/                    # Migration scripts
β”‚   └── deploy.ts                  # Deployment utilities
β”‚
β”œβ”€β”€ scripts/                       # Utility scripts
β”‚   └── global-config.ts           # Global configuration
β”‚
β”œβ”€β”€ target/                        # Build artifacts
β”‚   β”œβ”€β”€ debug/                     # Debug builds
β”‚   β”œβ”€β”€ release/                   # Release builds
β”‚   β”œβ”€β”€ idl/                       # Interface Definition Language files
β”‚   β”œβ”€β”€ types/                     # Generated TypeScript types
β”‚   └── sbpf-solana-solana/        # Solana BPF compilation
β”‚
β”œβ”€β”€ test-ledger/                   # Local validator data (gitignored)
β”‚   β”œβ”€β”€ faucet-keypair.json
β”‚   β”œβ”€β”€ validator-keypair.json
β”‚   β”œβ”€β”€ accounts/                  # Validator account data
β”‚   β”œβ”€β”€ rocksdb/                   # Local ledger state
β”‚   └── snapshots/                 # State snapshots
β”‚
β”œβ”€β”€ Cargo.toml                     # Workspace root
β”œβ”€β”€ Anchor.toml                    # Anchor configuration
β”œβ”€β”€ package.json                   # Root package config
β”œβ”€β”€ tsconfig.json                  # TypeScript configuration
β”œβ”€β”€ rust-toolchain.toml            # Rust version
└── README.md                      # This file

How It Works

1. User Interaction Flow

User connects wallet
        ↓
Browse available staking pools
        ↓
Stake SOL tokens in a pool
        ↓
Staking program processes transaction
        ↓
Indexer captures on-chain event
        ↓
Database updates with new position
        ↓
Dashboard reflects updated balance & rewards
        ↓
User can view activity history & claim rewards

2. Data Flow

  1. Transaction Submission: User connects wallet and submits a staking transaction (deposit, withdraw, claim)
  2. On-Chain Execution: Anchor smart contract validates and executes the transaction
  3. Event Emission: Program emits events containing transaction details
  4. Indexer Ingestion: Indexer service captures events via RPC calls
  5. Database Sync: Prisma ORM stores/updates data in PostgreSQL
  6. Frontend Query: Next.js app queries database via API routes or direct DB connection
  7. UI Update: React components display updated staking positions and rewards

3. Key Components

Smart Contracts (programs/staking/)

  • Define pool structure and rules
  • Validate stake/unstake operations
  • Calculate and distribute rewards
  • Maintain global state

Indexer (indexer/)

  • Continuously polls blockchain for new transactions
  • Extracts transaction data and account information
  • Handles network interruptions and recovery
  • Maintains exactly-once processing guarantee
  • Provides fast query API for frontend

Web Client (staking_client/)

  • User-friendly interface for staking operations
  • Real-time dashboard with balance and rewards
  • Pool discovery and comparison
  • Transaction history and status tracking

πŸš€ Getting Started

Prerequisites

  • Node.js 18+ (for indexer and client)
  • Rust 1.70+ (for smart contracts)
  • Solana CLI tools
  • PostgreSQL 13+ (for indexer)
  • A Solana wallet with devnet SOL (for testing)

Installation

  1. Clone the repository

    cd /path/to/staking
  2. Install root dependencies

    yarn install
  3. Setup Smart Contracts

    cd programs/staking
    cargo build
  4. Setup Indexer

    cd indexer
    npm install
    npm run db:migrate
    npm run dev
  5. Setup Web Client

    cd staking_client
    npm install
    npm run dev

Configuration

  • Anchor.toml: Specifies program IDs, RPC endpoints, and wallet location
  • indexer/.env: Database connection string and RPC endpoints
  • staking_client/.env.local: API endpoints and feature flags

Development

Build Smart Contracts

yarn build

Run Tests

yarn test              # Run all tests
yarn test tests/staking.ts    # Run specific test

Start Development Services

Terminal 1 - Local Validator

anchor localnet

Terminal 2 - Indexer

cd indexer
npm run dev

Terminal 3 - Web Client

cd staking_client
npm run dev

Deployment

  • Devnet: anchor deploy --provider.cluster devnet
  • Production: Follow deployment guides in respective component directories

Database Schema

The indexer uses Prisma to manage a PostgreSQL database with tables for:

  • Staking pools and their configurations
  • User staking positions and balances
  • Transaction history and events
  • Account state and metadata

See indexer/prisma/schema.prisma for the complete schema.

Security

  • Smart contracts use Anchor framework's built-in security checks
  • Indexer implements exactly-once processing to prevent data corruption
  • Private keys are managed via Solana CLI
  • All transactions require wallet signatures

License

ISC


For more detailed information, refer to the README files in individual component directories:

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors