1

I'm using a crowdsale contract (listed below) and on Ropsten network I just set founding goal to 1ETH send it to crowdsale address and then I can withdraw to beneficiary address but when I send ETH a second time I can't withdraw it anymore.

I find myself in need of a Crowdsale code where I can withdraw founds anytime or a way to automatically send ETH from crowdsale to ETH address just like someone sends 1 ETH to crowdsale:

  • That person receives Tokens
  • Crowdsale sends that ETH to specific ETH address
  • Owner of that address can withdraw anytime needed / multiple times

Here's the code I'm trying to use but only 1st withdrawal works (second stucks)

pragma solidity ^0.4.18;

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

contract Crowdsale {
    address public beneficiary;
    uint public fundingGoal;
    uint public amountRaised;
    uint public price;
    token public tokenReward;
    mapping(address => uint256) public balanceOf;
    bool fundingGoalReached = false;


    event GoalReached(address recipient, uint totalAmountRaised);
    event FundTransfer(address backer, uint amount, bool isContribution);

    /**
     * Constructor function
     *
     * Setup the owner
     */
    constructor(
        address ifSuccessfulSendTo,
        uint fundingGoalInEthers,

        uint milietherCostOfEachToken,
        address addressOfTokenUsedAsReward
    ) public {
        beneficiary = ifSuccessfulSendTo;
        fundingGoal = fundingGoalInEthers * 1 ether;

        price = milietherCostOfEachToken * 0.0001 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 () payable public {

        uint amount = msg.value;
        balanceOf[msg.sender] += amount;
        amountRaised += amount;
        tokenReward.transfer(msg.sender, amount * 10**18 / price);
       emit FundTransfer(msg.sender, amount, true);
    }


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

    }


    /**
     * 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() public  {
        if (!fundingGoalReached) {
            uint amount = balanceOf[msg.sender];
            balanceOf[msg.sender] = 0;
            if (amount > 0) {
                if (msg.sender.send(amount)) {
                   emit FundTransfer(msg.sender, amount, false);
                } else {
                    balanceOf[msg.sender] = amount;
                }
            }
        }

        if (fundingGoalReached && beneficiary == msg.sender) {
            if (beneficiary.send(amountRaised)) {
               emit FundTransfer(beneficiary, amountRaised, false);
            } else {
                //If we fail to send the funds to beneficiary, unlock funders balance
                fundingGoalReached = false;
            }
        }
    }

1 Answer 1

1

To automaticaly send funds to beneficiary address you can use transfer

function () payable public {

    uint amount = msg.value;
    balanceOf[msg.sender] += amount;
    amountRaised += amount;
    tokenReward.transfer(msg.sender, amount * 10**18 / price);
    emit FundTransfer(msg.sender, amount, true);

    // Forward funds immediately to beneficiary
    beneficiary.transfer(amount);
}
2
  • I'm trying to deploy contract using mist on osx - it worked fine on testnet but when i try to do it on main net the transaction just hangs on information "Couldn't estimate gas, restoring to default parameters. Transaction is likely cheaper than the estimate" I have deployed token contract using byte code and MEW before but in this contract i need to provide token address used as reward token price beneficiary address and i just don't know how to do it using remix and bytecode Commented Sep 1, 2018 at 10:02
  • solved, problem was that ....uhmmmm I'm stupid ;) Commented Sep 1, 2018 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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