Skip to content

Instantly share code, notes, and snippets.

@stupeters187
Created January 18, 2019 16:08
Show Gist options
  • Save stupeters187/1dd90d8d0419e40faf6532c56de4ac31 to your computer and use it in GitHub Desktop.
Save stupeters187/1dd90d8d0419e40faf6532c56de4ac31 to your computer and use it in GitHub Desktop.
Crowdfunding Smart Contract Example
pragma solidity 0.5.2;
contract Crowdfunding {
address payable beneficiary;
uint goalInEther;
uint deadline;
mapping (address => uint) public contributions;
address[] public contributors;
event NewContribution(address _who, uint _amount, uint _timestamp);
event RefundIssued(address _who, uint _amount, uint _timestamp);
event BeneficiaryPaid(address _beneficiary, uint _amount, uint _timestamp);
constructor(address payable _beneficiary, uint _goalInEther, uint _durationInHours) public {
beneficiary = _beneficiary;
goalInEther = _goalInEther * 1 ether;
deadline = now + (_durationInHours * 1 hours);
}
function contribute() public payable {
require(now < deadline);
if (contributions[msg.sender] == 0) contributors.push(msg.sender);
contributions[msg.sender] += msg.value;
emit NewContribution(msg.sender, msg.value, now);
}
function refund() public {
require(now > deadline &&
address(this).balance < goalInEther
);
uint amountToSend = contributions[msg.sender];
contributions[msg.sender] = 0;
msg.sender.transfer(amountToSend);
emit RefundIssued(msg.sender, amountToSend, now);
}
function payBeneficary() public {
require(now > deadline &&
address(this).balance >= goalInEther
);
beneficiary.transfer(address(this).balance);
emit BeneficiaryPaid(beneficiary, address(this).balance, now);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment