Trustless Crowdfunding Contract

What it does:
A trustless crowdfunding smart contract that collects funds for a project and automatically enforces success or refund rules without relying on any intermediary.

Why it matters:
Traditional crowdfunding requires trust in platforms or founders. This contract removes that risk by locking funds on-chain and releasing them only if clearly defined conditions are met.

How it works:

  • A project creator defines a funding goal and deadline.

  • Contributors fund the campaign directly on-chain.

  • Funds are locked until the deadline.

  • If the goal is reached, funds are released to the creator.

  • If not, contributors can claim automatic refunds.

      // SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/**
 * @title TrustlessCrowdfunding
 * @author Nam
 * @notice Trustless crowdfunding with automatic fund release or refunds
 */
contract TrustlessCrowdfunding {

    // -------------------- STATE VARIABLES --------------------

    address public creator;
    uint256 public goal;
    uint256 public deadline;
    uint256 public totalRaised;
    bool public fundsReleased;

    mapping(address => uint256) public contributions;

    // -------------------- EVENTS --------------------

    event Funded(address indexed contributor, uint256 amount);
    event FundsReleased(address indexed creator, uint256 amount);
    event Refunded(address indexed contributor, uint256 amount);

    // -------------------- MODIFIERS --------------------

    modifier onlyCreator() {
        require(msg.sender == creator, "Not creator");
        _;
    }

    modifier beforeDeadline() {
        require(block.timestamp = deadline, "Campaign active");
        _;
    }

    // -------------------- CONSTRUCTOR --------------------

    constructor(
        uint256 _goal,
        uint256 _duration
    ) {
        require(_goal > 0, "Invalid goal");
        require(_duration > 0, "Invalid duration");

        creator = msg.sender;
        goal = _goal;
        deadline = block.timestamp + _duration;
    }

    // -------------------- FUNDING LOGIC --------------------

    /**
     * @notice Contribute ETH to campaign
     */
    function contribute() external payable beforeDeadline {
        require(msg.value > 0, "Zero contribution");

        contributions[msg.sender] += msg.value;
        totalRaised += msg.value;

        emit Funded(msg.sender, msg.value);
    }

    // -------------------- FUND RELEASE --------------------

    /**
     * @notice Release funds if goal met
     */
    function releaseFunds()
        external
        afterDeadline
        onlyCreator
    {
        require(!fundsReleased, "Already released");
        require(totalRaised >= goal, "Goal not reached");

        fundsReleased = true;
        payable(creator).transfer(totalRaised);

        emit FundsReleased(creator, totalRaised);
    }

    // -------------------- REFUNDS --------------------

    /**
     * @notice Claim refund if goal not met
     */
    function refund() external afterDeadline {
        require(totalRaised  0, "No contribution");

        contributions[msg.sender] = 0;
        payable(msg.sender).transfer(amount);

        emit Refunded(msg.sender, amount);
    }

    // -------------------- VIEW FUNCTIONS --------------------

    /**
     * @notice Campaign status
     */
    function campaignStatus()
        external
        view
        returns (
            uint256 _goal,
            uint256 _raised,
            uint256 _deadline,
            bool _success
        )
    {
        return (
            goal,
            totalRaised,
            deadline,
            totalRaised >= goal
        );
    }
}