James Prado aabcd3f7a3 Use Network Symbol in Confirmation Modal (#1039)
* Set default unit to 'ETH' instead of 'ether'

* Use 'isEtherUnit()' everywhere

* Set default unit to empty string

* Update isEthUnit to isNetworkUnit

* Fix unit conversion for non-ethereum networks

* Set default network unit properly

* Fix tests

* fix typos

* Update isNetworkUnit selector

* Update isNetworkUnit

* Fix validationhelpers tests

* Add mock state to tests & Move isNetworkUnit to selectors

* Fix validation helper spec

* fix unit swap spec
2018-03-01 19:24:14 -06:00

69 lines
1.8 KiB
TypeScript

import { AppState } from 'reducers';
import { getNetworkConfig } from 'selectors/config';
import { getUnit, getParamsFromSerializedTx } from 'selectors/transaction';
import BN from 'bn.js';
import { Wei, TokenValue } from 'libs/units';
export const getRates = (state: AppState) => state.rates;
const getUSDConversionRate = (state: AppState, unit: string) => {
const { isTestnet } = getNetworkConfig(state);
const { rates } = getRates(state);
if (isTestnet) {
return null;
}
const conversionRate = rates[unit];
if (!conversionRate) {
return null;
}
return conversionRate.USD;
};
export const getValueInUSD = (state: AppState, value: TokenValue | Wei) => {
const unit = getUnit(state);
const conversionRate = getUSDConversionRate(state, unit);
if (!conversionRate) {
return null;
}
const sendValueUSD = value.muln(conversionRate);
return sendValueUSD;
};
export const getTransactionFeeInUSD = (state: AppState, fee: Wei) => {
const { unit } = getNetworkConfig(state);
const conversionRate = getUSDConversionRate(state, unit);
if (!conversionRate) {
return null;
}
const feeValueUSD = fee.muln(conversionRate);
return feeValueUSD;
};
export interface AllUSDValues {
valueUSD: BN | null;
feeUSD: BN | null;
totalUSD: BN | null;
}
export const getAllUSDValuesFromSerializedTx = (state: AppState): AllUSDValues => {
const fields = getParamsFromSerializedTx(state);
if (!fields) {
return {
feeUSD: null,
valueUSD: null,
totalUSD: null
};
}
const { currentValue, fee } = fields;
const valueUSD = getValueInUSD(state, currentValue);
const feeUSD = getTransactionFeeInUSD(state, fee);
return {
feeUSD,
valueUSD,
totalUSD: feeUSD && valueUSD ? valueUSD.add(feeUSD) : null
};
};