-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4422 from EdgeApp/jon/fio-req-denom
- fixed: Properly factor user currency display denomination settings in FIO requests.
- Loading branch information
Showing
5 changed files
with
127 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,38 @@ | ||
import { div, log10 } from 'biggystring' | ||
import { EdgeDenomination } from 'edge-core-js' | ||
import { EdgeCurrencyWallet } from 'edge-core-js' | ||
|
||
import { formatNumber } from '../locales/intl' | ||
import { | ||
DECIMAL_PRECISION, | ||
decimalOrZero, | ||
DEFAULT_TRUNCATE_PRECISION, | ||
maxPrimaryCurrencyConversionDecimals, | ||
precisionAdjust, | ||
truncateDecimals as nonLocalTruncateDecimals, | ||
zeroString | ||
} from '../util/utils' | ||
import { getDisplayDenomination } from '../selectors/DenominationSelectors' | ||
import { useSelector } from '../types/reactRedux' | ||
import { getCryptoText } from '../util/cryptoTextUtils' | ||
import { useTokenDisplayData } from './useTokenDisplayData' | ||
|
||
interface UseCryptoTextParams { | ||
displayDenomination: EdgeDenomination | ||
exchangeDenomination: EdgeDenomination | ||
exchangeRate?: string | ||
fiatDenomination: EdgeDenomination | ||
interface Props { | ||
nativeAmount: string | ||
currencyCode?: string | ||
tokenId?: string | ||
wallet: EdgeCurrencyWallet | ||
withSymbol?: boolean | ||
} | ||
|
||
/** | ||
* Get the numeric crypto text string value | ||
* TODO: Break this up once crypto & fiat text display logic is centralized into the appropriate text hooks/components. The order of operations should always be as follows: | ||
* 1. Numeric calculations | ||
* 2. Display Denomination | ||
* 3. Localization: commas, decimals, spaces | ||
*/ | ||
export const useCryptoText = ({ | ||
displayDenomination, | ||
exchangeDenomination, | ||
fiatDenomination, | ||
exchangeRate, | ||
nativeAmount, | ||
currencyCode | ||
}: UseCryptoTextParams) => { | ||
const { multiplier: displayMultiplier, symbol } = displayDenomination | ||
const { multiplier: exchangeMultiplier } = exchangeDenomination | ||
if (zeroString(nativeAmount)) return `${symbol ? symbol + ' ' : ''}0${currencyCode ? ' ' + currencyCode : ''}` | ||
let maxConversionDecimals = DEFAULT_TRUNCATE_PRECISION | ||
|
||
if (exchangeRate != null && parseFloat(exchangeRate) > 0) { | ||
const precisionAdjustValue = precisionAdjust({ | ||
primaryExchangeMultiplier: exchangeMultiplier, | ||
secondaryExchangeMultiplier: fiatDenomination.multiplier, | ||
exchangeSecondaryToPrimaryRatio: exchangeRate | ||
}) | ||
maxConversionDecimals = maxPrimaryCurrencyConversionDecimals(log10(displayMultiplier), precisionAdjustValue) | ||
} | ||
|
||
try { | ||
const preliminaryCryptoAmount = nonLocalTruncateDecimals(div(nativeAmount, displayMultiplier, DECIMAL_PRECISION), maxConversionDecimals) | ||
const finalCryptoAmount = formatNumber(decimalOrZero(preliminaryCryptoAmount, maxConversionDecimals)) // check if infinitesimal (would display as zero), cut off trailing zeroes | ||
|
||
if (currencyCode != null) { | ||
// Display with currency code if provided | ||
return `${finalCryptoAmount} ${currencyCode}` | ||
} | ||
|
||
// Display with symbol (if available) | ||
return `${symbol != null ? symbol + ' ' : ''}${finalCryptoAmount}` | ||
} catch (error: any) { | ||
if (error.message === 'Cannot operate on base16 float values') { | ||
const errorMessage = `${error.message}: Currency - ${exchangeDenomination.name}, amount - ${nativeAmount}, demonination multiplier: ${displayMultiplier}, exchange multiplier: ${exchangeMultiplier}` | ||
console.error(errorMessage) | ||
} | ||
console.error(error) | ||
} | ||
|
||
return '' | ||
export const useCryptoText = ({ wallet, tokenId, nativeAmount, withSymbol }: Props): string => { | ||
const { | ||
denomination: exchangeDenomination, | ||
fiatDenomination, | ||
assetToFiatRate | ||
} = useTokenDisplayData({ | ||
tokenId, | ||
wallet | ||
}) | ||
const displayDenomination = useSelector(state => | ||
getDisplayDenomination(state, wallet.currencyInfo.pluginId, exchangeDenomination.name ?? wallet.currencyInfo.currencyCode) | ||
) | ||
|
||
const cryptoText = getCryptoText({ | ||
displayDenomination, | ||
exchangeDenomination, | ||
exchangeRate: assetToFiatRate, | ||
fiatDenomination, | ||
nativeAmount, | ||
currencyCode: withSymbol ? undefined : displayDenomination.name | ||
}) | ||
|
||
return cryptoText | ||
} |
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,74 @@ | ||
import { div, log10 } from 'biggystring' | ||
import { EdgeDenomination } from 'edge-core-js' | ||
|
||
import { formatNumber } from '../locales/intl' | ||
import { | ||
DECIMAL_PRECISION, | ||
decimalOrZero, | ||
DEFAULT_TRUNCATE_PRECISION, | ||
maxPrimaryCurrencyConversionDecimals, | ||
precisionAdjust, | ||
truncateDecimals as nonLocalTruncateDecimals, | ||
zeroString | ||
} from './utils' | ||
|
||
interface GetCryptoTextParams { | ||
currencyCode?: string | ||
displayDenomination: EdgeDenomination | ||
exchangeDenomination: EdgeDenomination | ||
exchangeRate?: string | ||
fiatDenomination?: EdgeDenomination | ||
nativeAmount: string | ||
} | ||
|
||
/** | ||
* Get the numeric crypto text string value, factoring in exchange rate, denominations, etc. | ||
* TODO: Break this up once crypto & fiat text display logic is centralized into the appropriate text hooks/components. The order of operations should always be as follows: | ||
* 1. Numeric calculations | ||
* 2. Display Denomination | ||
* 3. Localization: commas, decimals, spaces | ||
*/ | ||
export const getCryptoText = ({ | ||
displayDenomination, | ||
exchangeDenomination, | ||
fiatDenomination, | ||
exchangeRate, | ||
nativeAmount, | ||
currencyCode | ||
}: GetCryptoTextParams) => { | ||
const { multiplier: displayMultiplier, symbol } = displayDenomination | ||
const { multiplier: exchangeMultiplier } = exchangeDenomination | ||
|
||
if (zeroString(nativeAmount)) return `${symbol ? symbol + ' ' : ''}0${currencyCode ? ' ' + currencyCode : ''}` | ||
|
||
let maxConversionDecimals = DEFAULT_TRUNCATE_PRECISION | ||
if (exchangeRate != null && fiatDenomination != null && parseFloat(exchangeRate) > 0) { | ||
const precisionAdjustValue = precisionAdjust({ | ||
primaryExchangeMultiplier: exchangeMultiplier, | ||
secondaryExchangeMultiplier: fiatDenomination.multiplier, | ||
exchangeSecondaryToPrimaryRatio: exchangeRate | ||
}) | ||
maxConversionDecimals = maxPrimaryCurrencyConversionDecimals(log10(displayMultiplier), precisionAdjustValue) | ||
} | ||
|
||
try { | ||
const truncatedCryptoAmount = nonLocalTruncateDecimals(div(nativeAmount, displayMultiplier, DECIMAL_PRECISION), maxConversionDecimals) | ||
const finalCryptoAmount = formatNumber(decimalOrZero(truncatedCryptoAmount, maxConversionDecimals)) // check if infinitesimal (would display as zero), cut off trailing zeroes | ||
|
||
if (currencyCode != null) { | ||
// Display with currency code if provided | ||
return `${finalCryptoAmount} ${currencyCode}` | ||
} | ||
|
||
// Display with symbol (if available) | ||
return `${symbol != null ? symbol + ' ' : ''}${finalCryptoAmount}` | ||
} catch (error: any) { | ||
if (error.message === 'Cannot operate on base16 float values') { | ||
const errorMessage = `${error.message}: Currency - ${exchangeDenomination.name}, amount - ${nativeAmount}, demonination multiplier: ${displayMultiplier}, exchange multiplier: ${exchangeMultiplier}` | ||
console.error(errorMessage) | ||
} | ||
console.error(error) | ||
} | ||
|
||
return '' | ||
} |