Skip to content

Instantly share code, notes, and snippets.

@Gim6626
Last active October 8, 2017 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gim6626/7f658d37683f9dbe41a26a7adc97489b to your computer and use it in GitHub Desktop.
Save Gim6626/7f658d37683f9dbe41a26a7adc97489b to your computer and use it in GitHub Desktop.
DVFooCrowdsale8.sol
pragma solidity ^0.4.13;
interface token {
function transfer(address receiver, uint256 amount);
function burn(uint256 value);
}
contract DVFooCrowdsale8 {
address public beneficiary;
uint public fundingGoal;
uint public amountRaised;
uint public deadline;
uint public price;
uint public tokensForSale;
uint public tokensRemains;
token public tokenReward;
mapping(address => uint256) public balanceOf;
bool fundingGoalReached = false;
event GoalReached(address beneficiary, uint amountRaised);
event FundTransfer(address backer, uint amount, bool isContribution);
bool public crowdsaleClosed = false;
/* data structure to hold information about campaign contributors */
/* at initialization, setup the owner */
function DVFooCrowdsale8(
address ifSuccessfulSendTo,
uint fundingGoalInEthers,
uint durationInMinutes,
uint etherCostOfEachToken,
uint tokensForSaleCount,
token addressOfTokenUsedAsReward
) {
beneficiary = ifSuccessfulSendTo;
fundingGoal = fundingGoalInEthers * 1 ether;
deadline = now + durationInMinutes * 1 minutes;
price = etherCostOfEachToken * 1 ether;
tokensForSale = tokensForSaleCount * 100;
tokensRemains = tokensForSale;
tokenReward = token(addressOfTokenUsedAsReward);
}
/* The function without name is the default function that is called whenever anyone sends funds to a contract */
function () payable {
assert(!crowdsaleClosed);
assert(msg.value <= fundingGoal - amountRaised);
uint amount = msg.value;
balanceOf[msg.sender] += amount;
amountRaised += amount;
if (amountRaised >= fundingGoal) {
fundingGoalReached = true;
deadline = now;
crowdsaleClosed = true;
GoalReached(beneficiary, amountRaised);
}
uint toSend = 100 * amount / price;
tokenReward.transfer(msg.sender, toSend);
tokensRemains -= toSend;
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 (beneficiary == msg.sender) {
if (amountRaised >= fundingGoal){
fundingGoalReached = true;
GoalReached(beneficiary, amountRaised);
}
crowdsaleClosed = true;
}
}
function closeCrowdsaleManually() {
if (beneficiary == msg.sender) {
fundingGoalReached = true;
deadline = now;
crowdsaleClosed = true;
GoalReached(beneficiary, amountRaised);
}
}
function safeWithdrawal() afterDeadline {
uint256 amount;
if (!fundingGoalReached) {
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)) {
if (tokensRemains > 0) {
tokenReward.burn(tokensRemains);
}
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