UniswapV2 swapExactTokensForETH approve and transfer
ERC20: transfer amount exceeds allowance
This is a custom error message from a contract.
Based on the context, I'm assuming it's coming from the token
contract's function transferFrom()
(or another function called by this one), from a failed require()
condition.
Which means that the accounts[1]
(user executing the swapTokenToEth()
function) haven't approved the dex
contract to manipulate their tokens.
Solution: Before executing dex.swapTokenToEth()
, you also need to execute tokenContract.approve(dexAddress)
from the accounts[1]
address. It's sufficient to execute the approve()
function just once (unless you redeploy the contracts) with a sufficient amount - the approval decreases with each amount used in the transferFrom()
function.