Scaffold-ETH Challenge: Building a Staking dApp (Part 1)

·

Introduction

In this guide, we’ll explore the first scaffold-ETH learning project: creating a staking dApp inspired by Ethereum 2.0’s staking contract.

Key Objectives


What You’ll Learn


Essential Resources


Project Setup

Clone and Configure

  1. Clone the scaffold-eth repository:

    git clone https://github.com/austintgriffith/scaffold-eth.git challenge-1-decentralized-staking
    cd challenge-1-decentralized-staking
    git checkout challenge-1-decentralized-staking
    yarn install

Key Commands


Part 1: Implementing the stake() Function

Core Concepts

Code Implementation

Update Staker.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "hardhat/console.sol";
import "./ExampleExternalContract.sol";

contract Staker {
    ExampleExternalContract public exampleExternalContract;
    mapping(address => uint256) public balances;
    uint256 public constant threshold = 1 ether;
    event Stake(address indexed sender, uint256 amount);

    constructor(address exampleExternalContractAddress) {
        exampleExternalContract = ExampleExternalContract(exampleExternalContractAddress);
    }

    function stake() public payable {
        balances[msg.sender] += msg.value;
        emit Stake(msg.sender, msg.value);
    }
}

Testing Your Contract

  1. Deploy the contract.
  2. Fund your wallet via the Faucet.
  3. Stake ETH (e.g., 0.5 ETH).
  4. Verify:

    • Balances update correctly.
    • Events trigger in the UI.
    • Contract balance reflects the stake.

FAQs

Q: Why isn’t my balance updating?
A: Ensure stake() is called with msg.value > 0 and the transaction succeeds.

Q: What’s the purpose of the threshold?
A: It’s the cap for total staked ETH (e.g., 1 ETH).

Q: How do I test events?
A: Check the browser console or use tools like Etherscan for testnets.


👉 Highly engaging anchor text

This guide continues in Part 2, where we’ll cover withdrawal logic and external contract interactions. Stay tuned!