Skip to content

Einarmig/StakingDAPPxSpeedRunEthereum

Repository files navigation

🏗️ Ethereum Staking Challenge

A decentralized staking application built on Ethereum that demonstrates smart contract development, time-based logic, and threshold mechanisms. This project is part of the SpeedRunEthereum challenges.

🎯 Project Overview

This staking dApp allows users to pool Ether together with a deadline and threshold mechanism. If the threshold is met before the deadline, all funds are sent to an external contract for completion. If not, users can withdraw their individual contributions.

Key Features

  • Time-bound Staking: 72-hour staking period from contract deployment
  • Threshold Mechanism: Requires 1 ETH minimum to complete successfully
  • Conditional Execution: Automatic fund distribution or withdrawal enablement
  • Event Emission: Full transparency of staking activities
  • Direct ETH Transfers: Users can stake by simply sending ETH to the contract

🔧 Smart Contract Architecture

The project consists of two main contracts:

1. Staker Contract

The main contract that handles all staking logic, including:

  • User balance tracking
  • Deadline and threshold validation
  • Conditional execution based on success criteria
  • Withdrawal mechanism for failed stakes

2. ExampleExternalContract

A simple contract that serves as the completion target when staking succeeds.

🎮 How It Works

  1. Staking Phase: Users can stake ETH during the 72-hour window
  2. Execution Phase: After the deadline, anyone can call execute()
    • If ≥ 1 ETH was staked: All funds go to the external contract ✅
    • If < 1 ETH was staked: Withdrawals become available ❌
  3. Withdrawal Phase: Users can reclaim their ETH if the threshold wasn't met

🛠️ Technical Implementation

Key Functions

  • stake(): Accept ETH deposits and update user balances
  • execute(): Determine success/failure and distribute funds accordingly
  • withdraw(): Allow users to reclaim funds if staking failed
  • timeLeft(): Display remaining time in the staking period
  • receive(): Enable direct ETH transfers to stake automatically

Security Features

  • Access Control: notCompleted modifier prevents actions after completion
  • Input Validation: Zero address checks and proper require statements
  • Reentrancy Protection: Simple transfer patterns without callbacks
  • State Management: Clear separation between staking and withdrawal phases

🔍 Contract Details

State Variables

  • threshold: Constant 1 ETH requirement
  • deadline: 72 hours from deployment
  • balances: Individual user stake tracking
  • openForWithdraw: Withdrawal availability flag

Events

  • Stake(address indexed staker, uint256 deposit): Tracks all staking activity

🚀 Deployment & Testing

Prerequisites

  • Node.js (v16 or higher) and yarn
  • Scaffold-ETH 2 framework
  • MetaMask or similar wallet
  • Git for version control

Setup

# Clone the scaffold-eth 2 repository
git clone https://github.com/scaffold-eth/scaffold-eth-2.git staking-challenge
cd staking-challenge

# Install dependencies
yarn install

# Start your local blockchain
yarn chain

# In a new terminal, deploy the contracts
yarn deploy

# In another terminal, start the frontend
yarn start

Test Coverage

# Run all tests
yarn test
  • Successful staking scenarios
  • Failed staking and withdrawal flows
  • Time-based execution logic
  • Edge cases and error conditions

🎓 Learning Outcomes

This project demonstrates proficiency in:

  • Solidity Development: Advanced contract patterns and modifiers
  • Time-based Logic: Deadline mechanisms and conditional execution
  • State Management: Complex multi-phase contract workflows
  • Event Handling: Proper logging and frontend integration
  • Security Best Practices: Input validation and access control
  • Gas Optimization: Efficient storage patterns and function design

🔗 Links & Deployment

Contract Addresses

  • Staker Contract: 0x333935AC52d6ecb67407478841D778B52E04d9b8
  • ExampleExternalContract: 0x8aaB1AdFd7e261b0E5b852Efc4a93251a27AE3b4

Contract Verification

🏆 Challenge Completion

  • ✅ Smart contracts deployed and verified
  • ✅ Frontend integration complete
  • ✅ All tests passing
  • ✅ Proper documentation and comments
  • ✅ Gas optimizations implemented

Built with ❤️ as part of the Ethereum developer journey

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors