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.
- 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
The staking pool consists of two main contracts:
- Main contract handling staking logic
- Manages user stakes and reward calculations
- Controls fund distribution and pool operations
- Implements admin-only functions for pool management
- ERC20-compatible token for the staking pool
- Minted to users who participate in staking
- Used for reward distribution and governance
- Solidity: Smart contract development
- Foundry: Development framework and testing
- Forge: Testing and deployment tools
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
- Foundry
- Git
-
Clone the repository
git clone https://github.com/AbhiXverse/Staking_Pool_Contract.git cd Staking_Pool_Contract -
Install dependencies
forge install
-
Build the contracts
forge build
-
Run tests
forge test -vv
The project includes a comprehensive test suite covering all major functionalities:
| 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 |
| 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 |
# 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 coverageRan 17 tests for test/StakingPoolTest.sol:StakingPoolTest
β
All tests passed (17/17)
β± Total execution time: 794.12ms
π Gas usage optimized across all functions
-
Staking Tokens
// Approve tokens first stakingToken.approve(stakingPoolAddress, amount); // Stake tokens stakingPool.stake(amount);
-
Claiming Rewards
// Check available rewards uint256 rewards = stakingPool.getReward(userAddress); // Claim rewards stakingPool.claimReward();
-
Unstaking
// Unstake tokens stakingPool.unstake(amount);
-
Fund the Pool
// Fund pool with reward tokens stakingPool.fundPool(rewardAmount);
-
Mint Pool Tokens
// Mint tokens to users poolToken.mint(userAddress, amount);
- 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
| 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 |
| 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 |
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
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