[Ethereum] gas required exceeds allowance or always failing transaction

fallback-functiongasmyetherwalletremixsolidity

I really need help with this error. I'm testing the contract by sending ETH to Crowdsale contract .(Ropsten, My ether wallet, Remix).
Full code is here: https://ropsten.etherscan.io/address/0x500c3ff2c1a561cd0fda1cf0c77fe0d17af5fda5
This function give me really problem;
function () payable public {
buy();
}

function buy () payable public whenNotPaused beforeDeadline afterStartTime saleNotClosed {
    require(msg.value >= minContribution);

    // Update the sender's balance of wei contributed and the amount raised
    uint amount = msg.value;
    uint currentBalance = balanceOf[msg.sender];
    balanceOf[msg.sender] = currentBalance.add(amount);
    amountRaised = amountRaised.add(amount);

    // Compute the number of tokens to be rewarded to the sender
    // Note: it's important for this calculation that both wei
    // and PDT have the same number of decimal places (18)
    uint numTokens = amount.mul(rate);

    // Transfer the tokens from the crowdsale supply to the sender
    if (tokenReward.transferFrom(tokenReward.owner(), msg.sender, numTokens)) {
        FundTransfer(msg.sender, amount, true);
        // Check if the funding goal or cap have been reached
        // TODO check impact on gas cost
        checkFundingGoal();
        checkFundingCap();
    }
    else {
        revert();
    }
}

I thing tokenReward is not correctly defined.
Here works fine:
function () payable {
buy();
}

function buy ()
    payable public
    whenNotPaused
    beforeDeadline
    afterStartTime
    saleNotClosed
{
    require(msg.value >= minContribution);
    uint amount = msg.value;

    // Compute the number of tokens to be rewarded to the sender
    // Note: it's important for this calculation that both wei
    // and PDT have the same number of decimal places (18)
    uint numTokens = amount.mul(rate);

    // update the total amount raised
    amountRaised = amountRaised.add(amount);

    // update the sender's balance of wei contributed
    balanceOf[msg.sender] = balanceOf[msg.sender].add(amount);
    // add to the token balance of the sender
    tokenBalanceOf[msg.sender] = tokenBalanceOf[msg.sender].add(numTokens);

    FundTransfer(msg.sender, amount, true);
    // Check if the funding goal or cap have been reached
    checkFundingGoal();
    checkFundingCap();
}

Please for a real answer how to fix this problem.Robert

Best Answer

You need to either increase gas amount during a transaction, Or fix something that is failing like not enough allowance, not enough balance, any if statement failing, any require statement failing, any revert or throw ...

Related Topic