Skip to content

Instantly share code, notes, and snippets.

@tungd
Last active August 1, 2018 06:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tungd/38005265fced4207260eab963e4df677 to your computer and use it in GitHub Desktop.
Save tungd/38005265fced4207260eab963e4df677 to your computer and use it in GitHub Desktop.
Example fund raising contract
contract RaiseFund {
address public owner;
uint public target;
uint public total;
bool public ended;
struct Donation {
address donator;
uint value;
}
Donation[] public donations;
constructor(uint _target) {
owner = msg.sender;
target = _target;
}
function donate() public payable {
require(msg.value > 0);
require(ended == false);
donations.push(Donation(msg.sender, msg.value));
total += msg.value;
if (total >= target) {
ended = true;
owner.transfer(total);
}
}
function refund() public {
require(msg.sender == owner);
require(ended == false);
for (uint i = 0; i < donations.length; i += 1) {
Donation memory donation = donations[i];
donation.donator.transfer(donation.value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment