Last active
August 1, 2018 06:19
-
-
Save tungd/38005265fced4207260eab963e4df677 to your computer and use it in GitHub Desktop.
Example fund raising contract
This file contains hidden or 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
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