From a00539e8c2d5348d6a110a9d8984cdde400bbff5 Mon Sep 17 00:00:00 2001 From: Mati Dastugue Date: Mon, 28 Dec 2020 14:09:30 -0300 Subject: [PATCH] Fix token price function to be more generic --- .../safe/components/Balances/dataFetcher.ts | 37 +++++-------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/src/routes/safe/components/Balances/dataFetcher.ts b/src/routes/safe/components/Balances/dataFetcher.ts index 05b38ff8..c7a3f2d4 100644 --- a/src/routes/safe/components/Balances/dataFetcher.ts +++ b/src/routes/safe/components/Balances/dataFetcher.ts @@ -13,7 +13,7 @@ export const BALANCE_TABLE_VALUE_ID = 'value' const { nativeCoin } = getNetworkInfo() -const getTokenPriceValue = (token: Token, currencyValues?: BalanceCurrencyList, currencyRate?: number): string => { +const getTokenValue = (token: Token, currencyValues?: BalanceCurrencyList, currencyRate?: number): string => { const currencyValue = currencyValues?.find(({ tokenAddress }) => { if (token.address === nativeCoin.address && !tokenAddress) { return true @@ -27,36 +27,16 @@ const getTokenPriceValue = (token: Token, currencyValues?: BalanceCurrencyList, } const { balanceInBaseCurrency } = currencyValue - const balance = new BigNumber(balanceInBaseCurrency).times(currencyRate).toFixed(2) + const balance = new BigNumber(balanceInBaseCurrency).times(currencyRate).toString() - return formatAmountInUsFormat(balance) + return balance } -const getTokenPriceInCurrency = ( - token: Token, - currencySelected?: string, - currencyValues?: BalanceCurrencyList, - currencyRate?: number, -): string => { +const getTokenPriceInCurrency = (balance: string, currencySelected?: string): string => { if (!currencySelected) { - return '' + return Number('').toFixed(2) } - const currencyValue = currencyValues?.find(({ tokenAddress }) => { - if (token.address === nativeCoin.address && !tokenAddress) { - return true - } - - return token.address === tokenAddress - }) - - if (!currencyValue || !currencyRate) { - return '' - } - - const { balanceInBaseCurrency } = currencyValue - const balance = new BigNumber(balanceInBaseCurrency).times(currencyRate).toFixed(2) - - return `${formatAmountInUsFormat(balance)} ${currencySelected}` + return `${formatAmountInUsFormat(Number(balance).toFixed(2))} ${currencySelected}` } export interface BalanceData { @@ -77,6 +57,7 @@ export const getBalanceData = ( ): List => { const { nativeCoin } = getNetworkInfo() return activeTokens.map((token) => { + const balance = getTokenValue(token, currencyValues, currencyRate) return { [BALANCE_TABLE_ASSET_ID]: { name: token.name, @@ -88,8 +69,8 @@ export const getBalanceData = ( [BALANCE_TABLE_BALANCE_ID]: `${formatAmountInUsFormat(token.balance?.toString() || '0')} ${token.symbol}`, balanceOrder: Number(token.balance), [FIXED]: token.symbol === nativeCoin.symbol, - [BALANCE_TABLE_VALUE_ID]: getTokenPriceInCurrency(token, currencySelected, currencyValues, currencyRate), - valueOrder: Number(getTokenPriceValue(token, currencyValues, currencyRate)), + [BALANCE_TABLE_VALUE_ID]: getTokenPriceInCurrency(balance, currencySelected), + valueOrder: Number(balance), } }) }