From b722b8d86ac33c7c809e8270e7bb44c78a4ba6c6 Mon Sep 17 00:00:00 2001 From: slowbackspace Date: Tue, 12 Mar 2019 12:55:21 +0100 Subject: [PATCH] handle missing rates in util function --- src/actions/ethereum/SendFormActions.js | 19 ++++++---------- .../ethereum/SendFormValidationActions.js | 2 +- src/utils/fiatConverter.js | 22 +++++++++++++++---- .../components/AccountMenu/index.js | 2 +- .../Summary/components/Balance/index.js | 16 +++++++------- .../ripple/components/Balance/index.js | 14 ++++++------ 6 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/actions/ethereum/SendFormActions.js b/src/actions/ethereum/SendFormActions.js index 652ea3fb..c3fe2119 100644 --- a/src/actions/ethereum/SendFormActions.js +++ b/src/actions/ethereum/SendFormActions.js @@ -246,10 +246,10 @@ export const onAddressChange = (address: string): ThunkAction => ( /* * Called from UI on "amount" field change */ -export const onAmountChange = (amount: string, shouldUpdateLocalAmount = true): ThunkAction => ( - dispatch: Dispatch, - getState: GetState -): void => { +export const onAmountChange = ( + amount: string, + shouldUpdateLocalAmount: boolean = true +): ThunkAction => (dispatch: Dispatch, getState: GetState): void => { const state = getState().sendFormEthereum; dispatch({ type: SEND.CHANGE, @@ -266,7 +266,7 @@ export const onAmountChange = (amount: string, shouldUpdateLocalAmount = true): if (shouldUpdateLocalAmount) { const { localCurrency } = getState().sendFormEthereum; const fiatRates = getState().fiat.find(f => f.network === state.networkName); - const localAmount = toFiatCurrency(amount, localCurrency, fiatRates.rates); + const localAmount = toFiatCurrency(amount, localCurrency, fiatRates); dispatch(onLocalAmountChange(localAmount, false)); } }; @@ -276,7 +276,7 @@ export const onAmountChange = (amount: string, shouldUpdateLocalAmount = true): */ export const onLocalAmountChange = ( localAmount: string, - shouldUpdateAmount = true + shouldUpdateAmount: boolean = true ): ThunkAction => (dispatch: Dispatch, getState: GetState): void => { const state = getState().sendFormEthereum; const { localCurrency } = getState().sendFormEthereum; @@ -300,12 +300,7 @@ export const onLocalAmountChange = ( if (shouldUpdateAmount) { if (!network) return; // converts amount in local currency to crypto currency that will be sent - const amount = fromFiatCurrency( - localAmount, - localCurrency, - fiatRates.rates, - network.decimals - ); + const amount = fromFiatCurrency(localAmount, localCurrency, fiatRates, network.decimals); dispatch(onAmountChange(amount, false)); } }; diff --git a/src/actions/ethereum/SendFormValidationActions.js b/src/actions/ethereum/SendFormValidationActions.js index 2be80b2e..6c22a7a1 100644 --- a/src/actions/ethereum/SendFormValidationActions.js +++ b/src/actions/ethereum/SendFormValidationActions.js @@ -135,7 +135,7 @@ export const recalculateTotalAmount = ($state: State): PayloadAction => ( // calculate amount in local currency const { localCurrency } = getState().sendFormEthereum; const fiatRates = getState().fiat.find(f => f.network === state.networkName); - const localAmount = toFiatCurrency(state.amount, localCurrency, fiatRates.rates); + const localAmount = toFiatCurrency(state.amount, localCurrency, fiatRates); if (localAmount) { state.localAmount = localAmount; } diff --git a/src/utils/fiatConverter.js b/src/utils/fiatConverter.js index 6d546ece..756a086d 100644 --- a/src/utils/fiatConverter.js +++ b/src/utils/fiatConverter.js @@ -1,16 +1,30 @@ import BigNumber from 'bignumber.js'; -const toFiatCurrency = (amount, fiatCurrency, rates) => { +const toFiatCurrency = (amount, fiatCurrency, networkRates) => { // calculate amount in local currency - const rate = rates[fiatCurrency]; + if (!networkRates || !networkRates.rates) { + return ''; + } + + const rate = networkRates.rates[fiatCurrency]; + if (!rate) { + return ''; + } let localAmount = BigNumber(amount).times(rate); localAmount = localAmount.isNaN() ? '' : localAmount.toFixed(2); return localAmount; }; -const fromFiatCurrency = (localAmount, fiatCurrency, rates, decimals) => { - const rate = rates[fiatCurrency]; +const fromFiatCurrency = (localAmount, fiatCurrency, networkRates, decimals) => { + if (!networkRates || !networkRates.rates) { + return ''; + } + + const rate = networkRates.rates[fiatCurrency]; + if (!rate) { + return ''; + } let amount = BigNumber(localAmount).div(rate); amount = amount.isNaN() ? '' : amount.toFixed(decimals); diff --git a/src/views/Wallet/components/LeftNavigation/components/AccountMenu/index.js b/src/views/Wallet/components/LeftNavigation/components/AccountMenu/index.js index 9d4dac40..4ebb7cb7 100644 --- a/src/views/Wallet/components/LeftNavigation/components/AccountMenu/index.js +++ b/src/views/Wallet/components/LeftNavigation/components/AccountMenu/index.js @@ -124,7 +124,7 @@ const AccountMenu = (props: Props) => { balance = `${availableBalance} ${network.symbol}`; if (fiatRates) { - fiat = toFiatCurrency(availableBalance, localCurrency, fiatRates.rates); + fiat = toFiatCurrency(availableBalance, localCurrency, fiatRates); balance = `${availableBalance} ${network.symbol} / `; } } diff --git a/src/views/Wallet/views/Account/Summary/components/Balance/index.js b/src/views/Wallet/views/Account/Summary/components/Balance/index.js index d830616b..86268a57 100644 --- a/src/views/Wallet/views/Account/Summary/components/Balance/index.js +++ b/src/views/Wallet/views/Account/Summary/components/Balance/index.js @@ -121,12 +121,12 @@ class AccountBalance extends PureComponent { render() { const { network, localCurrency } = this.props; - const fiatRate = this.props.fiat.find(f => f.network === network.shortcut); + const fiatRates = this.props.fiat.find(f => f.network === network.shortcut); let fiatRateValue = ''; let fiat = ''; - if (fiatRate) { - fiatRateValue = new BigNumber(fiatRate.rates[localCurrency]).toFixed(2); - fiat = toFiatCurrency(this.props.balance, localCurrency, fiatRate.rates); + if (fiatRates) { + fiatRateValue = new BigNumber(fiatRates.rates[localCurrency]).toFixed(2); + fiat = toFiatCurrency(this.props.balance, localCurrency, fiatRates); } const NoRatesTooltip = ( @@ -158,7 +158,7 @@ class AccountBalance extends PureComponent { - {fiatRate ? ( + {fiatRates ? ( { 'N/A' )} - {!fiatRate && NoRatesTooltip} + {!fiatRates && NoRatesTooltip} {this.props.balance} {network.symbol} @@ -182,7 +182,7 @@ class AccountBalance extends PureComponent { - {fiatRate ? ( + {fiatRates ? ( { 'N/A' )} - {!fiatRate && NoRatesTooltip} + {!fiatRates && NoRatesTooltip} 1 {network.symbol} diff --git a/src/views/Wallet/views/Account/Summary/ripple/components/Balance/index.js b/src/views/Wallet/views/Account/Summary/ripple/components/Balance/index.js index 1a21ac0f..3f999aa8 100644 --- a/src/views/Wallet/views/Account/Summary/ripple/components/Balance/index.js +++ b/src/views/Wallet/views/Account/Summary/ripple/components/Balance/index.js @@ -119,13 +119,13 @@ class AccountBalance extends PureComponent { render() { const { network, localCurrency } = this.props; - const fiatRate = this.props.fiat.find(f => f.network === network.shortcut); + const fiatRates = this.props.fiat.find(f => f.network === network.shortcut); let accountBalance = ''; let fiatRateValue = ''; let fiat = ''; - if (fiatRate) { + if (fiatRates) { accountBalance = new BigNumber(this.props.balance); - fiatRateValue = new BigNumber(fiatRate.rates[localCurrency]).toFixed(2); + fiatRateValue = new BigNumber(fiatRates.rates[localCurrency]).toFixed(2); fiat = accountBalance.times(fiatRateValue).toFixed(2); } @@ -156,9 +156,9 @@ class AccountBalance extends PureComponent { - {fiatRate ? `${fiat} ${localCurrency}` : 'N/A'} + {fiatRates ? `${fiat} ${localCurrency}` : 'N/A'} - {!fiatRate && NoRatesTooltip} + {!fiatRates && NoRatesTooltip} {this.props.balance} {network.symbol} @@ -177,9 +177,9 @@ class AccountBalance extends PureComponent { - {fiatRate ? `${fiatRateValue} ${localCurrency}` : 'N/A'} + {fiatRates ? `${fiatRateValue} ${localCurrency}` : 'N/A'} - {!fiatRate && NoRatesTooltip} + {!fiatRates && NoRatesTooltip} 1 {network.symbol}