A secure ERC20 token staking protocol with dynamic reward calculation
Staking Protocol allows users to stake their ERC20 tokens and earn rewards over time. The protocol includes security measures like pausing capabilities, reentrancy protection, rate limiting, and owner-only administrative functions.
- Staking & Unstaking: Securely stake and unstake ERC20 tokens
- Reward System: Earn rewards at a rate of approximately 3% APY
- Dynamic Rewards: Owner can adjust reward rates to accommodate market conditions
- Emergency Controls: Contract can be paused by owner in case of emergencies
- Security Measures: Implements reentrancy guards, rate limiting, and other protections
- Time-based Rewards: Rewards calculated based on time staked using block timestamps
The protocol consists of the following components:
StakingContract.sol: Core staking logic with security featuresTokenERC20.sol: ERC20 token implementation for staking
Rewards are calculated using the formula:
rewards = stakedAmount * rewardRate * timeElapsed / BASIS_POINTS
Where:
- Current
rewardRateis 1,000,000,000 (configurable by owner) BASIS_POINTSis 10^18timeElapsedis calculated using block timestamps (current timestamp - last calculation timestamp)- This results in approximately 3% APY
This protocol implements multiple security features:
- OpenZeppelin's Pausable, ReentrancyGuard, and Ownable patterns
- SafeERC20 for secure token transfers
- Minimum staking amount requirements (configurable at deployment)
- Rate limiting to prevent flash loan attacks (1 hour between operations)
- Reserve balance checks to ensure contract solvency
- Input validation for all user interactions
- Comprehensive event logging for all important operations
- Emergency pause functionality for contract owner
- Solidity 0.8.28
- Foundry (for testing and deployment)
Built with 5.0.1 version of OpenZeppelin's libraries
To install this version, you can run:
forge install OpenZeppelin/openzeppelin-contracts@v5.0.1- Clone the repository
git clone https://github.com/your-username/SimpleStakingProtocol.git
cd SimpleStakingProtocol- Install dependencies
forge installRun the comprehensive test suite:
forge testThe protocol maintains several critical invariants to ensure security and correctness:
- Total staked amount = Sum of all individual user stakes
- Contract token balance ≥ Total staked amount + minimum reserve percentage
- User staked amount ≤ User token balance
- Only owner can pause/unpause contract and modify reward rates
- All state changes emit corresponding events
- Minimum time between certain user actions to prevent flash loan attacks
This project is licensed under the MIT License
For questions or contributions, please open an issue on the repository.
Thank u for checking out 🤠
