|
| 1 | +contract token { mapping (address => uint) public coinBalanceOf; function token() {} function sendCoin(address receiver, uint amount) returns(bool sufficient) { } } |
| 2 | + |
| 3 | +contract Crowdsale { |
| 4 | + |
| 5 | + address public beneficiary; |
| 6 | + uint public fundingGoal; uint public amountRaised; uint public deadline; uint public price; |
| 7 | + token public tokenReward; |
| 8 | + Funder[] public funders; |
| 9 | + event FundTransfer(address backer, uint amount, bool isContribution); |
| 10 | + |
| 11 | + /* data structure to hold information about campaign contributors */ |
| 12 | + struct Funder { |
| 13 | + address addr; |
| 14 | + uint amount; |
| 15 | + } |
| 16 | + |
| 17 | + /* at initialization, setup the owner */ |
| 18 | + function Crowdsale(address _beneficiary, uint _fundingGoal, uint _duration, uint _price, token _reward) { |
| 19 | + beneficiary = _beneficiary; |
| 20 | + fundingGoal = _fundingGoal; |
| 21 | + deadline = now + _duration * 1 minutes; |
| 22 | + price = _price; |
| 23 | + tokenReward = token(_reward); |
| 24 | + } |
| 25 | + |
| 26 | + /* The function without name is the default function that is called whenever anyone sends funds to a contract */ |
| 27 | + function () { |
| 28 | + uint amount = msg.value; |
| 29 | + funders[funders.length++] = Funder({addr: msg.sender, amount: amount}); |
| 30 | + amountRaised += amount; |
| 31 | + tokenReward.sendCoin(msg.sender, amount / price); |
| 32 | + FundTransfer(msg.sender, amount, true); |
| 33 | + } |
| 34 | + |
| 35 | + modifier afterDeadline() { if (now >= deadline) _ } |
| 36 | + |
| 37 | + /* checks if the goal or time limit has been reached and ends the campaign */ |
| 38 | + function checkGoalReached() afterDeadline { |
| 39 | + if (amountRaised >= fundingGoal){ |
| 40 | + beneficiary.send(amountRaised); |
| 41 | + FundTransfer(beneficiary, amountRaised, false); |
| 42 | + } else { |
| 43 | + FundTransfer(0, 11, false); |
| 44 | + for (uint i = 0; i < funders.length; ++i) { |
| 45 | + funders[i].addr.send(funders[i].amount); |
| 46 | + FundTransfer(funders[i].addr, funders[i].amount, false); |
| 47 | + } |
| 48 | + } |
| 49 | + suicide(beneficiary); |
| 50 | + } |
| 51 | +} |
0 commit comments