Skip to content

Instantly share code, notes, and snippets.

@Adophilus
Created March 30, 2024 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adophilus/db7adedc2feb69b615a89a96032fb493 to your computer and use it in GitHub Desktop.
Save Adophilus/db7adedc2feb69b615a89a96032fb493 to your computer and use it in GitHub Desktop.
A basic crowdfunding smart contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
// AGENDA
// Develop a crowdfund smart contract
// Adhere to best practices
// Transfer & withdrawal support
// Support for locked deposits
// Support for pull out window
// Multiple/concurrent funding
contract Crowdfund {
uint target;
uint public amountFunded;
address owner;
uint deadline;
mapping(address => uint) contributors;
enum State {
Closed,
Open
}
State state = State.Open;
constructor (uint _target, uint _deadline) {
owner = msg.sender;
target = _target;
deadline = _deadline;
}
function fund () public payable {
require(block.timestamp < deadline, "Deadline has passed");
contributors[msg.sender] += msg.value;
amountFunded += msg.value;
}
function withdraw() public {
require(msg.sender == owner, "Only the owner can call this function");
require(block.timestamp > deadline, "Deadline not yet reached");
uint amountToSend = amountFunded;
amountFunded = 0;
state = State.Closed;
payable(owner).transfer(amountToSend);
}
function getContribution() public view returns (uint) {
return contributors[msg.sender];
}
function pullOutContribution () public {
uint contribution = contributors[msg.sender];
contributors[msg.sender] = 0;
amountFunded -= contribution;
payable(msg.sender).transfer(contribution);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment