Create a gist now

Instantly share code, notes, and snippets.

@Fanilus /Crowdsale_1.0.sol Secret
Last active Dec 1, 2017

What would you like to do?
pragma solidity ^0.4.16;
interface token {
function transfer(address receiver, uint amount);
function balanceOf(address _owner) constant returns (uint balance);
}
contract Crowdsale {
address public beneficiary;
uint public fundingGoal;
uint public amountRaised;
uint private amountBonus1;
uint private amountBonus2;
uint private amountBonus3;
uint public deadline;
uint public startdate;
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);
/**
* Constrctor function
*
* Setup the owner
*/
function Crowdsale(
address ifSuccessfulSendTo,
uint fundingGoalInEthers,
uint durationInMinutes,
uint etherCostOfEachToken,
address addressOfTokenUsedAsReward
) {
beneficiary = ifSuccessfulSendTo;
fundingGoal = fundingGoalInEthers * 1 ether;
deadline = now + durationInMinutes * 1 minutes;
startdate = now;
price = etherCostOfEachToken;
tokenReward = token(addressOfTokenUsedAsReward);
amountBonus1 = 100 * 1 ether;//100
amountBonus2 = 60 * 1 ether;//60
amountBonus3 = 20 * 1 ether;//20
}
/**
* Calculate bonus ratio from ammount
*/
function bonusRatio(uint amount) private returns (uint) {
if(amount >= amountBonus1){
return 120;//20%
}else if (amount >= amountBonus2) {
return 115;//15%
}else if (amount >= amountBonus3) {
return 110;//10%
}else{
return 100;//0
}
}
/**
* Calculate tokens
*/
function getCurrentTokensOffer(uint amount) private returns (uint) {
uint bonusAm = bonusRatio(amount);
uint tokensGet = 0;
if (now < startdate + 1 * 1 days){
tokensGet = (amount / price) * bonusAm/100;
}else if (now < startdate + 5 * 1 days)
tokensGet = (amount / (price * 103030303030303/100000000000000)) * bonusAm/100;
else if (now < startdate + 13 * 1 days)
tokensGet = (amount / (price * 109090909090909/100000000000000)) * bonusAm/100;
else if (now < startdate + 26 * 1 days)
tokensGet = (amount / (price * 116666666666666/100000000000000)) * bonusAm/100;
else
tokensGet = (amount / (price * 118181818181818/100000000000000)) * bonusAm/100;
return tokensGet;
}
/**
* Fallback function
*
* The function without name is the default function that is called whenever anyone sends funds to a contract
*/
function () payable {
require(msg.value >= 13200 * 1 szabo);// min 0.0132 ETH
require(!crowdsaleClosed);
uint amount = msg.value;
balanceOf[msg.sender] += amount;
amountRaised += amount;
tokenReward.transfer(msg.sender, getCurrentTokensOffer(amount) * 1 ether);
FundTransfer(msg.sender, amount, true);
}
modifier afterDeadline() { if (now >= deadline) _; }
/**
* Check if goal was reached
*
* 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;
}
/**
* Withdraw the funds
*
* Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached,
* sends the entire amount to the beneficiary. If goal was not reached, each contributor can withdraw
* the amount they contributed.
*/
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);
tokenReward.transfer(beneficiary, tokenReward.balanceOf(address(this)));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment