Created
March 30, 2024 20:46
-
-
Save Adophilus/db7adedc2feb69b615a89a96032fb493 to your computer and use it in GitHub Desktop.
A basic crowdfunding smart contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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