2

I am able to add liquidity and remove liquidity on uniswap, but I am having trouble calculating the min values for the token pairs.

For removing liquidity I am trying to get the amount of each token by doing the following:

const pathA = [liquidityToken.address, tokenA.address];
  const amountsA = await uniswap.getAmountsOut(liquidityAmount, pathA);
  const desiredAmtA = amountsA[1];

  const pathB = [liquidityToken.address, tokenB.address];
  const amountsB = await uniswap.getAmountsOut(liquidityAmount, pathA);
  const desiredAmtB = amountsB[1];

This is throwing an error, but I'm not sure how to get the amount of tokena/b that my liquidity token represents. And consequently, I"m trying to calculate the min from it like this. I don't understand how these slippage tolerances get calculated.

const minAmtA = desiredAmtA.mul(BigNumber.from(9)).div(BigNumber.from(10));
const minAmtB = desiredAmtB.mul(BigNumber.from(9)).div(BigNumber.from(10));

It works when I put 0, but obviously I don't want 100% slippage

    tokenA.address,
    tokenB.address,
    liquidityAmount,
    BigNumber.from(0), // don't know how to calculate this
    BigNumber.from(0), // don't know how to calculate this
    wallet.address,
    deadline,
    {
      gasLimit: process.env.GAS_LIMIT,
      gasPrice: 20e9,
    }
  );
1
  • what does balanceOf return for your LP token?
    – Huge
    Commented Jun 21, 2021 at 14:12

1 Answer 1

1

This is wrong for two reasons:

  1. You are not trading liquidity tokens for pool tokens, you're removing liquidity which is different. amountADesired & amountBDesired are not maintained as pairs with liquidity pool tokens.
  2. getAmountsOut and getAmountsIn are used for calculating trade prices, which is not the case in adding/removing liquidity.

Your liquidity tokens represent your share in the pool. So the formula for calculating amount of pooled tokens for certain liquidity would be :

amountADesired = (liquidity* reserveA)/totalSupply

This is what happens behind the scene in burn function of Pool Smart contract. Uniswap Pair burn method

Here's a code sample for removing liquidity, Rest of it is easy if you've come this far. Comment down if you need further help

    const toBN = web3.utils.toBN;
    const owner = '<Your address>'
    const pairContract = new web3.eth.Contract(getPairABI(), pairAddress);
    
    const reserves = await getReserves(tokenAName, tokenBName);// calling pairContract.getResrves()
    const totalSupply = await pairContract.methods.totalSupply().call();
    const ownerBalance = await pairContract.methods.balanceOf(owner).call();

    console.log('LIQUIDITY SHARE', web3.utils.toBN(ownerBalance).div(toBN(totalSupply)).mul(toBN(100)).toNumber());
    const liquidityPerc = 50;
    const liquidity = toBN(ownerBalance).mul(toBN(liquidityPerc)).div(toBN(100));

    const amountA = liquidity.mul(toBN(reserves[0])).div(toBN(totalSupply));
    const amountB = liquidity.mul(toBN(reserves[1])).div(toBN(totalSupply));
    console.log('HOlDING AMOUNTS', amountA.toString(), amountB.toString());
    //5% slippage
    var amountAMin = toBN(amountA).div(toBN(100)).mul(toBN(95));
    var amountBMin = toBN(amountB).div(toBN(100)).mul(toBN(95));
    console.log('MIN AMOUNTS', amountAMin.toString(), amountBMin.toString());
    
    var routerAbi = getRouterABI();
    var contract = new web3.eth.Contract(routerAbi, getRouterAddress(dex));

    var data = contract.methods.removeLiquidity(
        getTokenAddress(tokenAName),
        getTokenAddress(tokenBName),
        web3.utils.toHex(liquidity),
        web3.utils.toHex(amountAMin),
        web3.utils.toHex(amountBMin),
        targetAccount.address,
        web3.utils.toHex(Math.round(Date.now()/1000)+60*200), // in seconds
    );

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.