Skip to content

Commit 16bcf5c

Browse files
authored
Merge pull request #5758 from EdgeApp/william/fiat-auto-precision
Default auto-precision to true in `useFiatText`
2 parents cd9cb78 + 5f31263 commit 16bcf5c

File tree

5 files changed

+74
-48
lines changed

5 files changed

+74
-48
lines changed

eslint.config.mjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ export default [
344344
'src/components/themed/DividerLine.tsx',
345345
'src/components/themed/EdgeProviderComponent.tsx',
346346
'src/components/themed/EdgeText.tsx',
347-
'src/components/themed/ExchangeQuoteComponent.tsx',
348347
'src/components/themed/ExplorerCard.tsx',
349348
'src/components/themed/Fade.tsx',
350349

@@ -399,7 +398,6 @@ export default [
399398
'src/components/tiles/FiatAmountTile.tsx',
400399
'src/components/tiles/InterestRateChangeTile.tsx',
401400
'src/components/tiles/LtvRatioTile.tsx',
402-
'src/components/tiles/NetworkFeeTile.tsx',
403401
'src/components/tiles/PercentageChangeArrowTile.tsx',
404402
'src/components/tiles/TotalDebtCollateralTile.tsx',
405403
'src/constants/WalletAndCurrencyConstants.ts',
@@ -423,7 +421,6 @@ export default [
423421
'src/hooks/useBackButtonToast.tsx',
424422
'src/hooks/useCryptoText.ts',
425423
'src/hooks/useExperimentConfig.ts',
426-
'src/hooks/useFiatText.ts',
427424
'src/hooks/useFilter.ts',
428425
'src/hooks/useHistoricalRate.ts',
429426
'src/hooks/useIconColor.ts',

src/__tests__/scenes/__snapshots__/SwapConfirmationScene.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ exports[`SwapConfirmationScene should render with loading props 1`] = `
14211421
]
14221422
}
14231423
>
1424-
€0.00
1424+
€0.000000
14251425
</Text>
14261426
</View>
14271427
</View>

src/components/themed/ExchangeQuoteComponent.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { useTokenDisplayData } from '../../hooks/useTokenDisplayData'
99
import { lstrings } from '../../locales/strings'
1010
import { convertCurrency } from '../../selectors/WalletSelectors'
1111
import { useSelector } from '../../types/reactRedux'
12-
import { getWalletTokenId } from '../../util/CurrencyInfoHelpers'
1312
import { fixSides, mapSides, sidesToMargin } from '../../util/sides'
1413
import { DECIMAL_PRECISION, removeIsoPrefix } from '../../util/utils'
1514
import { EdgeCard } from '../cards/EdgeCard'
@@ -24,7 +23,7 @@ interface Props {
2423
showFeeWarning?: boolean | null
2524
}
2625

27-
export const ExchangeQuote = (props: Props) => {
26+
export const ExchangeQuote: React.FC<Props> = props => {
2827
const { fromTo, quote, showFeeWarning } = props
2928
const { request, fromNativeAmount, toNativeAmount, networkFee } = quote
3029
const { fromWallet, fromTokenId, toWallet, toTokenId } = request
@@ -37,7 +36,7 @@ export const ExchangeQuote = (props: Props) => {
3736

3837
// Fees are assumed to be denominated in the native currency
3938
const feeNativeAmount = networkFee.nativeAmount
40-
const feeTokenId = getWalletTokenId(fromWallet, networkFee.currencyCode)
39+
const feeTokenId = networkFee.tokenId
4140
const feeCryptoText = useCryptoText({
4241
wallet: fromWallet,
4342
nativeAmount: feeNativeAmount,
@@ -56,14 +55,16 @@ export const ExchangeQuote = (props: Props) => {
5655
})
5756

5857
const feeFiatText = useFiatText({
59-
autoPrecision: true,
60-
pluginId: fromWallet.currencyInfo.pluginId,
61-
tokenId: feeTokenId,
6258
cryptoExchangeMultiplier: feeDenomination.multiplier,
6359
isoFiatCurrencyCode,
6460
nativeCryptoAmount: feeNativeAmount,
61+
pluginId: fromWallet.currencyInfo.pluginId,
62+
tokenId: feeTokenId,
63+
64+
autoPrecision: true,
6565
hideFiatSymbol: true,
66-
subCentTruncation: true
66+
subCentTruncation: true,
67+
displayZeroAsInteger: false
6768
})
6869

6970
const feeFiatAmount = useSelector(state => {
@@ -121,7 +122,7 @@ export const ExchangeQuote = (props: Props) => {
121122
label: React.ReactNode,
122123
value: React.ReactNode,
123124
style: any = {}
124-
) => {
125+
): React.ReactNode => {
125126
return (
126127
<View style={[styles.row, style]}>
127128
<View style={styles.label}>{label}</View>
@@ -130,11 +131,10 @@ export const ExchangeQuote = (props: Props) => {
130131
)
131132
}
132133

133-
const renderBottom = () => {
134+
const renderBottom = (): React.ReactNode => {
134135
if (fromTo === 'from') {
135-
const feeTextStyle = showFeeWarning
136-
? styles.bottomWarningText
137-
: styles.bottomText
136+
const feeTextStyle =
137+
showFeeWarning === true ? styles.bottomWarningText : styles.bottomText
138138

139139
return (
140140
<View style={styles.bottomContainer}>

src/components/tiles/NetworkFeeTile.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface Props {
1717
}
1818

1919
// TODO: Integrate into SendScene, FlipInputModal, and AdvancedDetailsModal
20-
export const NetworkFeeTile = (props: Props) => {
20+
export const NetworkFeeTile: React.FC<Props> = props => {
2121
const { wallet, nativeAmount } = props
2222
const { currencyConfig } = wallet
2323

@@ -66,14 +66,16 @@ export const NetworkFeeTile = (props: Props) => {
6666
})
6767

6868
const feeFiatAmount = useFiatText({
69-
appendFiatCurrencyCode: false,
70-
autoPrecision: true,
69+
cryptoExchangeMultiplier: exchangeDenominationMultiplier,
70+
isoFiatCurrencyCode: defaultIsoFiat,
71+
nativeCryptoAmount: nativeAmount,
7172
pluginId: wallet.currencyInfo.pluginId,
7273
tokenId: null,
73-
cryptoExchangeMultiplier: exchangeDenominationMultiplier,
74+
75+
appendFiatCurrencyCode: false,
76+
autoPrecision: true,
7477
fiatSymbolSpace: true,
75-
isoFiatCurrencyCode: defaultIsoFiat,
76-
nativeCryptoAmount: nativeAmount
78+
displayZeroAsInteger: false
7779
})
7880

7981
const title = lstrings.loan_estimate_fee

src/hooks/useFiatText.ts

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,63 @@ import { useSelector } from '../types/reactRedux'
1212
import { DECIMAL_PRECISION, removeIsoPrefix, zeroString } from '../util/utils'
1313

1414
const defaultMultiplier = Math.pow(10, DECIMAL_PRECISION).toString()
15+
1516
interface Props {
16-
appendFiatCurrencyCode?: boolean
17-
autoPrecision?: boolean
18-
pluginId: string
19-
tokenId: EdgeTokenId
2017
cryptoExchangeMultiplier?: string
21-
fiatSymbolSpace?: boolean
22-
hideFiatSymbol?: boolean
18+
nativeCryptoAmount?: string
2319
isoFiatCurrencyCode?: string
20+
pluginId: string
21+
tokenId: EdgeTokenId
22+
2423
maxPrecision?: number
2524
minPrecision?: number
26-
nativeCryptoAmount?: string
25+
26+
/** Show the fiat name after the number, like "USD" */
27+
appendFiatCurrencyCode?: boolean
28+
29+
/**
30+
* Automatically add more decimal places to show small numbers.
31+
* Defaults to true.
32+
*/
33+
autoPrecision?: boolean
34+
35+
/** Show 0 as "0" without any decimals. Defaults to true. */
36+
displayZeroAsInteger?: boolean
37+
38+
/** Put a space after the fiat symbol, like "$ 1.00" */
39+
fiatSymbolSpace?: boolean
40+
41+
/** Show a placeholder instead of the value */
42+
hideBalance?: boolean
43+
44+
/** Remove the fiat symbol (no $) */
45+
hideFiatSymbol?: boolean
46+
47+
/** Don't group digits for long numbers */
2748
noGrouping?: boolean
49+
50+
/** Round small values to "0.01" */
2851
subCentTruncation?: boolean
29-
hideBalance?: boolean
3052
}
3153

3254
export const useFiatText = (props: Props): string => {
3355
const {
34-
appendFiatCurrencyCode,
35-
autoPrecision = false,
36-
pluginId,
37-
tokenId,
3856
cryptoExchangeMultiplier = defaultMultiplier,
39-
fiatSymbolSpace = false,
40-
hideFiatSymbol,
57+
nativeCryptoAmount = cryptoExchangeMultiplier,
4158
isoFiatCurrencyCode = USD_FIAT,
59+
pluginId,
60+
tokenId,
4261
maxPrecision,
4362
minPrecision,
44-
nativeCryptoAmount = cryptoExchangeMultiplier,
45-
noGrouping,
46-
subCentTruncation,
47-
hideBalance = false
63+
64+
appendFiatCurrencyCode = false,
65+
autoPrecision = true,
66+
displayZeroAsInteger = true,
67+
fiatSymbolSpace = false,
68+
hideBalance = false,
69+
hideFiatSymbol = false,
70+
noGrouping = false,
71+
subCentTruncation = false
4872
} = props
4973

5074
// Convert native to fiat amount.
@@ -68,18 +92,20 @@ export const useFiatText = (props: Props): string => {
6892
const isSubCentTruncationActive =
6993
subCentTruncation && lt(abs(fiatAmount), '0.01')
7094

71-
// Convert the amount to an internationalized string or '0'
95+
// Use a placeholder if we are hidding the balance:
7296
const fiatString = hideBalance
7397
? lstrings.redacted_placeholder
74-
: autoPrecision || !zeroString(fiatAmount)
75-
? formatFiatString({
98+
: // Flatten 0's:
99+
displayZeroAsInteger && zeroString(fiatAmount)
100+
? '0'
101+
: // Normal decimal formatting:
102+
formatFiatString({
76103
fiatAmount: isSubCentTruncationActive ? '0.01' : fiatAmount,
77104
autoPrecision,
78105
minPrecision,
79106
maxPrecision: isSubCentTruncationActive ? 2 : maxPrecision,
80107
noGrouping
81108
})
82-
: '0'
83109

84110
const lessThanSymbol = isSubCentTruncationActive ? '<' : ''
85111
const fiatSymbol = hideFiatSymbol
@@ -101,10 +127,10 @@ export const formatFiatString = (props: {
101127
maxPrecision?: number
102128
}): string => {
103129
const {
130+
autoPrecision = true,
104131
fiatAmount,
105-
minPrecision = 2,
106132
maxPrecision = 6,
107-
autoPrecision = true,
133+
minPrecision = 2,
108134
noGrouping = false
109135
} = props
110136

@@ -142,10 +168,11 @@ export const displayFiatAmount = (
142168
fiatAmount?: number | string,
143169
precision: number = 2,
144170
noGrouping: boolean = true
145-
) => {
171+
): string => {
146172
const fiatAmountBns = fiatAmount != null ? add(fiatAmount, '0') : undefined
147-
if (fiatAmountBns == null || fiatAmountBns === '0')
173+
if (fiatAmountBns == null || fiatAmountBns === '0') {
148174
return precision > 0 ? formatNumber('0.' + '0'.repeat(precision)) : '0'
175+
}
149176
const absoluteAmount = abs(fiatAmountBns)
150177
return formatNumber(toFixed(absoluteAmount, precision, precision), {
151178
noGrouping

0 commit comments

Comments
 (0)