5

I need to quote online prices of different token pairs through uniswap-v2 API. As the below picture shows, I tend to pass token pair (maybe with base token desired amount) to the API and get all price related values as result.

enter image description here

I found Fetcher.fetchPairData method in uniswap documentions, but it does not return the correct values. Am I using the correct method or I have to use another method for this purpose?

2

This has been a pain for me for a long time. You basically have to compare the liquidity of the different pairs and run the Uniswap formula for calculating the price.

Y * I / (X + I)

  • I is your input amount of source tokens
  • X is the balance of the pool in the source token
  • Y is the balance of the pool in the target token

In the end, the process we came up to get the price of all Uniswap pairs was the following.

  1. Find all token pairs by looking at all historical PairCreated events.
  2. Read the reserves from token0 and token1 from the pair
  3. Calculate the price of token0 and token1 in usd according to the uniswap formula.

This took us a lot of work to do, especially when you need it for multiple tokens and on multiple chains (maybe you want the same on pancake).

Anyway, we took our solution and we turned it into an API you can use if you want. It's the Moralis Deep Index API. You simply run

/token/ERC20/{TOKEN_ADDRESS}/price

This will give you the exact same price you see on, for example, Etherscan. It currently supports ethereum, bsc and polygon. And it's free.

Disclaimer, I work at moralis and we developed this API.

0

Sticking to Uniswap SDK terms, a "pair price" strictly speaking does not exist. The closest thing to it is the MidPrice of a given Route.

However, your screenshot indicates that what you're really looking for is the ExecutionPrice of a Trade:

import { ChainId, Token, WETH, Fetcher, Trade, Route, TokenAmount, TradeType } from '@uniswap/sdk'

const DAI = new Token(ChainId.MAINNET, '0x6B175474E89094C44Da98b954EedeAC495271d0F', 18)
const pair = await Fetcher.fetchPairData(DAI, WETH[DAI.chainId])    
const route = new Route([pair], WETH[DAI.chainId])    
const trade = new Trade(route, new TokenAmount(WETH[DAI.chainId], '1000000000000000000'), TradeType.EXACT_INPUT)

console.log(trade.executionPrice.toSignificant(6))
4
  • @pwagnet Thanks for your answer but I already have tested executionPrice and its value is not even close to what I see through UniSwap UI. Mar 26 at 10:09
  • 1
    @pwagner I've asked similar quesiton in here: stackoverflow.com/questions/66944736/…. Why some prices fetched, with similar code to yours are so accurate and some are not even close?
    – Karzel
    Apr 5 at 18:10
  • 1
    I guess the issue is that "Disable Multihops" is not turned on in the Uniswap UI settings.
    – pwagner
    Apr 5 at 18:20
  • What to do when the pair of the tokens does not exists ? For example If i want to retrive the Route object for the following tokens TOKEN-A: 0x3301ee63fb29f863f2333bd4466acb46cd8323e6 TOKEB-B: 0xf32aa187d5bc16a2c02a6afb7df1459d0d107574
    – Mat.C
    Sep 19 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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