2

am using Uniswap V3: Router 2 swapExactTokensForTokens to make simple swaps, eg swap usdc to weth.

The issue am facing is in every and each example found online amountOutMin is defined to 0 , which allows 100% slippage and is not good.

encodeFunctionData("swapExactTokensForTokens",[ammount,"0",[token,"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"],public_key])

I have not found solution for this that would not require me to use uniswap sdk. Which is pointless since am doing this in pure js.

  • note ammount is in token native decimal

To do this you need to:

  1. Query the right pool from uniswap factory 0x1F98431c8aD98523631AE4a59f267346ea31F984 , use 1% fee (10000)
  2. Query price from the pool using the slot0 function, this returns price
  3. price = (sqrtPriceX96 ** 2 / 2 ** 192)
  4. min_received = (ammount*(sqrtPriceX96 ** 2 / 2 ** 192))*0.9 (for 10% slippage)
  • this should return value in decimals of token you are trying to swap to This works but not for tokens with 6 decimals ? huh

Am i missing smt ?

For example in this pool the price should equal 820000000000000wei which is 0.00082 or 1USD at the time

https://etherscan.io/address/0xC5aF84701f98Fa483eCe78aF83F11b6C38ACA71D#readContract

But it equals 1.2235422586602433e-9 or 0.0000000012235422586602433

is this issue in fetching the pool in first place or i need to add decimals calculation

SOLUTION:

-check token0 in given pool, if it is the currency you want output in, use:

2 ** 192 / sqrtPriceX96 ** 2 * 10 ** (decimals1-decimals0)

-if token 1 is currency you want output in, use:

sqrtPriceX96 ** 2 / 2 ** 192 * 10** (decimals1-decimals0)

1 Answer 1

0

So it looks like you read the "sqrtPriceX96" by querying the "slot0" function on the pair contract, then you call the "swap" method on the pair contract. this takes recipient, a true or false which indicated which way you are swapping, the amount, and the "sqrtPriceX96". It also looks like they have a "Quoter" contract that gives you the amount out for the frontend I assume.

Here are some links to the new interfaces:

pool read fuctions: https://docs.uniswap.org/protocol/reference/core/interfaces/pool/IUniswapV3PoolState#slot0

pool write functions: https://docs.uniswap.org/protocol/reference/core/interfaces/pool/IUniswapV3PoolActions#swap

quoter functions: https://docs.uniswap.org/protocol/reference/periphery/interfaces/IQuoter

15
  • by ammount you mean for example ammount of APE in trade APE to WETH ? Dont i need to know the price of APE in WETH to calculate amountOutMin?
    – sijog70374
    Nov 13, 2022 at 15:51
  • Expected ammout out would be (PRICEINWETH * AMMOUNTOFAPE)*0.99 (for 1 % slippage) But we still need that price in WETH which i have no clue how to get
    – sijog70374
    Nov 13, 2022 at 16:00
  • I edited my answer to show you how to get the amountOut hope this helps! let me know if you have any questions Nov 13, 2022 at 16:39
  • This is exactly what i need but for pancakeswap and bsc.
    – sijog70374
    Nov 13, 2022 at 18:38
  • Uniswap v2 used to have simmilar approach, no clue what to do on V3 tho
    – sijog70374
    Nov 13, 2022 at 18:39

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.