Created
December 3, 2018 03:52
-
-
Save fromjyk/9bfd1b6a27ebbe6c3efe9181f7d52045 to your computer and use it in GitHub Desktop.
This file contains 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
pragma solidity >=0.4.22 <0.6.0; | |
interface token { | |
function transfer(address receiver, uint amount) external; | |
} | |
contract Crowdsale { | |
address public beneficiary; | |
uint public fundingGoal; | |
uint public amountRaised; | |
uint public deadline; | |
uint public price; | |
token public tokenReward; | |
mapping(address => uint256) public balanceOf; | |
bool fundingGoalReached = false; | |
bool crowdsaleClosed = false; | |
event GoalReached(address recipient, uint totalAmountRaised); | |
event FundTransfer(address backer, uint amount, bool isContribution); | |
constructor( | |
address ifSuccessfulSendTo, | |
uint fundingGoalInEthers, | |
uint durationInMinutes, | |
uint etherCostOfEachToken, | |
address addressOfTokenUsedAsReward | |
) public { | |
beneficiary = ifSuccessfulSendTo; | |
fundingGoal = fundingGoalInEthers * 1 ether; | |
deadline = now + durationInMinutes * 1 minutes; | |
price = etherCostOfEachToken * 1 ether; | |
tokenReward = token(addressOfTokenUsedAsReward); | |
} | |
function () payable external { | |
require(!crowdsaleClosed); | |
uint amount = msg.value; | |
balanceOf[msg.sender] += amount; | |
amountRaised += amount; | |
tokenReward.transfer(msg.sender, amount / price); | |
emit FundTransfer(msg.sender, amount, true); | |
} | |
modifier afterDeadline() { if (now >= deadline) _; } | |
function checkGoalReached() public afterDeadline { | |
if (amountRaised >= fundingGoal){ | |
fundingGoalReached = true; | |
emit GoalReached(beneficiary, amountRaised); | |
} | |
crowdsaleClosed = true; | |
} | |
function safeWithdrawal() public afterDeadline { | |
if (!fundingGoalReached) { | |
uint amount = balanceOf[msg.sender]; | |
balanceOf[msg.sender] = 0; | |
if (amount > 0) { | |
if (msg.sender.send(amount)) { | |
emit FundTransfer(msg.sender, amount, false); | |
} else { | |
balanceOf[msg.sender] = amount; | |
} | |
} | |
} | |
if (fundingGoalReached && beneficiary == msg.sender) { | |
if (msg.sender.send(amountRaised)) { | |
emit FundTransfer(beneficiary, amountRaised, false); | |
} else { | |
fundingGoalReached = false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment