-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix weird issues with rounding (#5822)
* addresses most of the issues with stablecoins and beyond 2nd order magnitude rounding * add test suite * rm console * fix precision rounding function * fix some safe math rounding on max amount * rm old function * fix lint * clean up numberFormatter * revert change to safemath * fix some flakiness * remove logs app wide * add more tests * Apply suggestions from code review * add constants and adjust fallbacks
- Loading branch information
Showing
8 changed files
with
220 additions
and
36 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
98 changes: 98 additions & 0 deletions
98
src/__swaps__/screens/Swap/__tests__/NiceIncrementerFormatter.test.ts
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,98 @@ | ||
import { niceIncrementFormatter } from '@/__swaps__/utils/swaps'; | ||
import { SLIDER_WIDTH } from '../constants'; | ||
|
||
type TestCase = { | ||
incrementDecimalPlaces: number; | ||
inputAssetBalance: number | string; | ||
assetBalanceDisplay: string; | ||
inputAssetUsdPrice: number; | ||
niceIncrement: number | string; | ||
percentageToSwap: number; | ||
sliderXPosition: number; | ||
stripSeparators?: boolean; | ||
isStablecoin?: boolean; | ||
} & { | ||
testName: string; | ||
expectedResult: string; | ||
}; | ||
|
||
const TEST_CASES: TestCase[] = [ | ||
{ | ||
incrementDecimalPlaces: 0, | ||
inputAssetBalance: 45.47364224817269, | ||
assetBalanceDisplay: '45.47364225', | ||
inputAssetUsdPrice: 0.9995363790000001, | ||
niceIncrement: '1', | ||
percentageToSwap: 0.5, | ||
sliderXPosition: SLIDER_WIDTH / 2, | ||
stripSeparators: true, | ||
isStablecoin: true, | ||
testName: 'DAI Stablecoin', | ||
expectedResult: '22.74', | ||
}, | ||
{ | ||
incrementDecimalPlaces: 2, | ||
inputAssetBalance: 100, | ||
assetBalanceDisplay: '100.00', | ||
inputAssetUsdPrice: 10, | ||
niceIncrement: '0.1', | ||
percentageToSwap: 0, | ||
sliderXPosition: 0, | ||
stripSeparators: false, | ||
isStablecoin: false, | ||
testName: 'Zero percent swap', | ||
expectedResult: '0.00', | ||
}, | ||
{ | ||
incrementDecimalPlaces: 2, | ||
inputAssetBalance: 100, | ||
assetBalanceDisplay: '100.00', | ||
inputAssetUsdPrice: 10, | ||
niceIncrement: '0.1', | ||
percentageToSwap: 1, | ||
sliderXPosition: SLIDER_WIDTH, | ||
stripSeparators: false, | ||
isStablecoin: false, | ||
testName: 'Full swap', | ||
expectedResult: '100.00', | ||
}, | ||
{ | ||
incrementDecimalPlaces: 2, | ||
inputAssetBalance: 123.456, | ||
assetBalanceDisplay: '123.46', | ||
inputAssetUsdPrice: 1, | ||
niceIncrement: '0.05', | ||
percentageToSwap: 0.25, | ||
sliderXPosition: SLIDER_WIDTH / 4, | ||
stripSeparators: true, | ||
isStablecoin: false, | ||
testName: 'Quarter swap with fractional increment', | ||
expectedResult: '30.86', | ||
}, | ||
{ | ||
incrementDecimalPlaces: 0, | ||
inputAssetBalance: '1000', | ||
assetBalanceDisplay: '1,000', | ||
inputAssetUsdPrice: 0.5, | ||
niceIncrement: '100', | ||
percentageToSwap: 0.75, | ||
sliderXPosition: (3 * SLIDER_WIDTH) / 4, | ||
stripSeparators: false, | ||
isStablecoin: false, | ||
testName: 'Large increment test', | ||
expectedResult: '750', | ||
}, | ||
]; | ||
|
||
describe('NiceIncrementFormatter', () => { | ||
beforeAll(() => { | ||
jest.mock('react-native-reanimated', () => require('react-native-reanimated/mock')); | ||
}); | ||
|
||
TEST_CASES.forEach(({ testName, expectedResult, ...params }, index) => { | ||
// eslint-disable-next-line jest/valid-title | ||
test(testName || `test-${index}`, () => { | ||
expect(niceIncrementFormatter({ ...params })).toBe(expectedResult); | ||
}); | ||
}); | ||
}); |
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
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
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
Oops, something went wrong.