handle missing rates in util function

pull/448/head
slowbackspace 5 years ago
parent a0d9383621
commit b722b8d86a

@ -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));
}
};

@ -135,7 +135,7 @@ export const recalculateTotalAmount = ($state: State): PayloadAction<State> => (
// 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;
}

@ -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);

@ -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} / `;
}
}

@ -121,12 +121,12 @@ class AccountBalance extends PureComponent<Props, State> {
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<Props, State> {
</Label>
<TooltipWrapper>
<FiatValue>
{fiatRate ? (
{fiatRates ? (
<FormattedNumber
currency={localCurrency}
value={fiat}
@ -170,7 +170,7 @@ class AccountBalance extends PureComponent<Props, State> {
'N/A'
)}
</FiatValue>
{!fiatRate && NoRatesTooltip}
{!fiatRates && NoRatesTooltip}
</TooltipWrapper>
<CoinBalance>
{this.props.balance} {network.symbol}
@ -182,7 +182,7 @@ class AccountBalance extends PureComponent<Props, State> {
</Label>
<TooltipWrapper>
<FiatValueRate>
{fiatRate ? (
{fiatRates ? (
<FormattedNumber
currency={localCurrency}
value={fiatRateValue}
@ -194,7 +194,7 @@ class AccountBalance extends PureComponent<Props, State> {
'N/A'
)}
</FiatValueRate>
{!fiatRate && NoRatesTooltip}
{!fiatRates && NoRatesTooltip}
</TooltipWrapper>
<CoinBalance>1 {network.symbol}</CoinBalance>
</BalanceRateWrapper>

@ -119,13 +119,13 @@ class AccountBalance extends PureComponent<Props, State> {
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<Props, State> {
<Label>Balance</Label>
<TooltipWrapper>
<FiatValue>
{fiatRate ? `${fiat} ${localCurrency}` : 'N/A'}
{fiatRates ? `${fiat} ${localCurrency}` : 'N/A'}
</FiatValue>
{!fiatRate && NoRatesTooltip}
{!fiatRates && NoRatesTooltip}
</TooltipWrapper>
<CoinBalance>
{this.props.balance} {network.symbol}
@ -177,9 +177,9 @@ class AccountBalance extends PureComponent<Props, State> {
<Label>Rate</Label>
<TooltipWrapper>
<FiatValueRate>
{fiatRate ? `${fiatRateValue} ${localCurrency}` : 'N/A'}
{fiatRates ? `${fiatRateValue} ${localCurrency}` : 'N/A'}
</FiatValueRate>
{!fiatRate && NoRatesTooltip}
{!fiatRates && NoRatesTooltip}
</TooltipWrapper>
<CoinBalance>1 {network.symbol}</CoinBalance>
</BalanceRateWrapper>

Loading…
Cancel
Save