question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uniswap TokenAmount JSBI to BigNumber

See original GitHub issue

The Uniswap SDK returns a TokenAmount object which contains a JSBI number from the following call: pair.reserveOf(uniToken1)

Output to console.log(pair.reserveOf(uniToken1)):

TokenAmount { numerator: JSBI(2) [ 709530937, 371, sign: false ], denominator: JSBI(1) [ 1000000, sign: false ], currency: Token { decimals: 6, symbol: ‘USDC’, name: undefined, chainId: 1, address: ‘0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48’ }, token: Token { decimals: 6, symbol: ‘USDC’, name: undefined, chainId: 1, address: ‘0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48’ } }

I then need to call the Uniswap Router v2 using ethers.js, and require the “amount” part of the above object in BigNumber.

.toString() or String(pair.reserveOf(uniToken1)) doesn’t seem to work, so I’ve been relegated to using: const res1Uni = ethers.BigNumber.from(pair.reserveOf(uniToken1).toFixed(0))

I’m guessing that for big reserve figures I might run into issues with the above and there’s probably a better way to do this conversion?

*Edit: Forget to mention, a direct conversion attempt doesn’t work (presumably because it’s a TokenAmount object?): const res0Uni = ethers.BigNumber.from(pair.reserveOf(uniToken0)) throws a “invalid BigNumber value” error.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rishanbcommented, Oct 10, 2020

Solved…this pair contained a token with 18 decimals and a token with 6 decimals. Somehow I got them switched (in some convoluted naming manner) and so it seems like the output from “reserveOf” was valid (i.e. not throwing an error) but incorrect, causing the JSBI to not be automatically converted to a BigNumber automatically as expected. TY for the help 😃

1reaction
ricmoocommented, Oct 10, 2020

That’s what the denominator is doing. It is removing those digits. 😃

So, if I use your numbers, I get this:

v = FixedNumber.from("1589263911247").divUnsafe(FixedNumber.from("1000000"))
FixedNumber {
  format:
   FixedFormat {
     signed: true,
     width: 128,
     decimals: 18,
     name: 'fixed128x18',
     _multiplier: '1000000000000000000' },
  _hex: '0x01508a2b54b611587ff000',
  _value: '1589263.911247',
  _isFixedNumber: true }

v.toString()
'1589263.911247'
Read more comments on GitHub >

github_iconTop Results From Across the Web

uniswap V2 invalid BigNumber value for JSBI negative value
The example tells you the value should be converted to hex: const value = trade.inputAmount.raw // // needs to be converted to e.g....
Read more >
How to get uniswap quote using a fractional input amount
i just needed to convert the token balance to token amount in the contract call, silly me :). Theres no such thing as...
Read more >
Integrating the Auto Router
For EXACT_INPUT swaps, this is the input token amount. ... gasPrice: BigNumber.from(route.gasPriceWei), ... fromRawAmount(currency, JSBI.
Read more >
sdk-core/currencyAmount.ts at main · Uniswap ...
Code shared across TypeScript Uniswap SDK versions ... import JSBI from 'jsbi' ... @param numerator the numerator of the fractional token amount.
Read more >
invalid bignumber string ethers - You.com | The AI Search ...
js application that will swap tokens through the router contract of Uniswap-like dex (it is called Ubeswap, a fork of the Uniswapv2 on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found