1

I have a dex contract and i want to swap ether with an ERC20 token, when i call a swap function a get this error:

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? execution reverted: ERC20: transfer amount exceeds balance { "originalError": { "code": 3, "data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002645524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e63650000000000000000000000000000000000000000000000000000", "message": "execution reverted: ERC20: transfer amount exceeds balance" } }

But i have enough ether for calling this function(as much as msg.value).

Here is my swap function:

function _swapWithEth () external payable {
        require(msg.value > 0 , "Amount zero");

        uint _amountInWithFee = (msg.value * 995) / 1000;
        
        uint256 ethPrice = getEthPrice();
        _amountOut =(_amountInWithFee * ethPrice / price);

        MYToken.transfer(msg.sender, _amountOut);

    }

Anyone know what's wrong? Thanks

1
  • Looking quickly at your contract, price is not defined, _amountOut is not declared as a uint and I believe you're looking to ascertain msg.sender.balance as opposed to msg.value, since the MYToken transfer should check and call a payable amount during execution of this integrated function. Try to re-structure.
    – immaxkent
    Dec 8, 2022 at 10:06

1 Answer 1

1

It looks like the _swapWithEth function is trying to transfer an ERC20 token to the sender, but the transfer is failing because the sender does not have enough balance to cover the amount being transferred.

The error message ERC20: transfer amount exceeds balance indicates that the sender's balance of the ERC20 token is less than the amount being transferred.

To fix this issue, you can either make sure that the sender has enough balance of the ERC20 token to cover the transfer, or you can modify the function to check the sender's balance before making the transfer and return an error if the balance is insufficient.

Here is an example of how you could modify the _swapWithEth function to check the sender's balance before making the transfer:

function _swapWithEth() external payable {
  require(msg.value > 0, "Amount zero");

  uint _amountInWithFee = (msg.value * 995) / 1000;
        
  uint256 ethPrice = getEthPrice();
  _amountOut =(_amountInWithFee * ethPrice / price);

  // Check the sender's balance before making the transfer
  require(MYToken.balanceOf(msg.sender) >= _amountOut, "Insufficient balance");

  // Make the transfer if the sender has enough balance
  MYToken.transfer(msg.sender, _amountOut);
}

This modification will check the sender's balance of the ERC20 token before making the transfer, and return an error if the balance is insufficient. This should prevent the transaction from failing with the ERC20: transfer amount exceeds balance error.

3
  • This is checking the sender's balance, while the transfer is from the contract to the msg.sender -> the check should be balanceOf(address(this)) ), ie the contract should hold enough tokens. Dec 8, 2022 at 16:31
  • it seems you right i haven't enough ERC20 token , because of decimals i deposit amount less than the amount that i need for this function, with more tokens i can solve this,Thanks
    – Alireza
    Dec 9, 2022 at 10:53
  • cheers, hope it's helpful to u.
    – david fnck
    Dec 9, 2022 at 12:47

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.