Skip to content

Instantly share code, notes, and snippets.

@gotoalberto-zz
Last active September 26, 2016 17:12
Show Gist options
  • Select an option

  • Save gotoalberto-zz/e8fd482961e9c9d15b68ce7e5bf740bc to your computer and use it in GitHub Desktop.

Select an option

Save gotoalberto-zz/e8fd482961e9c9d15b68ce7e5bf740bc to your computer and use it in GitHub Desktop.
contract Crowdsale {
address public beneficiary;
uint public fundingGoal; uint public amountRaised; uint public deadline;
mapping(address => uint256) public balanceOf;
bool fundingGoalReached = false;
event GoalReached(address beneficiary, uint amountRaised);
event FundTransfer(address backer, uint amount, bool isContribution);
bool crowdsaleClosed = false;
/* at initialization, setup the owner */
function Crowdsale(
address ifSuccessfulSendTo,
uint fundingGoalInEthers,
uint durationInMinutes
) {
beneficiary = ifSuccessfulSendTo;
fundingGoal = fundingGoalInEthers * 1 ether;
deadline = now + durationInMinutes * 1 minutes;
}
/* The function without name is the default function that is called whenever anyone sends funds to a contract */
function () {
if (crowdsaleClosed) throw;
uint amount = msg.value;
balanceOf[msg.sender] += amount;
amountRaised += amount;
FundTransfer(msg.sender, amount, true);
}
modifier afterDeadline() { if (now >= deadline) _ }
/* checks if the goal or time limit has been reached and ends the campaign */
function checkGoalReached() afterDeadline {
if (amountRaised >= fundingGoal){
fundingGoalReached = true;
GoalReached(beneficiary, amountRaised);
}
crowdsaleClosed = true;
}
function safeWithdrawal() afterDeadline {
if (!fundingGoalReached) {
uint amount = balanceOf[msg.sender];
balanceOf[msg.sender] = 0;
if (amount > 0) {
if (msg.sender.send(amount)) {
FundTransfer(msg.sender, amount, false);
} else {
balanceOf[msg.sender] = amount;
}
}
}
if (fundingGoalReached && beneficiary == msg.sender) {
if (beneficiary.send(amountRaised)) {
FundTransfer(beneficiary, amountRaised, false);
} else {
//If we fail to send the funds to beneficiary, unlock funders balance
fundingGoalReached = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment