StakeVault
Earn 5% APR on staked ETH with instant withdrawals - built with Hardhat v3 for secure DeFi staking.
Problem Statement
StakeVault is a decentralized ETH staking protocol that enables users to earn passive income through a simple and secure smart contract. Users can deposit any amount of ETH and start earning 5% Annual Percentage Rate (APR) immediately, with rewards calculated automatically based on staking duration.Key Features: • Flexible Staking: Deposit any amount of ETH with no minimum requirements • 5% APR: Competitive annual returns calculated pro-rata based on time staked • No Lock-up Period: Withdraw your principal and earned rewards anytime • Automatic Reward Calculation: Time-weighted rewards computed on-chain in real-time • Gas Efficient: Optimized Solidity code minimizes transaction costs • Fully Tested: 13 comprehensive tests (6 Solidity + 7 TypeScript) ensuring securityThe smart contract tracks each user's deposit time and balance, calculating rewards using the formula: reward = (balance × 5% × time_staked) / 365_daysThis means if you stake 10 ETH for 37 days, you'll earn approximately 0.05 ETH in rewards. The contract handles all calculations on-chain, ensuring transparency and trustless operation.StakeVault demonstrates modern DeFi principles with a focus on simplicity, security, and user experience. It's designed for both individual stakers looking for passive income and as a building block for more complex DeFi protocols.Live Contract: 0xf8c7cce6a80140b6c6cba4fe9ca172b6c544fe75 (Sepolia Testnet)Technical feedback for Avail Nexus SDK is included in the project README.
Solution
StakeVault was built entirely with Hardhat v3, leveraging the latest features and best practices in Ethereum development.Core Technologies:Hardhat v3 (3.0.9) - The latest version of Hardhat with ESM supportUsed the new network connection patterns with typed network configurationsLeveraged Hardhat's simulated networks (EDR) for local testing with time manipulationConfigured multiple networks: hardhatMainnet (L1 simulation), hardhatOp (Optimism simulation), and Sepolia testnetSolidity 0.8.28 - Latest stable versionUtilized custom errors for gas efficiency (ZeroDeposit, NoBalance, TransferFailed)Implemented events for off-chain tracking (Deposited, Withdrawn)Used native time calculations with block.timestamp for trustless reward computationApplied security best practices: checks-effects-interactions patternViem (2.38.4) - Modern TypeScript library replacing ethers.jsType-safe contract interactions with automatic ABI inferenceBetter performance and smaller bundle size compared to ethers.jsUsed parseEther/formatEther for precise ETH amount handlingTesting Stack:Forge-std for Solidity tests with powerful cheatcodes (vm.prank, vm.warp, vm.deal)Node.js test runner for TypeScript integration tests with viemFuzz testing to validate deposit function with randomized inputsTime manipulation using testClient.increaseTime() to simulate days/weeks of stakingDevelopment Tools:dotenv for secure environment variable managementTypeScript for type safety across all scriptsHardhat Ignition for reproducible deploymentsNotable Technical Decisions:• Integer Math Precision: Since Solidity doesn't support floating-point, we calculate rewards as (balance × rate × time) / (seconds_per_year × 100). This means the effective APR shows slight variations over short periods (4% after 7 days, 4.93% after 37 days) but converges to 5% over longer durations.• Vault Funding Strategy: The contract needs pre-funded ETH to pay rewards. In tests, we use testClient.setBalance() to fund the vault with 100 ETH. In production, this would be handled through protocol fees or treasury allocations.• Dual Testing Approach: Combining Solidity unit tests (fast, focused) with TypeScript integration tests (realistic, full workflow) ensures both contract logic correctness and real-world usability.• Time Travel for Demos: Created demo scripts that use Hardhat's time manipulation to compress 37 days of staking into seconds, allowing judges to see the complete lifecycle instantly.Hardhat v3 Specific Features Used:ESM module system (import/export instead of require)New network.connect() API for programmatic network switchingType-safe configuration with explicit network types ("edr-simulated", "rpc")@nomicfoundation/hardhat-toolbox-viem for integrated toolingParticularly Hacky/Clever Solutions:Demo Script with Delays: Added 4-second pauses between steps in demo-local.ts using async/await, making it perfect for live video demonstrations without visible "waiting" messages.getUserInfo Helper Function: Added a view function that returns (staked, rewards, depositTime) in one call, reducing RPC calls and improving frontend performance.Automatic Vault Funding in Tests: Instead of manually funding the vault before each test, we added vm.deal() in setUp() to ensure the vault always has 100 ETH for reward payouts.Multiple Demo Scripts: Created separate scripts (demo-deposit, demo-check-rewards, demo-withdraw, demo-multi-user) for modular demonstrations, plus one comprehensive demo-local.ts that shows the entire flow.The project showcases modern Ethereum development practices while maintaining simplicity and security, making it both a functional DeFi protocol and an educational reference for Hardhat v3 patterns.Technical feedback for Avail Nexus SDK is included in the project README.
Hackathon
ETHOnline 2025
2025
Contributors
- SudeepGowda55
15 contributions
- VishruthVS
11 contributions