pragma solidity 0.4.26; contract CrowdFunding { uint256 public fundingGoal; uint256 public raisedAmount = 0; uint256 public deadline; uint256 public price; address public beneficiary; mapping(address => uint256) public balanceOf; bool public fundingGoalReached = false; bool public crowdsaleClosed = false; /* events that will be fired on changes */ event GoalReached(address recipient, uint256 totalAmountRaised); event FundTransfer(address backer, uint256 amount, bool isContribution); /* initialization function */ constructor(uint256 _fundingGoal, uint256 _durationInMinutes, uint256 _price, address _beneficiary) public { fundingGoal = _fundingGoal * 1 ether; deadline = now + (_durationInMinutes * 1 minutes); price = _price * 1 ether; beneficiary = _beneficiary; } /* The function without name is the default function that is called whenever anyone sends funds to a contract */ function () public payable { require(!crowdsaleClosed); uint256 amount = msg.value; balanceOf[msg.sender] += amount; raisedAmount += amount; emit 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() public afterDeadline { if (raisedAmount >= fundingGoal) { fundingGoalReached = true; emit GoalReached(beneficiary, raisedAmount); } crowdsaleClosed = true; } /* withdraw the funds */ function safeWithdrawal() public { require(fundingGoalReached); uint256 amount = raisedAmount; raisedAmount = 0; emit FundTransfer(beneficiary, amount, false); beneficiary.transfer(amount); } }
0.4.26