1

I'm using Ropsten Testnet and Remix.

Is there an obvious error in this code? Why does not he accept eth.

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();
    }
}

The Crowdsale contract has an allowance. The gas limit has been increased in many ways. My ether wallet warning ! enter image description here

The full code is here: https://ropsten.etherscan.io/address/0x500c3ff2c1a561cd0fda1cf0c77fe0d17af5fda5

What I'm missing here?. Thank you in advance for your answer. The last question was unanswered, so I ask again !

2 Answers 2

1

the fallback function can't execute your buy() function as it has only a stipend of 2300 gas. check How much computation can be done in a fallback function?

so remove it from the fallback function and call it directly

function () payable public {  }

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();
    }
}
4
  • Thank you for answering. Thorkil Værge how to correct this. I'm not programmer. Without if (tokenReward.transferFrom(tokenReward.owner(), msg.sender, numTokens)) { , works fine. I'm just not sure when I will delete this lines I will be able to distribute tokens. Badr Bellaj - How to make this fallback function cheaper. What should I remowe?
    – Robert
    Apr 26, 2018 at 12:25
  • Badr Bellaj , Thank You for the correction but this give me error in Remix
    – Robert
    Apr 26, 2018 at 12:53
  • @Robert now it should be ok. don't forget to accept the answer if you think it's helpful
    – Badr Bellaj
    Apr 26, 2018 at 13:32
  • Badr Bellaj You was right. Works!
    – Robert
    Apr 26, 2018 at 14:31
0

The function call that is failing is a call to this method:

function setTokenOffering(address offeringAddr, uint256 amountForSale) external onlyOwner onlyTokenOfferingAddrNotSet {
        require(!transferEnabled);

        uint256 amount = (amountForSale == 0) ? TokenAllowance : amountForSale;
        require(amount <= TokenAllowance);

        approve(offeringAddr, amount);
        tokenAllowanceAddr = offeringAddr;

    }

You have already called the enableTransfer function which sets transferEnabled to true. So the first line in the above method require(!transferEnabled); will fail and the execution will stop.

Your Answer

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

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