-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: better support decimal overflow (#2075)
* better support decimal overflow * improve tests * explicitly define num digits in toSignificant * add tests for 0 decimals * undo change to tryParseAmount and instead compute price without relying on parse * remove console log and add test * removed comment * addressed pr feedback * improve parsing logic * addressed pr feedback
- Loading branch information
Justin Domingue
committed
Jul 23, 2021
1 parent
311bdc1
commit e07599e
Showing
2 changed files
with
65 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Token } from '@uniswap/sdk-core' | ||
import { tryParsePrice } from './utils' | ||
|
||
describe('hooks', () => { | ||
describe('#tryParsePrice', () => { | ||
it('should return undefined if amount is not a number', () => { | ||
const baseToken = new Token(1, '0x6b175474e89094c44da98b954eedeac495271d0f', 6) | ||
const quoteToken = new Token(1, '0x1b175474e89094c44da98b954eedeac495271d0f', 6) | ||
|
||
expect(tryParsePrice(undefined, undefined, undefined)).toBeUndefined() | ||
expect(tryParsePrice(baseToken, quoteToken)).toBeUndefined() | ||
expect(tryParsePrice(baseToken, quoteToken, '')).toBeUndefined() | ||
expect(tryParsePrice(baseToken, quoteToken, 'abc.123')).toBeUndefined() | ||
expect(tryParsePrice(baseToken, quoteToken, '1.2.3')).toBeUndefined() | ||
expect(tryParsePrice(baseToken, quoteToken, '20.')).toEqual(undefined) | ||
}) | ||
|
||
it('should return a price', () => { | ||
const baseToken = new Token(1, '0x6b175474e89094c44da98b954eedeac495271d0f', 6) | ||
const quoteToken = new Token(1, '0x1b175474e89094c44da98b954eedeac495271d0f', 6) | ||
|
||
expect(tryParsePrice(baseToken, quoteToken, '20')?.toSignificant(6)).toEqual('20') | ||
expect(tryParsePrice(baseToken, quoteToken, '20.05')?.toSignificant(6)).toEqual('20.05') | ||
expect(tryParsePrice(baseToken, quoteToken, '20.123456789')?.toSignificant(6)).toEqual('20.1235') | ||
expect(tryParsePrice(baseToken, quoteToken, '0.123456789')?.toSignificant(6)).toEqual('0.123457') | ||
expect(tryParsePrice(baseToken, quoteToken, '.123456789')?.toSignificant(6)).toEqual('0.123457') | ||
expect( | ||
tryParsePrice( | ||
baseToken, | ||
quoteToken, | ||
(2 ** 128).toLocaleString('fullwide', { useGrouping: false }) | ||
)?.toSignificant(6) | ||
).toEqual('340282000000000000000000000000000000000') | ||
expect( | ||
tryParsePrice(baseToken, quoteToken, /* ~2^-128 */ '0.000000000000000000000000000587747')?.toSignificant(6) | ||
).toEqual('0.000000000000000000000000000587747') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e07599e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: