Solidity – How to Fix ‘Insufficient Allowance’ Error When Sending Transactions in Uniswap

solidityuniswap

I'm using UniswapV2Router02 contract and here is the problem.
When I try to call the swap function in Remix IDE it shows me

execution reverted: Dai/insufficient-allowance

Although I have DAI in the account that I'm sending the transaction with.
Here is the full code Click on this link and copy paste the code in your Remix IDE (Ignore the additional files if you're not familiar with codesandbox just the opened file code will be enough šŸ™‚ )

FULL CODE

and here is the values that i'm passing to swap function (since I'm using Remix tools to do it, I'm just assigning them here so that it's more clear)

_tokenIn: 0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735
_tokenOut: 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984
_amountIn: 100000
_minAmountOut: 0000001// I guess Iā€™m doing something wrong here please let me know if it's wrong
_to: 0x9b6582f3b8c262B062BDC779064F16aB026eE566// the account that i'm sure has DAI

I'm using Injected provider (MetaMask) for ENVIRONMENT

Best Answer

You need to call approve function. You need to approve DAI spender as contract address from your wallet account.

Here, spender is contract address

amount is _amountIn * 1e18

And you need to approve from your wallet account

Also remember, you have to send this transaction to DAI contract.

DAI.approve(spender, amount, {"from":account})