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
price
is not defined,_amountOut
is not declared as a uint and I believe you're looking to ascertainmsg.sender.balance
as opposed tomsg.value
, since theMYToken
transfer should check and call a payable amount during execution of this integrated function. Try to re-structure.