Skip to content

Instantly share code, notes, and snippets.

@Gim6626
Last active July 31, 2018 05:51
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/360ce0d6b1aa84ea68636928f82ce5d0 to your computer and use it in GitHub Desktop.
Save Gim6626/360ce0d6b1aa84ea68636928f82ce5d0 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.21;
contract QZCrowdsale {
address public owner;
uint public fundingGoal;
uint public amountRaised;
uint public deadline;
address[] investors;
mapping(address => uint256) public balanceOf;
bool public fundingGoalReached;
bool public crowdsaleClosed;
event CrowdsaleClosed(uint amountRaised);
event FundTransfer(address to, uint amount);
/* data structure to hold information about campaign contributors */
/* at initialization, setup the owner */
function QZCrowdsale(
uint fundingGoalInEthers,
uint durationInMinutes
) public {
owner = msg.sender;
fundingGoal = fundingGoalInEthers * 1 ether;
deadline = now + durationInMinutes * 1 minutes;
amountRaised = 0;
fundingGoalReached = false;
crowdsaleClosed = false;
}
function numberOfInvestors() view public returns (uint) {
return investors.length;
}
function investorAddress(uint i) view public returns (address) {
return investors[i];
}
/* The function without name is the default function that is called whenever anyone sends funds to a contract */
function () payable public {
require(!crowdsaleClosed);
require(now < deadline);
uint amount = msg.value;
balanceOf[msg.sender] += amount;
investors.push(msg.sender);
amountRaised += amount;
if (amountRaised >= fundingGoal) {
fundingGoalReached = true;
crowdsaleClosed = true;
emit CrowdsaleClosed(amountRaised);
}
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
modifier onlyWhenCrowdsaleIsClosed() {
require(crowdsaleClosed == true);
_;
}
/* checks if the goal or time limit has been reached and ends the campaign */
function checkForClosureConditions() onlyOwner public {
if (now >= deadline) {
crowdsaleClosed = true;
emit CrowdsaleClosed(amountRaised);
} else if (amountRaised >= fundingGoal) {
fundingGoalReached = true;
crowdsaleClosed = true;
emit CrowdsaleClosed(amountRaised);
}
}
function closeCrowdsaleManually() onlyOwner public {
crowdsaleClosed = true;
emit CrowdsaleClosed(amountRaised);
}
function safeWithdrawal() onlyWhenCrowdsaleIsClosed onlyOwner public {
require(owner.send(amountRaised));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment