1
pragma solidity ^0.4.16;

interface token {
    function mintToken(address receiver, uint amount);
}

contract Owned {

    address owner;

    /*this function is executed at initialization and sets the owner of the contract */
    function owned() { owner = msg.sender; }

    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }
}

contract Selfdestroy is Owned {

    /* Function to recover the funds on the contract */
    function Destroy() onlyOwner() {
        selfdestruct(owner);
    }

}


contract Crowdsale is Owned,Selfdestroy {
    address public beneficiary;
    uint public fundingGoal;
    uint public amountRaised;
    uint public deadline;
    uint public price;
    token public tokenReward;
    mapping(address => uint256) public balanceOf;
    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;
        price = etherCostOfEachToken;
        tokenReward = token(addressOfTokenUsedAsReward);
    }

    /**
     * Fallback function
     *
     * The function without name is the default function that is called whenever anyone sends funds to a contract
     */
    function () payable {  
        uint amount = msg.value;
        balanceOf[msg.sender] += amount;
        amountRaised += amount;
        tokenReward.mintToken(msg.sender, amount / price);
        FundTransfer(msg.sender, amount, true);
    }

    /** 
     * Withdraw the funds
     */
    function OwnerWithdrawal() onlyOwner {
     beneficiary.transfer(amountRaised);
    }

}

I edit the function OwnerWithdrawal to withdraw eth from contract , but it's can't execute on second time. Is there any way to execute many time I want?

  • What do you mean by "can't execute a second time"? Does it throw an error? What error? Did you try debugging it on Remix? Are you sure it doesn't get executed? Maybe since it does bu given that the second time amountRaised will be 0, nothing happens. – pabloruiz55 Nov 2 '17 at 14:52
  • It's mean, the first time I run that funtion its working ok. But on next time when I want withdraw again, it's showing a error like " The contract won't allow this transaction to be executed". Although I tried running Owned function again before executing this funtion. I test it on Mist. – Liam Hoang Nov 2 '17 at 15:08
0

It seems like the variable amountRaised keeps track of the amount of Ether that the Crowdsale contract has received.

For example, suppose the contract has received 4 ETH, and you call the OwnerWithdrawal() function. The first time, it will send 4 ETH. After this, the contract is empty. If you call OwnerWithdrawal() again it will try to send 4 ETH again, but it will fail because the Crowdsale contract does not have 4 ETH.

  • I also think that too, but I'm not good at code. Can please you give me a suggestion to change this function? I want to be able to withdraw at any time. – Liam Hoang Nov 3 '17 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.