-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* formatAmount util function, use rounded shortened values in Bridge/Swap Input * Update Bridge/Swap page with parsed / formatted balances * Clean imports
- Loading branch information
1 parent
11f31ee
commit f274044
Showing
7 changed files
with
105 additions
and
30 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
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
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,69 @@ | ||
interface FormatOptions { | ||
fullAmount?: boolean | ||
standardDigits?: number | ||
useCompactNotation?: boolean | ||
compactDigits?: number | ||
minimumAmount?: number | ||
roundingMode?: string | ||
} | ||
|
||
export const formatAmount = ( | ||
amount: string, | ||
options?: FormatOptions | ||
): string => { | ||
if (amount === '') return '' | ||
|
||
const floatAmount = parseFloat(amount) | ||
|
||
try { | ||
if (!Number.isFinite(floatAmount)) { | ||
throw new TypeError(`"${amount}" is not a finite number`) | ||
} | ||
} catch ({ name, message }) { | ||
console.error(name, message) | ||
return amount | ||
} | ||
|
||
const fullAmount = options?.fullAmount ?? false | ||
const standardDigits = options?.standardDigits ?? 4 | ||
const useCompactNotation = options?.useCompactNotation ?? true | ||
const compactDigits = options?.compactDigits ?? useCompactNotation ? 2 : 0 | ||
const minimumAmount = options?.minimumAmount ?? 0.0001 | ||
|
||
const locales = 'en-UK' | ||
|
||
if (!floatAmount) return '0.0' | ||
|
||
if (fullAmount) return Intl.NumberFormat(locales).format(floatAmount) | ||
|
||
if (floatAmount < minimumAmount) return `< ${minimumAmount}` | ||
|
||
const absAmount = Math.abs(floatAmount) | ||
|
||
if (absAmount < 0.0001) | ||
return Intl.NumberFormat(locales, { | ||
maximumSignificantDigits: 1, | ||
}).format(floatAmount) | ||
|
||
if (absAmount < 1) | ||
return Intl.NumberFormat(locales, { | ||
minimumFractionDigits: standardDigits, | ||
}).format(floatAmount) | ||
|
||
if (absAmount < 1000) | ||
return Intl.NumberFormat(locales, { | ||
minimumSignificantDigits: standardDigits, | ||
maximumSignificantDigits: standardDigits, | ||
}).format(floatAmount) | ||
|
||
if (absAmount < 1000000) | ||
return Intl.NumberFormat(locales, { | ||
maximumFractionDigits: 0, | ||
}).format(floatAmount) | ||
|
||
return Intl.NumberFormat(locales, { | ||
minimumFractionDigits: compactDigits, | ||
maximumFractionDigits: compactDigits, | ||
notation: useCompactNotation ? 'compact' : 'standard', | ||
}).format(floatAmount) | ||
} |
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 |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import { trimTrailingZeroesAfterDecimal } from '@/utils/trimTrailingZeroesAfterDecimal' | ||
import { formatBigIntToString } from './bigint/format' | ||
import { hasOnlyZeroes } from './hasOnlyZeroes' | ||
|
||
export const getParsedBalance = ( | ||
balance: bigint, | ||
decimals: number, | ||
places?: number | ||
) => { | ||
const formattedBalance = formatBigIntToString(balance, decimals, places) | ||
const verySmallBalance = balance > 0n && hasOnlyZeroes(formattedBalance) | ||
|
||
return verySmallBalance ? '< 0.001' : formattedBalance | ||
return trimTrailingZeroesAfterDecimal( | ||
formatBigIntToString(balance, decimals, places) | ||
) | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/synapse-interface/utils/trimTrailingZeroesAfterDecimal.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