Skip to content

Instantly share code, notes, and snippets.

@AlwaysBCoding
Created February 25, 2017 22:34
Show Gist options
  • Save AlwaysBCoding/b85d35c03a4b9cd35d2ac8b61b1dc3e8 to your computer and use it in GitHub Desktop.
Save AlwaysBCoding/b85d35c03a4b9cd35d2ac8b61b1dc3e8 to your computer and use it in GitHub Desktop.
Ethereum Ðapp Development - Video 16 | Avoiding Loops with Mappings
contract CrowdFund {
address public beneficiary;
uint256 public goal;
uint256 public deadline;
mapping (address => uint256) funders;
address[] funderAddresses;
function CrowdFund(address _beneficiary, uint256 _goal, uint256 _duration) {
beneficiary = _beneficiary;
goal = _goal;
deadline = now + _duration;
}
function getFunderContribution(address _addr) constant returns (uint) {
return funders[_addr];
}
function getFunderAddress(uint _index) constant returns (address) {
return funderAddresses[_index];
}
function funderAddressLength() constant returns (uint) {
return funderAddresses.length;
}
function contribute() payable {
if(funders[msg.sender] == 0) funderAddresses.push(msg.sender);
funders[msg.sender] += msg.value;
}
function payout() {
if(this.balance >= goal && now > deadline) beneficiary.send(this.balance);
}
function refund() {
if(now > deadline && this.balance < goal) {
msg.sender.send(funders[msg.sender]);
funders[msg.sender] = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment