Uniswap TokenAmount JSBI to BigNumber
See original GitHub issueThe 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:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
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 😃
That’s what the denominator is doing. It is removing those digits. 😃
So, if I use your numbers, I get this: