0

Hello i have this solidity code in the contract of a token, to receive ETH, send token to sender at an exchange rate and transfer ETH to owner, however on sending ETH to the contract, it fails. Please have an idea of what the issue might be


      function () public payable {
        fundTokens();
    }

    function fundTokens() public payable {
        require(msg.value > 0);
        uint256 tokens = msg.value.mul(exchangeRate);
       require(balances[this].sub(tokens) > 0);
        balances[msg.sender] = balances[msg.sender].add(tokens);
        balances[this] = balances[this].sub(tokens);
        Transfer(msg.sender, owner, msg.value);
       forwardFunds();
    }

    function forwardFunds() internal {
        owner.transfer(msg.value);
    }
    

1
  • Try this simple debugging technique - comment out all lines in fundTokens but the first one, try transactions. When it works - uncomment one more line and iterate.
    – Utgarda
    Commented Jun 7, 2021 at 17:39

2 Answers 2

1

You might need to check it https://docs.soliditylang.org/en/v0.8.4/contracts.html?highlight=receive#receive-ether-function. Your code would consumed whole 23,000 Gas.

In your case, you better to try this:

    address payable owner;
    
    receive() external payable {
        owner.transfer(msg.value);
    }
0

Guys thanks for your input, @Chiro Nobuhiro. I was able to solve the issue by increasing the gas limit, apparently the transaction needed more gas

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.