I just started learning how to write smart contracts. I have some questions about crowdfunding.I follow the instructions of my textbook.
My operating system is Windows 10. It compiles OK, but when I check the state, it always show "Funding", and it doesn't end. First,I use Mist Wallet to deploy the contract.Set the duration and goalAmount,I set 1800 sec and 10 ether.Then I press the deploy button. Second,I use geth to Transaction =>(cf.fund.sendTransation({from:eth.accounts[1],gas:5000000,value:web3.toWei(10,"ether")}) Up to now is no problem.All 10 ether have sent to the account. Last,I check the state =>(cf.status() => "Funding"/cf.ended() => "false") It used to be "Campaign Succeeded"/true
Is there any problem in my program?
pragma solidity ^0.4.18;
contract CrowdFunding {
struct Investor {
address addr;
uint amount;
}
address public owner;
uint public numInvestors;
uint public deadline;
string public status;
bool public ended;
uint public goalAmount;
uint public totalAmount;
mapping (uint => Investor) public Investors;
modifier onlyOwner () {
require(msg.sender == owner);
_;
}
constructor(uint _duration, uint _goalAmount) public {
owner = msg.sender;
deadline = now + _duration;
goalAmount = _goalAmount;
status = "Funding";
ended = false;
numInvestors = 0;
totalAmount = 0;
}
function fund() public payable {
require(!ended);
Investor storage inv = Investors[numInvestors++];
inv.addr = msg.sender;
inv.amount = msg.value;
totalAmount += inv.amount;
}
function checkGoalReached () public onlyOwner {
require(!ended);
require(now >= deadline);
if(totalAmount >= goalAmount) {
status = "Campaign Succeeded";
ended = true;
require(!owner.send(address(this).balance));
uint i = 0;
status = "Campaign Failed";
ended = true;
while(i <= numInvestors) {
require(!Investors[i].addr.send(Investors[i].amount));
i++;
}
}
}
function kill() public onlyOwner {
selfdestruct(owner);
}
}
}
incheckGoalReached()
around here:if(totalAmount >= goalAmount)