Skip to content

AbhiXverse/Staking_Pool_Contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Staking Pool Smart Contract

A decentralized staking pool smart contract built with Solidity that allows users to stake tokens, earn rewards over time, and manage their staking positions efficiently.

πŸš€ Features

  • Token Staking: Users can stake their tokens to earn rewards
  • Reward Distribution: Automatic reward calculation based on staking duration and amount
  • Pool Token System: Mint pool-specific tokens for staking participants
  • Admin Controls: Administrative functions for pool management and funding
  • Secure Operations: Built-in security measures and access controls
  • Time-based Rewards: Rewards accumulate over time based on staking duration

πŸ“‹ Contract Overview

The staking pool consists of two main contracts:

StakingPool Contract

  • Main contract handling staking logic
  • Manages user stakes and reward calculations
  • Controls fund distribution and pool operations
  • Implements admin-only functions for pool management

PoolToken Contract

  • ERC20-compatible token for the staking pool
  • Minted to users who participate in staking
  • Used for reward distribution and governance

πŸ›  Technology Stack

  • Solidity: Smart contract development
  • Foundry: Development framework and testing
  • Forge: Testing and deployment tools

πŸ“ Project Structure

staking-pool/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ StakingPool.sol      # Main staking contract
β”‚   └── PoolToken.sol        # Pool token contract
β”œβ”€β”€ test/
β”‚   └── StakingPoolTest.sol  # Comprehensive test suite
β”œβ”€β”€ script/
β”‚   └── Deploy.s.sol         # Deployment scripts
β”œβ”€β”€ foundry.toml             # Foundry configuration
└── README.md               # This documentation

πŸ”§ Installation & Setup

Prerequisites

Installation Steps

  1. Clone the repository

    git clone https://github.com/AbhiXverse/Staking_Pool_Contract.git
    cd Staking_Pool_Contract
  2. Install dependencies

    forge install
  3. Build the contracts

    forge build
  4. Run tests

    forge test -vv

πŸ§ͺ Testing

The project includes a comprehensive test suite covering all major functionalities:

Test Coverage

Test Case Description Status
testInitialState() Verifies contract initialization βœ… PASS
testStaking() Tests basic staking functionality βœ… PASS
testUnstaking() Tests unstaking mechanism βœ… PASS
testClaimReward() Tests reward claiming βœ… PASS
testRewardAccumulation() Verifies reward calculation βœ… PASS
testRewardCalculationOverTime() Tests time-based rewards βœ… PASS
testMultipleUsers() Tests multi-user scenarios βœ… PASS
testStakingPoolFunding() Tests pool funding mechanism βœ… PASS
testPoolTokenMinting() Tests token minting βœ… PASS
testOnlyAdminCanFundPool() Tests admin-only functions βœ… PASS
testGetContractInfo() Tests contract information retrieval βœ… PASS
testTokenTransfer() Tests token transfer functionality βœ… PASS
testTokenApproveAndTransferFrom() Tests token approval mechanism βœ… PASS

Error Handling Tests

Test Case Description Status
testRevertWhenStakeZeroAmount() Prevents zero-amount staking βœ… PASS
testRevertWhenUnstakeWithoutStaking() Prevents unstaking without stake βœ… PASS
testRevertWhenClaimRewardWithoutStaking() Prevents reward claims without stake βœ… PASS
testRevertWhenNonOwnerMinting() Prevents unauthorized minting βœ… PASS

Running Tests

# Run all tests with verbose output
forge test -vv

# Run specific test
forge test --match-test testStaking -vv

# Run tests with gas reporting
forge test --gas-report

# Run tests with coverage
forge coverage

Latest Test Results

Ran 17 tests for test/StakingPoolTest.sol:StakingPoolTest
βœ… All tests passed (17/17)
⏱ Total execution time: 794.12ms
πŸ“Š Gas usage optimized across all functions

πŸ“– Usage Guide

For Users

  1. Staking Tokens

    // Approve tokens first
    stakingToken.approve(stakingPoolAddress, amount);
    
    // Stake tokens
    stakingPool.stake(amount);
  2. Claiming Rewards

    // Check available rewards
    uint256 rewards = stakingPool.getReward(userAddress);
    
    // Claim rewards
    stakingPool.claimReward();
  3. Unstaking

    // Unstake tokens
    stakingPool.unstake(amount);

For Administrators

  1. Fund the Pool

    // Fund pool with reward tokens
    stakingPool.fundPool(rewardAmount);
  2. Mint Pool Tokens

    // Mint tokens to users
    poolToken.mint(userAddress, amount);

πŸ”’ Security Features

  • Access Control: Admin-only functions protected
  • Input Validation: All inputs validated for security
  • Reentrancy Protection: Protected against reentrancy attacks
  • Safe Math: Uses OpenZeppelin's safe math operations
  • Zero Amount Checks: Prevents zero-value operations

πŸ“Š Contract Functions

StakingPool Main Functions

Function Access Description
stake(uint256 amount) Public Stake tokens to earn rewards
unstake(uint256 amount) Public Withdraw staked tokens
claimReward() Public Claim accumulated rewards
getReward(address user) View Get pending rewards for user
fundPool(uint256 amount) Admin Add funds to reward pool

PoolToken Functions

Function Access Description
mint(address to, uint256 amount) Owner Mint new tokens
transfer(address to, uint256 amount) Public Transfer tokens
approve(address spender, uint256 amount) Public Approve token spending

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ† Test Results Summary

Ran 17 tests for test/StakingPoolTest.sol:StakingPoolTest
[PASS] testClaimReward() (gas: 174896)
[PASS] testGetContractInfo() (gas: 123200)
[PASS] testInitialState() (gas: 32797)
[PASS] testMultipleUsers() (gas: 219312)
[PASS] testOnlyAdminCanFundPool() (gas: 45208)
[PASS] testPoolTokenMinting() (gas: 33016)
[PASS] testRevertWhenClaimRewardWithoutStaking() (gas: 60847)
[PASS] testRevertWhenNonOwnerMinting() (gas: 14953)
[PASS] testRevertWhenStakeZeroAmount() (gas: 65518)
[PASS] testRevertWhenUnstakeWithoutStaking() (gas: 36492)
[PASS] testRewardAccumulation() (gas: 124532)
[PASS] testRewardCalculationOverTime() (gas: 139221)
[PASS] testStaking() (gas: 123538)
[PASS] testStakingPoolFunding() (gas: 50577)
[PASS] testTokenApproveAndTransferFrom() (gas: 53943)
[PASS] testTokenTransfer() (gas: 35946)
[PASS] testUnstaking() (gas: 186928)

Suite result: βœ… ok. 17 passed; 0 failed; 0 skipped
Total time: 36.72ms (28.08ms CPU time)

Made with ❀️ by Abhi

About

A decentralized staking pool smart contract built with Solidity that allows users to stake tokens, earn rewards over time, and manage their staking positions efficiently.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors