1

I'm working on V3 of Uniswap for the first time, and am trying to get the current price of a pool.

I'm using this tutorial page https://docs.uniswap.org/sdk/guides/fetching-prices, which certainly seems to attempt what I wish.

Further, the comments in the method token0Price states:

 /**
   * Returns the current mid-price of the pool in terms of token0, i.e. the ratio of token1 over token0
   */

However, when I call this function, what I receive is:

Price {
  numerator: JSBI(8) [
    445412081,   346709960,
    584891573,   611262682,
    360319882,   934240370,
    1035473620,  1369,
    sign: false
  ],
  denominator: JSBI(7) [ 0, 0, 0, 0, 0, 0, 4096, sign: false ],
  baseCurrency: Token {
    chainId: 3,
    decimals: 6,
    symbol: 'USDC',
    name: 'USD Coin',
    isNative: false,
    isToken: true,
    address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
  },
  quoteCurrency: Token {
    chainId: 3,
    decimals: 18,
    symbol: 'WETH',
    name: 'Wrapped Ether',
    isNative: false,
    isToken: true,
    address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
  },
  scalar: Fraction {
    numerator: JSBI(1) [ 1000000, sign: false ],
    denominator: JSBI(2) [ 660865024, 931322574, sign: false ]
  }
}

How do I translate this Price objects JSBI arrays into an actual price of the pool?

1
  • Uni's documentation is rather... lacking Commented May 6, 2022 at 15:49

1 Answer 1

2

These JSBI objects got a method toSignificant, use it to convert the big number into a number literal.

// toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;

const p = pool.token0Price.toSignificant(6);

will return a floating point number with precision of 6 digits.

1
  • Thank you - will look into submitting a PR to the project documentation that adds this. Commented May 4, 2022 at 18:50

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.