StakeVault

Earn 5% APR on staked ETH with instant withdrawals - built with Hardhat v3 for secure DeFi staking.

StakeVault

Created At

ETHOnline 2025

Project Description

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 security

The smart contract tracks each user's deposit time and balance, calculating rewards using the formula: reward = (balance × 5% × time_staked) / 365_days

This 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.

How it's Made

StakeVault was built entirely with Hardhat v3, leveraging the latest features and best practices in Ethereum development.

Core Technologies:

  1. Hardhat v3 (3.0.9) - The latest version of Hardhat with ESM support

    • Used the new network connection patterns with typed network configurations
    • Leveraged Hardhat's simulated networks (EDR) for local testing with time manipulation
    • Configured multiple networks: hardhatMainnet (L1 simulation), hardhatOp (Optimism simulation), and Sepolia testnet
  2. Solidity 0.8.28 - Latest stable version

    • Utilized 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 computation
    • Applied security best practices: checks-effects-interactions pattern
  3. Viem (2.38.4) - Modern TypeScript library replacing ethers.js

    • Type-safe contract interactions with automatic ABI inference
    • Better performance and smaller bundle size compared to ethers.js
    • Used parseEther/formatEther for precise ETH amount handling
  4. Testing Stack:

    • Forge-std for Solidity tests with powerful cheatcodes (vm.prank, vm.warp, vm.deal)
    • Node.js test runner for TypeScript integration tests with viem
    • Fuzz testing to validate deposit function with randomized inputs
    • Time manipulation using testClient.increaseTime() to simulate days/weeks of staking
  5. Development Tools:

    • dotenv for secure environment variable management
    • TypeScript for type safety across all scripts
    • Hardhat Ignition for reproducible deployments

Notable 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 switching
  • Type-safe configuration with explicit network types ("edr-simulated", "rpc")
  • @nomicfoundation/hardhat-toolbox-viem for integrated tooling

Particularly Hacky/Clever Solutions:

  1. 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.

  2. getUserInfo Helper Function: Added a view function that returns (staked, rewards, depositTime) in one call, reducing RPC calls and improving frontend performance.

  3. 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.

  4. 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.

background image mobile

Join the mailing list

Get the latest news and updates