Lab 3: Crowdfunding Contract With Goals, Deadlines, Refunds and a Safe Withdrawal on a Test Network
Goal
Build a contract where people contribute ETH toward a goal. If the goal is met by the deadline the creator withdraws the funds. If not, each contributor can take a refund.
Design
| Rule | Implementation |
|---|---|
| Contributions only before the deadline | block.timestamp < deadline |
| Track who gave how much | mapping(address => uint256) |
| Creator withdraws only if goal reached and deadline passed | Checks in withdraw |
| Refund only if goal missed | Checks in refund |
| Avoid reentrancy | Update state before sending ETH |
Contract
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Crowdfund {
address payable public immutable creator;
uint256 public immutable goal;
uint256 public immutable deadline;
uint256 public totalRaised;
bool public withdrawn;
mapping(address => uint256) public contributions;
event Contributed(address indexed from, uint256 amount);
event Withdrawn(uint256 amount);
event Refunded(address indexed to, uint256 amount);
error NotCreator();
error CampaignEnded();
error CampaignRunning();
error GoalNotReached();
error GoalReached();
error NothingToRefund();
error AlreadyWithdrawn();
error TransferFailed();
error ZeroAmount();
constructor(uint256 goalWei, uint256 durationSeconds) {
creator = payable(msg.sender);
goal = goalWei;
deadline = block.timestamp + durationSeconds;
}
function contribute() external payable {
if (block.timestamp >= deadline) revert CampaignEnded();
if (msg.value == 0) revert ZeroAmount();
contributions[msg.sender] += msg.value;
totalRaised += msg.value;
emit Contributed(msg.sender, msg.value);
}
function withdraw() external {
if (msg.sender != creator) revert NotCreator();
if (block.timestamp < deadline) revert CampaignRunning();
if (totalRaised < goal) revert GoalNotReached();
if (withdrawn) revert AlreadyWithdrawn();
withdrawn = true; // effect first
uint256 amount = address(this).balance;
(bool ok, ) = creator.call{value: amount}(""); // interaction last
if (!ok) revert TransferFailed();
emit Withdrawn(amount);
}
function refund() external {
if (block.timestamp < deadline) revert CampaignRunning();
if (totalRaised >= goal) revert GoalReached();
uint256 amount = contributions[msg.sender];
if (amount == 0) revert NothingToRefund();
contributions[msg.sender] = 0; // effect first
(bool ok, ) = payable(msg.sender).call{value: amount}("");
if (!ok) revert TransferFailed();
emit Refunded(msg.sender, amount);
}
}
Test in Remix (Fast Path)
For a quick test deploy with a goal of 2000000000000000000 (2 ether) and a duration of 120 seconds.
- Using account 2, set Value to 1 ether and call
contribute - Using account 3, contribute 1 ether.
totalRaisedshould be 2 ether - Try
withdrawas the creator now. It reverts withCampaignRunning - Wait two minutes. On the Remix VM you can also deploy a new contract with duration 5 and wait a few seconds
- Call
withdrawas the creator. The creator's balance grows - Redeploy with a goal of 10 ether, contribute 1 ether, wait for the deadline and call
refundfrom the contributor
Deploy to Sepolia
- Get Sepolia test ETH from a faucet
- In Remix choose Injected Provider and the Sepolia network
- Deploy with a small goal such as 0.01 ether and a duration of 600 seconds
- Contribute from a second wallet, wait, then withdraw or refund
- Search the contract address in a Sepolia explorer and read the events
Security Review Questions
- Why is
withdrawnset to true before sending ETH? - Why does each contributor pull their own refund instead of the contract refunding everyone in a loop?
- What would happen if the creator were a contract that refuses ETH?
Extensions
- Allow the creator to cancel before any contributions
- Add a minimum contribution
- Write Hardhat tests for every revert case
- Add a front end with ethers.js
Conclusion
You built a crowdfunding contract that applies checks-effects-interactions, pull-based refunds and deadlines, then tested it and deployed to a test network.
Quick Summary: goal; design; contract; Remix test; Sepolia; review; extensions.

