Lab 3: Crowdfunding Contract With Goals, Deadlines, Refunds and a Safe Withdrawal on a Test Network

Blockchain Developer 70 min min read Updated: Nov 17, 2026 Intermediate
Lab 3: Crowdfunding Contract With Goals, Deadlines, Refunds and a Safe Withdrawal on a Test Network
Intermediate Topic 3 of 3

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

RuleImplementation
Contributions only before the deadlineblock.timestamp < deadline
Track who gave how muchmapping(address => uint256)
Creator withdraws only if goal reached and deadline passedChecks in withdraw
Refund only if goal missedChecks in refund
Avoid reentrancyUpdate 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.

  1. Using account 2, set Value to 1 ether and call contribute
  2. Using account 3, contribute 1 ether. totalRaised should be 2 ether
  3. Try withdraw as the creator now. It reverts with CampaignRunning
  4. Wait two minutes. On the Remix VM you can also deploy a new contract with duration 5 and wait a few seconds
  5. Call withdraw as the creator. The creator's balance grows
  6. Redeploy with a goal of 10 ether, contribute 1 ether, wait for the deadline and call refund from the contributor

Deploy to Sepolia

  1. Get Sepolia test ETH from a faucet
  2. In Remix choose Injected Provider and the Sepolia network
  3. Deploy with a small goal such as 0.01 ether and a duration of 600 seconds
  4. Contribute from a second wallet, wait, then withdraw or refund
  5. Search the contract address in a Sepolia explorer and read the events

Security Review Questions

  • Why is withdrawn set 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

  1. Allow the creator to cancel before any contributions
  2. Add a minimum contribution
  3. Write Hardhat tests for every revert case
  4. 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.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators