I have a token-tez pool on quipuswap v2 dex.

I want to calculate how much tez should I provide to dex to buy back the x amount of tokens from the dex.

What is the formula to calculate this?

How can I do that?


1 Answer 1


The approach is similar to V1.

You need to get reserves and supply using view in the same way as you did before and then use the approach below to calculate the result.

Here is a reference implementation in TS for calculating how many token A you have to provide to buy bTokenAmount of token b:

export const findAmmSwapInput = (
  bTokenAmount: BigNumber,
  pair: RoutePairWithDirection
) => {
  const feeRatio = getPairFeeRatio(pair);
  const numerator = pair.aTokenPool.times(bTokenAmount);
  const denominator = pair.bTokenPool.minus(bTokenAmount).times(feeRatio);
  const input = numerator.idiv(denominator).plus(1);
  return input.isGreaterThan(0) ? input : new BigNumber(Infinity);

Notes: idiv - division where only integer part is left (ex. 3.idiv(2) = 1) feeRatio - 0.9965

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.