1

I tried my hand at creating a crowdsale contract.

After sending my custom token to the contract and doing test purchases by sending ether to the crowdsale contract a issue is now at hand where the crowdsale has ended with the funded ether and the remaining custom tokens still in the crowdsale contract and is not being sent to the beneficiary. did I miss something out.

"funded Ether and remaining ico tokens are stuck in the crowsale contract"

bellow is the code used. " source ethereum.org"


pragma solidity ^0.4.18;

interface token { 
    function transfer(address receiver, uint amount) public ;

}

contract Crowdsale {
    address public beneficiary;
    uint public fundingGoal;
    uint public amountRaised;
    uint public deadline;
    uint public price;
    token public tokenReward;
    mapping(address => uint256) public balanceOf;
    bool fundingGoalReached = false;
    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
    ) public {
        beneficiary = ifSuccessfulSendTo;
        fundingGoal = fundingGoalInEthers * 1 ether;
        deadline = now + durationInMinutes * 1 minutes;
        price = etherCostOfEachToken * 0.0005 ether;
        tokenReward = token(addressOfTokenUsedAsReward);
    }

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

    modifier afterDeadline() { if (now >= deadline) _; }

    /**
     * Check if goal was reached
     *
     * Checks if the goal or time limit has been reached and ends the campaign
     */
    function checkGoalReached() private afterDeadline {
        if (amountRaised >= fundingGoal){
            fundingGoalReached = true;
            GoalReached(beneficiary, amountRaised);
        }
        crowdsaleClosed = true;
    }


    /**
     * Withdraw the funds
     *
     * Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached,
     * sends the entire amount to the beneficiary. If goal was not reached, each contributor can withdraw
     * the amount they contributed.
     */
    function safeWithdrawal() private 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;
            }
        }
    }
}
  • Did you call safeWithdrawal from the beneficiary's account? – user19510 Jan 18 '18 at 18:09
  • That's fine. I'm saying you have to call safeWithdrawal from the beneficiary account to get anything out of the contract. – user19510 Jan 18 '18 at 19:24
  • And unless I missed it, there's no way to get remaining tokens out of the contract. – user19510 Jan 18 '18 at 19:26
  • I'm not sure what you mean. If you call safeWithdrawal from the beneficiary's account, it should receive the ether (if the funding goal was met). – user19510 Jan 18 '18 at 19:36
  • ok could you provide me an example of calling safeWithdrawal from the beneficiary's account ? – R.D Jan 18 '18 at 19:37
0

You can use private function only from within contract, which mean all of functions you marked as private including safeWithdrawal won't work when you try to withdraw funds.

You should use public modifier instead of private if you want to access it from outside.

Have you tried it out on main net? In that case i am afriad your funds are lost forever. :(

  • Thank you. I used a test net, it's lost forever there. ;) – R.D Jan 19 '18 at 6:11

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.