mirror of
https://github.com/trezor/trezor-wallet
synced 2025-02-08 14:12:41 +00:00
handle missing rates in util function
This commit is contained in:
parent
a0d9383621
commit
b722b8d86a
@ -246,10 +246,10 @@ export const onAddressChange = (address: string): ThunkAction => (
|
|||||||
/*
|
/*
|
||||||
* Called from UI on "amount" field change
|
* Called from UI on "amount" field change
|
||||||
*/
|
*/
|
||||||
export const onAmountChange = (amount: string, shouldUpdateLocalAmount = true): ThunkAction => (
|
export const onAmountChange = (
|
||||||
dispatch: Dispatch,
|
amount: string,
|
||||||
getState: GetState
|
shouldUpdateLocalAmount: boolean = true
|
||||||
): void => {
|
): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
||||||
const state = getState().sendFormEthereum;
|
const state = getState().sendFormEthereum;
|
||||||
dispatch({
|
dispatch({
|
||||||
type: SEND.CHANGE,
|
type: SEND.CHANGE,
|
||||||
@ -266,7 +266,7 @@ export const onAmountChange = (amount: string, shouldUpdateLocalAmount = true):
|
|||||||
if (shouldUpdateLocalAmount) {
|
if (shouldUpdateLocalAmount) {
|
||||||
const { localCurrency } = getState().sendFormEthereum;
|
const { localCurrency } = getState().sendFormEthereum;
|
||||||
const fiatRates = getState().fiat.find(f => f.network === state.networkName);
|
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));
|
dispatch(onLocalAmountChange(localAmount, false));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -276,7 +276,7 @@ export const onAmountChange = (amount: string, shouldUpdateLocalAmount = true):
|
|||||||
*/
|
*/
|
||||||
export const onLocalAmountChange = (
|
export const onLocalAmountChange = (
|
||||||
localAmount: string,
|
localAmount: string,
|
||||||
shouldUpdateAmount = true
|
shouldUpdateAmount: boolean = true
|
||||||
): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
||||||
const state = getState().sendFormEthereum;
|
const state = getState().sendFormEthereum;
|
||||||
const { localCurrency } = getState().sendFormEthereum;
|
const { localCurrency } = getState().sendFormEthereum;
|
||||||
@ -300,12 +300,7 @@ export const onLocalAmountChange = (
|
|||||||
if (shouldUpdateAmount) {
|
if (shouldUpdateAmount) {
|
||||||
if (!network) return;
|
if (!network) return;
|
||||||
// converts amount in local currency to crypto currency that will be sent
|
// converts amount in local currency to crypto currency that will be sent
|
||||||
const amount = fromFiatCurrency(
|
const amount = fromFiatCurrency(localAmount, localCurrency, fiatRates, network.decimals);
|
||||||
localAmount,
|
|
||||||
localCurrency,
|
|
||||||
fiatRates.rates,
|
|
||||||
network.decimals
|
|
||||||
);
|
|
||||||
dispatch(onAmountChange(amount, false));
|
dispatch(onAmountChange(amount, false));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -135,7 +135,7 @@ export const recalculateTotalAmount = ($state: State): PayloadAction<State> => (
|
|||||||
// calculate amount in local currency
|
// calculate amount in local currency
|
||||||
const { localCurrency } = getState().sendFormEthereum;
|
const { localCurrency } = getState().sendFormEthereum;
|
||||||
const fiatRates = getState().fiat.find(f => f.network === state.networkName);
|
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) {
|
if (localAmount) {
|
||||||
state.localAmount = localAmount;
|
state.localAmount = localAmount;
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,30 @@
|
|||||||
import BigNumber from 'bignumber.js';
|
import BigNumber from 'bignumber.js';
|
||||||
|
|
||||||
const toFiatCurrency = (amount, fiatCurrency, rates) => {
|
const toFiatCurrency = (amount, fiatCurrency, networkRates) => {
|
||||||
// calculate amount in local currency
|
// 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);
|
let localAmount = BigNumber(amount).times(rate);
|
||||||
localAmount = localAmount.isNaN() ? '' : localAmount.toFixed(2);
|
localAmount = localAmount.isNaN() ? '' : localAmount.toFixed(2);
|
||||||
return localAmount;
|
return localAmount;
|
||||||
};
|
};
|
||||||
|
|
||||||
const fromFiatCurrency = (localAmount, fiatCurrency, rates, decimals) => {
|
const fromFiatCurrency = (localAmount, fiatCurrency, networkRates, decimals) => {
|
||||||
const rate = rates[fiatCurrency];
|
if (!networkRates || !networkRates.rates) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
const rate = networkRates.rates[fiatCurrency];
|
||||||
|
if (!rate) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
let amount = BigNumber(localAmount).div(rate);
|
let amount = BigNumber(localAmount).div(rate);
|
||||||
amount = amount.isNaN() ? '' : amount.toFixed(decimals);
|
amount = amount.isNaN() ? '' : amount.toFixed(decimals);
|
||||||
|
@ -124,7 +124,7 @@ const AccountMenu = (props: Props) => {
|
|||||||
|
|
||||||
balance = `${availableBalance} ${network.symbol}`;
|
balance = `${availableBalance} ${network.symbol}`;
|
||||||
if (fiatRates) {
|
if (fiatRates) {
|
||||||
fiat = toFiatCurrency(availableBalance, localCurrency, fiatRates.rates);
|
fiat = toFiatCurrency(availableBalance, localCurrency, fiatRates);
|
||||||
balance = `${availableBalance} ${network.symbol} / `;
|
balance = `${availableBalance} ${network.symbol} / `;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -121,12 +121,12 @@ class AccountBalance extends PureComponent<Props, State> {
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { network, localCurrency } = this.props;
|
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 fiatRateValue = '';
|
||||||
let fiat = '';
|
let fiat = '';
|
||||||
if (fiatRate) {
|
if (fiatRates) {
|
||||||
fiatRateValue = new BigNumber(fiatRate.rates[localCurrency]).toFixed(2);
|
fiatRateValue = new BigNumber(fiatRates.rates[localCurrency]).toFixed(2);
|
||||||
fiat = toFiatCurrency(this.props.balance, localCurrency, fiatRate.rates);
|
fiat = toFiatCurrency(this.props.balance, localCurrency, fiatRates);
|
||||||
}
|
}
|
||||||
|
|
||||||
const NoRatesTooltip = (
|
const NoRatesTooltip = (
|
||||||
@ -158,7 +158,7 @@ class AccountBalance extends PureComponent<Props, State> {
|
|||||||
</Label>
|
</Label>
|
||||||
<TooltipWrapper>
|
<TooltipWrapper>
|
||||||
<FiatValue>
|
<FiatValue>
|
||||||
{fiatRate ? (
|
{fiatRates ? (
|
||||||
<FormattedNumber
|
<FormattedNumber
|
||||||
currency={localCurrency}
|
currency={localCurrency}
|
||||||
value={fiat}
|
value={fiat}
|
||||||
@ -170,7 +170,7 @@ class AccountBalance extends PureComponent<Props, State> {
|
|||||||
'N/A'
|
'N/A'
|
||||||
)}
|
)}
|
||||||
</FiatValue>
|
</FiatValue>
|
||||||
{!fiatRate && NoRatesTooltip}
|
{!fiatRates && NoRatesTooltip}
|
||||||
</TooltipWrapper>
|
</TooltipWrapper>
|
||||||
<CoinBalance>
|
<CoinBalance>
|
||||||
{this.props.balance} {network.symbol}
|
{this.props.balance} {network.symbol}
|
||||||
@ -182,7 +182,7 @@ class AccountBalance extends PureComponent<Props, State> {
|
|||||||
</Label>
|
</Label>
|
||||||
<TooltipWrapper>
|
<TooltipWrapper>
|
||||||
<FiatValueRate>
|
<FiatValueRate>
|
||||||
{fiatRate ? (
|
{fiatRates ? (
|
||||||
<FormattedNumber
|
<FormattedNumber
|
||||||
currency={localCurrency}
|
currency={localCurrency}
|
||||||
value={fiatRateValue}
|
value={fiatRateValue}
|
||||||
@ -194,7 +194,7 @@ class AccountBalance extends PureComponent<Props, State> {
|
|||||||
'N/A'
|
'N/A'
|
||||||
)}
|
)}
|
||||||
</FiatValueRate>
|
</FiatValueRate>
|
||||||
{!fiatRate && NoRatesTooltip}
|
{!fiatRates && NoRatesTooltip}
|
||||||
</TooltipWrapper>
|
</TooltipWrapper>
|
||||||
<CoinBalance>1 {network.symbol}</CoinBalance>
|
<CoinBalance>1 {network.symbol}</CoinBalance>
|
||||||
</BalanceRateWrapper>
|
</BalanceRateWrapper>
|
||||||
|
@ -119,13 +119,13 @@ class AccountBalance extends PureComponent<Props, State> {
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { network, localCurrency } = this.props;
|
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 accountBalance = '';
|
||||||
let fiatRateValue = '';
|
let fiatRateValue = '';
|
||||||
let fiat = '';
|
let fiat = '';
|
||||||
if (fiatRate) {
|
if (fiatRates) {
|
||||||
accountBalance = new BigNumber(this.props.balance);
|
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);
|
fiat = accountBalance.times(fiatRateValue).toFixed(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,9 +156,9 @@ class AccountBalance extends PureComponent<Props, State> {
|
|||||||
<Label>Balance</Label>
|
<Label>Balance</Label>
|
||||||
<TooltipWrapper>
|
<TooltipWrapper>
|
||||||
<FiatValue>
|
<FiatValue>
|
||||||
{fiatRate ? `${fiat} ${localCurrency}` : 'N/A'}
|
{fiatRates ? `${fiat} ${localCurrency}` : 'N/A'}
|
||||||
</FiatValue>
|
</FiatValue>
|
||||||
{!fiatRate && NoRatesTooltip}
|
{!fiatRates && NoRatesTooltip}
|
||||||
</TooltipWrapper>
|
</TooltipWrapper>
|
||||||
<CoinBalance>
|
<CoinBalance>
|
||||||
{this.props.balance} {network.symbol}
|
{this.props.balance} {network.symbol}
|
||||||
@ -177,9 +177,9 @@ class AccountBalance extends PureComponent<Props, State> {
|
|||||||
<Label>Rate</Label>
|
<Label>Rate</Label>
|
||||||
<TooltipWrapper>
|
<TooltipWrapper>
|
||||||
<FiatValueRate>
|
<FiatValueRate>
|
||||||
{fiatRate ? `${fiatRateValue} ${localCurrency}` : 'N/A'}
|
{fiatRates ? `${fiatRateValue} ${localCurrency}` : 'N/A'}
|
||||||
</FiatValueRate>
|
</FiatValueRate>
|
||||||
{!fiatRate && NoRatesTooltip}
|
{!fiatRates && NoRatesTooltip}
|
||||||
</TooltipWrapper>
|
</TooltipWrapper>
|
||||||
<CoinBalance>1 {network.symbol}</CoinBalance>
|
<CoinBalance>1 {network.symbol}</CoinBalance>
|
||||||
</BalanceRateWrapper>
|
</BalanceRateWrapper>
|
||||||
|
Loading…
Reference in New Issue
Block a user