Skip to content

Instantly share code, notes, and snippets.

@raza-basit
Created May 16, 2017 18:39
Show Gist options
  • Save raza-basit/266e95901786cf2a8ca746b9ff4306e8 to your computer and use it in GitHub Desktop.
Save raza-basit/266e95901786cf2a8ca746b9ff4306e8 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.2;
/*contract token {
function transfer(address receiver, uint amount){}
}*/
contract Crowdsale {
address beneficiary;
uint fundingGoal;
uint amountRaised;
uint deadline;
uint price;
//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);
event ContractCreated(address indexed beneficiary,uint duration);
event PayableCalled(bool inPayable);
event Transaction(address indexed fundsSender,uint funds,uint amountRaiesed);
event DeadlineCheck(address indexed checker,bool deadlinePassed);
bool crowdsaleClosed = false;
function getBeneficiary() returns(address) {
return beneficiary;
}
function getFundingGoal() returns(uint) {
return fundingGoal;
}
function getAmountRaised() returns(uint) {
return amountRaised;
}
function getDeadline() returns(uint) {
return deadline;
}
function getPrice() returns(uint) {
return price;
}
function Crowdsale(
address ifSuccessfulSendTo,
uint fundingGoalInEthers,
uint durationInMinutes,
uint etherCostOfEachToken
//,
//token addressOfTokenUsedAsReward
) {
beneficiary = ifSuccessfulSendTo;
fundingGoal = fundingGoalInEthers * 1 ether;
deadline = now + durationInMinutes * 1 minutes;
price = etherCostOfEachToken * 1 ether;
// tokenReward = token(addressOfTokenUsedAsReward);
ContractCreated(ifSuccessfulSendTo,deadline);
}
/* The function without name is the default function that is called whenever anyone sends funds to a contract */
function () payable {
PayableCalled(true);
if (crowdsaleClosed) throw;
uint amount = msg.value;
balanceOf[msg.sender] = amount;
amountRaised += amount;
Transaction(msg.sender,msg.value,amountRaised);
//tokenReward.transfer(msg.sender, amount / price);
FundTransfer(msg.sender, amount, true);
}
modifier afterDeadline() {
if (now >= deadline){
DeadlineCheck(msg.sender,true);
_;
}else{
DeadlineCheck(msg.sender,true);
}
}
/* 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