"Dai/insufficient-allowance" on transferFrom

Hi, I am having issue swapping back DAI tokens to ETH with uniswap interface, I am getting this error "Dai/insufficient-allowance" on the transaction especially this part require(allowance[src][msg.sender] >= wad, "Dai/insufficient-allowance"); my wallet and contract both hold DAI tokens 200 and 50 respectively and trying to swap 10 DAI. I don’t understand why I get insufficient balance error. How do I resolve this issue?

Here is the snippet of the part where I get the error

function Sell(uint tokenAmount) public payable {
    uint deadline = block.timestamp + 15; 
    
    require(token.transferFrom(msg.sender, address(this), tokenAmount), 'transferFrom failed.');
    require(token.approve(address(uniswapRouter), tokenAmount), 'approve failed.');
    
    // Swap DAI for ETH
    uniswapRouter.swapExactTokensForETH(tokenAmount, msg.value, getPathForTokentoETH(), address(this), deadline);
  }
1 Like

Hi @jmarrr,

Welcome to the community :wave:

Have you approved a sufficient allowance to the contract to use your tokens?

For an example of how this works see: Example on how to use ERC20 token in another contract