A decentralized staking platform built on Solana blockchain that allows users to stake tokens and earn rewards. This project consists of a Rust smart contract (program) and a TypeScript interface for interacting with the blockchain.
- Token Staking: Users can stake their tokens to earn rewards
- Reward Distribution: Automated reward distribution based on staking duration and amount
- Pool Management: Initialize and manage staking pools with configurable parameters
- User Management: Create user accounts and track individual staking positions
- Flexible Unstaking: Partial and complete unstaking options
- Reward Claims: Users can claim accumulated rewards
- Pool Closure: Administrators can close pools and return funds
The core staking logic is implemented in Rust as a Solana program:
- Entrypoint: Program entry point for processing instructions
- Instructions: Defines all supported operations (stake, unstake, claim rewards, etc.)
- Processor: Implements the business logic for each instruction
- State: Defines data structures for pools, users, and program state
- Error Handling: Custom error types for better debugging
A TypeScript library for interacting with the smart contract:
- Connection Service: Manages Solana network connections
- Transaction Builders: Creates and sends transactions to the blockchain
- Data Models: Type-safe interfaces for program data
- Utilities: Helper functions for common operations
- Rust: Latest stable version (1.70+)
- Solana CLI: Latest stable version
- Node.js: Version 16.0.0 or higher
- TypeScript: Version 4.4.2 or higher
git clone <repository-url>
cd Rust-contract-stackingcargo buildcd interface
npm install
npm run build# From project root
./deploy-localnet.shThe program supports the following instructions:
-
InitializePool: Create a new staking pool
reward_duration: Duration of the reward periodpool_nonce: Unique identifier for the poolfund_amount: Initial funding amount
-
CreateUser: Create a user account for staking
nonce: Unique identifier for the user
-
Stake: Stake tokens in the pool
amount_to_deposit: Amount of tokens to stake
-
Unstake: Withdraw staked tokens
amount_to_withdraw: Amount of tokens to withdraw
-
ClaimRewards: Claim accumulated rewards
-
ClosePool: Close the staking pool (admin only)
-
CloseUser: Close a user account
-
FinalUnstake: Final unstaking operation
import { ConnectionService } from '@your-staking/blockchain-interface';
// Initialize connection
const connection = new ConnectionService('devnet');
// Create staking transaction
const stakeTx = await createStakeTransaction({
amount: 1000,
userAccount: userPubkey,
poolAccount: poolPubkey
});
// Send transaction
const signature = await connection.sendTransaction(stakeTx);cargo testcd interface
npm test./test.shThe interface supports multiple Solana networks:
mainnet-beta: Production networkdevnet: Development networktestnet: Test networklocalnet: Local development network
- Your Token Decimals: 9
- Max Supply: 1,000,000,000 tokens
- Reward Token Decimals: 9
createStakeTransaction(): Build stake transactioncreateUnstakeTransaction(): Build unstake transactioncreateClaimRewardsTransaction(): Build claim rewards transactioncreateInitializePoolTransaction(): Build pool initialization transactioncreateUserTransaction(): Build user creation transaction
PoolInfo: Pool information and configurationUserInfo: User staking position and rewardsStakingInstructions: Staking operation parameters
The program includes comprehensive error handling with custom error types:
InvalidInstruction: Invalid instruction dataInsufficientFunds: Insufficient balance for operationPoolNotFound: Pool account not foundUserNotFound: User account not foundInvalidPoolState: Pool in invalid state for operation
- All operations are validated on-chain
- User accounts are protected by PDA derivation
- Pool operations require proper authorization
- Funds are secured by Solana's account model
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the ISC License - see the LICENSE file for details.
For support and questions:
- Create an issue in the repository
- Check the test files for usage examples
- Review the TypeScript interface documentation
- v0.1.0: Initial release with core staking functionality
- Basic staking and unstaking operations
- Reward distribution system
- Pool and user management
- TypeScript interface library
Note: This is a development version. Use at your own risk and ensure thorough testing before deploying to production networks.