2018-02-07 04:39:24 +00:00
|
|
|
import { AppState } from 'reducers';
|
|
|
|
import { getNetworkConfig } from 'selectors/config';
|
2018-03-02 01:24:14 +00:00
|
|
|
import { getUnit, getParamsFromSerializedTx } from 'selectors/transaction';
|
2018-02-07 04:39:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-02 01:24:14 +00:00
|
|
|
const conversionRate = rates[unit];
|
2018-02-07 04:39:24 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
};
|