Skip to content

Valeprogr/au-hardhat-practice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

💧 First Smart Contract — Faucet on Ethereum Sepolia

This is my first smart contract built and deployed with Hardhat 3, written in Solidity 0.8.28, and tested using Mocha + Chai + Ethers v6.
It implements a simple Faucet, which allows users to withdraw small amounts of ETH with a cooldown time between withdrawals.


🧩 Overview

Contract: Faucet.sol
Network: Sepolia (chainId 11155111)
Address: 0xcAC087aF249117F2a4Ef479c528749C858D6A8a2
Deployer: 0xeC0760E041Ff8C5377cCB8d123d90D3F96d0fba9
Verified on: Blockscout


⚙️ Smart Contract Description

Faucet.sol

A simple faucet that:

  • allows anyone to fund it by sending ETH,
  • allows each address to withdraw up to 0.1 ETH,
  • enforces a 1-hour cooldown between withdrawals per address.

Main features

  • Uses custom errors (AmountZero, AmountTooHigh, CooldownActive, InsufficientFaucetBalance) → cheaper gas than revert strings.
  • Emits events:
    • Funded(address from, uint256 amount)
    • Withdraw(address to, uint256 amount)
  • Safe transfer pattern with call{value: _amount} and CEI (Checks–Effects–Interactions).

🧪 Testing

All tests are simulated locally using Hardhat’s built-in network (no real gas or ETH needed).

Run the suite:

npx hardhat test

It covers:

  • ✅ Faucet funding (receive + Funded event)
  • ✅ Successful withdrawals within limits
  • ✅ Reverts on zero or excessive amounts
  • ✅ Reverts if faucet balance is insufficient
  • ✅ Cooldown logic (CooldownActive revert + success after 1h)

🚀 Deployment

Deployment script: scripts/deploy.ts

Deploy to Sepolia

npx hardhat run scripts/deploy.ts --network sepolia

Expected output example:

Network: sepolia (chainId 11155111)
Faucet deployed to: 0xcAC087aF249117F2a4Ef479c528749C858D6A8a2
Deploy tx hash: 0x0359ebde17a703e77f9687fd41b70c4cc939ca6fd88fd96af334b4753ae1096c
Deploying contract with account: 0xeC0760E041Ff8C5377cCB8d123d90D3F96d0fba9

Verify on Etherscan

Make sure you have an API key in .env:

ETHERSCAN_API_KEY=your_key_here

Then run:

npx hardhat verify --network sepolia 0xcAC087aF249117F2a4Ef479c528749C858D6A8a2

🧰 Environment Setup

Requirements

  • Node.js ≥ 18
  • Hardhat 3
  • TypeScript
  • Ethers v6

Install dependencies

npm install

Environment variables (.env)

SEPOLIA_RPC_URL=https://eth-sepolia.g.alchemy.com/v2/your-key
SEPOLIA_PRIVATE_KEY=0xabc123...   # private key of your Sepolia wallet
ETHERSCAN_API_KEY=your-etherscan-key

🧠 Useful Hardhat Commands

Command Description
npx hardhat compile Compile contracts
npx hardhat test Run test suite
npx hardhat run scripts/deploy.ts --network sepolia Deploy to Sepolia
npx hardhat verify --network sepolia <address> Verify on Etherscan
npx hardhat console --network sepolia Open interactive console

🪙 Example Usage

Fund the Faucet

Send ETH directly to the contract address:

0xcAC087aF249117F2a4Ef479c528749C858D6A8a2

Withdraw from the Faucet

Call:

withdraw(uint256 _amount)

Example: _amount = 0.05 ether


🧑‍💻 Tech Stack

  • Solidity 0.8.28
  • Hardhat 3 + Ethers v6
  • TypeScript
  • Chai / Mocha for testing
  • Dotenv for config
  • Blockscout / Etherscan for verification

🏁 Next Steps

  • ✅ Add faucetBalance() public view
  • 🧪 Expand test coverage for reentrancy checks
  • 🌐 Deploy also to Optimism testnet
  • 📜 Write a frontend DApp with Viem + React to interact with the faucet

✨ Author

Made with ❤️ by Valentina First Smart Contract project – Ethereum Bootcamp @ Alchemy University (2025)

About

This is my first smart contract built and deployed with Hardhat 3, written in Solidity 0.8.28, and tested using Mocha + Chai + Ethers v6.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors