A professional-grade Staking dApp built with Hardhat and Solidity. This project implements a "Synthetix-style" staking algorithm, allowing users to stake ERC20 tokens and earn rewards proportional to their share of the pool and the time staked.
- Time-Based Rewards: Users earn rewards every second they are staked.
- Math Precision: Uses a "Global Odometer" (Reward Per Token) pattern for O(1) complexity, ensuring gas costs remain low regardless of user count.
- Security: Built with OpenZeppelin v5 (ReentrancyGuard, Ownable, SafeERC20).
- Admin Controls: Owner can set reward durations and fund the pool dynamically.
- Comprehensive Testing: Includes "Time Travel" tests to verify reward accumulation over time.
contracts/: Smart contracts (Staking.sol,MockERC20.sol).test/: Hardhat tests ensuring mathematical accuracy.ignition/modules/: Deployment modules for Hardhat Ignition.scripts/: Interaction scripts for mainnet/testnet and local time manipulation.
-
Clone the repository
git clone <your-repo-url> cd StakingPool
-
Install Dependencies
npm install
-
Environment Configuration Create a
.envfile in the root directory (do not share this file!).# .env PRIVATE_KEY="your_wallet_private_key" SEPOLIA_RPC_URL="[https://sepolia.infura.io/v3/YOUR_KEY](https://sepolia.infura.io/v3/YOUR_KEY)" ETHERSCAN_API_KEY="your_etherscan_key"
We use Hardhat and Chai for testing. The test suite includes "Time Travel" scenarios to simulate days or weeks of staking in seconds.
npx hardhat test
What is tested?
- ✅ Staking & Withdrawing mechanics.
- ✅ Reward calculation accuracy (Math verification).
- ✅ Multi-user scenarios (ensuring no one steals rewards).
- ✅ Access controls (Owner functions).
We use Hardhat Ignition for robust deployments.
Great for development and frontend testing.
- Start the Local Node:
npx hardhat node
- Deploy:
npx hardhat ignition deploy ignition/modules/Deploy.js --network localhost
Deploys to the real Ethereum test network.
npx hardhat ignition deploy ignition/modules/Deploy.js --network sepolia
After deployment, you can interact with your contract using the provided scripts.
This script funds the pool, sets the reward rate, and stakes tokens on behalf of the owner.
- Note: If running on Sepolia, make sure to update the addresses in the script first.
npx hardhat run scripts/interact.js --network localhost
# OR
npx hardhat run scripts/interact.js --network sepolia
Only works on localhost.
This script simulates the passage of time (e.g., fast-forward 30 seconds) to verify rewards without waiting.
npx hardhat run scripts/checkRewards.js --network localhost
- Staking Token: The ERC20 token users deposit.
- Rewards Token: The ERC20 token users earn (can be the same or different).
- updateReward Modifier: The heartbeat of the contract. It updates the state whenever a user interacts, ensuring calculations are always precise down to the second.
- ReentrancyGuard: Prevents reentrancy attacks on withdraw/claim functions.
- SafeERC20: Handles non-standard ERC20 tokens securely.
- Rounding Errors: Minimized by scaling calculations by
1e18.