I deployed Uniswap contracts locally on Ganache. I was able to addLiquidity, process different swaps, but I can't now remove that liquidity. I just get 'VM Exception while processing transaction: revert ds-math-sub-underflow'.

I know that possibly I have to approve LP tokens to router contract, but I don't know how to do that properly. Because I manually deployed only Factory and Router.

Maybe someone can help me. Thanks in advance!

1 Answer 1


So I solved this problem by deploying my own contract that abstractly looks like this:

import "./../router/libraries/SafeMath.sol";
import "./../core/interfaces/IUniswapV2Pair.sol";
import "hardhat/console.sol";

contract Approver {
    using SafeMath for uint;

    // returns sorted token addresses, used to handle return values from pairs sorted in this order
    function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1) {
        require(tokenA != tokenB, 'UniswapV2Library: IDENTICAL_ADDRESSES');
        (token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
        require(token0 != address(0), 'UniswapV2Library: ZERO_ADDRESS');

    // calculates the CREATE2 address for a pair without making any external calls
    function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
        (address token0, address token1) = sortTokens(tokenA, tokenB);
        pair = address(uint(keccak256(abi.encodePacked(
                keccak256(abi.encodePacked(token0, token1)),
                hex'ca29708fdd31b503262d6a40c70a2bf4316b0193e0b6df6e194e1cbcfb0b4bde' // don't forget to change this init code on your specific 

    function approve(address from) public { //set account you added liquidity from address
        address pair = pairFor(factory_address,tokenA_address,tokenB_address);
        (bool result) = IUniswapV2Pair(pair).approve(from,router02_address,value); // send liquidity to pair
        require(result, "failed");

You can just call approve() func and try to remove liquidity method again.

Hope it'll help someone)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.