Solidity – Reasons for addLiquidity() Function Reverting Every Time in Remix and Uniswap

remixsolidityuniswap

I am having issue with Uniswap addLiquidity() function. I tried a lot of things but I didn't. Here is my code. I am trying on remix.

edit: i am just trying add liquidity weth and dai not new pair.

pragma solidity 0.6.6;

import './IERC20.sol';
import './IUniswapV2Factory.sol';
import './IUniswapV2Pair.sol';
import './IUniswapV2Router01.sol';
import './IUniswapV2Router02.sol'; 

contract justLiquidity {
    address private constant UNISWAP_V2_ROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;

    
    function _addLiquidity (address _token1, address _token2, uint256 _amount1, uint256 _amount2) external {


        IERC20(_token1).transferFrom(msg.sender, address(this), _amount1); 
        IERC20(_token2).transferFrom(msg.sender, address(this), _amount2);
        IERC20(_token2).approve(UNISWAP_V2_ROUTER, _amount2 * 999);
        
        IERC20(_token1).approve(UNISWAP_V2_ROUTER, _amount1 * 999);
        



        IUniswapV2Router02(UNISWAP_V2_ROUTER).addLiquidity(_token1, _token2, _amount1, _amount2, 1, 1, address(this), block.timestamp); 

    }
}

what i am missing?

Best Answer

Finally I solved my problem. It doesn't work if I call approve() function for my contract. When I call approve() function in my wallet it worked.

Related Topic