1

I'm trying to remove the liquidity previously added by the contract itself.

function getPoolPair() public view returns (address) {
    return IPancakeFactory( pancakeRouter.factory() ).getPair( address(this), pancakeRouter.WETH() );
}


function getPoolLiquidityInBNB() public view returns(uint256){
    return IERC20( getPoolPair() ).balanceOf( address(this) );
}

function addBalanceToLiquidity() public {
    approve( address(this), routerAddress, 1000 );
    
    pancakeRouter.addLiquidityETH{value : address(this).balance}(
        address(this),
        1000,
        0, // slippage is unavoidable
        0, // slippage is unavoidable
        address(this),
        block.timestamp + 360
    );
    
}

function removeLiquidity() public onlyOwner {
    
    uint256 poolLiquidity = getPoolLiquidityInBNB();
    
    approve( routerAddress,  getPoolPair(), poolLiquidity );
    
    pancakeRouter.removeLiquidityETH(
        address(this),
        poolLiquidity,
        0,
        0,
        address(this),
        block.timestamp + 360        
    );
}

The liq was created without any problem but when I try to remove it I receive the error:

ds-math-sub-underflow

What is the correct way to use removeLiquidityETH ?

1 Answer 1

2

The LP token has to be approved for spending by the Router contract. Also, check the balances of the LP tokens that you have to be exact amounts. Another thing is to make sure if you are removing all liquidity to calculate it like this

uint256 public constant MINIMUM_LIQUIDITY = 10**3;
 lpTokenAmount = (Math.sqrt(tokenBInputAmount * tokenAInputAmount)) -  MINIMUM_LIQUIDITY;

Math.sol contract can be found here: https://github.com/dapphub/ds-math/blob/master/src/math.sol

Your Answer

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

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