2018-06-23 08:52:45 +02:00
|
|
|
import currencyFormatter from 'currency-formatter'
|
|
|
|
import currencies from 'currency-formatter/currencies'
|
|
|
|
import ethUtil from 'ethereumjs-util'
|
2018-07-26 21:15:34 +02:00
|
|
|
import BigNumber from 'bignumber.js'
|
2018-06-23 08:52:45 +02:00
|
|
|
|
|
|
|
import {
|
|
|
|
conversionUtil,
|
|
|
|
addCurrencies,
|
|
|
|
multiplyCurrencies,
|
|
|
|
conversionGreaterThan,
|
|
|
|
} from '../../conversion-util'
|
|
|
|
|
2018-07-24 05:25:04 +02:00
|
|
|
import { unconfirmedTransactionsCountSelector } from '../../selectors/confirm-transaction'
|
|
|
|
|
2018-06-23 08:52:45 +02:00
|
|
|
export function increaseLastGasPrice (lastGasPrice) {
|
|
|
|
return ethUtil.addHexPrefix(multiplyCurrencies(lastGasPrice, 1.1, {
|
|
|
|
multiplicandBase: 16,
|
|
|
|
multiplierBase: 10,
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hexGreaterThan (a, b) {
|
|
|
|
return conversionGreaterThan(
|
|
|
|
{ value: a, fromNumericBase: 'hex' },
|
|
|
|
{ value: b, fromNumericBase: 'hex' },
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getHexGasTotal ({ gasLimit, gasPrice }) {
|
|
|
|
return ethUtil.addHexPrefix(multiplyCurrencies(gasLimit, gasPrice, {
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
multiplicandBase: 16,
|
|
|
|
multiplierBase: 16,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addEth (...args) {
|
|
|
|
return args.reduce((acc, base) => {
|
|
|
|
return addCurrencies(acc, base, {
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addFiat (...args) {
|
|
|
|
return args.reduce((acc, base) => {
|
|
|
|
return addCurrencies(acc, base, {
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-07-31 07:03:20 +02:00
|
|
|
export function getValueFromWeiHex ({
|
2018-06-23 08:52:45 +02:00
|
|
|
value,
|
2018-10-26 10:26:43 +02:00
|
|
|
fromCurrency = 'ETH',
|
2018-06-23 08:52:45 +02:00
|
|
|
toCurrency,
|
|
|
|
conversionRate,
|
|
|
|
numberOfDecimals,
|
2018-08-31 21:14:06 +02:00
|
|
|
toDenomination,
|
2018-06-23 08:52:45 +02:00
|
|
|
}) {
|
|
|
|
return conversionUtil(value, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
2018-10-26 10:26:43 +02:00
|
|
|
fromCurrency,
|
2018-06-23 08:52:45 +02:00
|
|
|
toCurrency,
|
|
|
|
numberOfDecimals,
|
|
|
|
fromDenomination: 'WEI',
|
2018-08-31 21:14:06 +02:00
|
|
|
toDenomination,
|
2018-06-23 08:52:45 +02:00
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getTransactionFee ({
|
|
|
|
value,
|
2018-10-26 10:26:43 +02:00
|
|
|
fromCurrency = 'ETH',
|
2018-06-23 08:52:45 +02:00
|
|
|
toCurrency,
|
|
|
|
conversionRate,
|
|
|
|
numberOfDecimals,
|
|
|
|
}) {
|
|
|
|
return conversionUtil(value, {
|
|
|
|
fromNumericBase: 'BN',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
2018-10-26 10:26:43 +02:00
|
|
|
fromCurrency,
|
2018-06-23 08:52:45 +02:00
|
|
|
toCurrency,
|
|
|
|
numberOfDecimals,
|
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatCurrency (value, currencyCode) {
|
|
|
|
const upperCaseCurrencyCode = currencyCode.toUpperCase()
|
|
|
|
|
|
|
|
return currencies.find(currency => currency.code === upperCaseCurrencyCode)
|
2018-08-16 14:28:27 +02:00
|
|
|
? currencyFormatter.format(Number(value), { code: upperCaseCurrencyCode, style: 'currency' })
|
2018-06-23 08:52:45 +02:00
|
|
|
: value
|
|
|
|
}
|
2018-07-14 22:47:07 +02:00
|
|
|
|
|
|
|
export function convertTokenToFiat ({
|
|
|
|
value,
|
2018-10-26 10:26:43 +02:00
|
|
|
fromCurrency = 'ETH',
|
2018-07-14 22:47:07 +02:00
|
|
|
toCurrency,
|
|
|
|
conversionRate,
|
|
|
|
contractExchangeRate,
|
|
|
|
}) {
|
|
|
|
const totalExchangeRate = conversionRate * contractExchangeRate
|
|
|
|
|
|
|
|
return conversionUtil(value, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
2018-10-26 10:26:43 +02:00
|
|
|
fromCurrency,
|
2018-07-14 22:47:07 +02:00
|
|
|
toCurrency,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
conversionRate: totalExchangeRate,
|
|
|
|
})
|
|
|
|
}
|
2018-07-24 05:25:04 +02:00
|
|
|
|
|
|
|
export function hasUnconfirmedTransactions (state) {
|
|
|
|
return unconfirmedTransactionsCountSelector(state) > 0
|
|
|
|
}
|
2018-07-26 21:15:34 +02:00
|
|
|
|
|
|
|
export function roundExponential (value) {
|
|
|
|
const PRECISION = 4
|
2018-08-14 05:12:22 +02:00
|
|
|
const bigNumberValue = new BigNumber(String(value))
|
2018-07-26 21:15:34 +02:00
|
|
|
|
|
|
|
// In JS, numbers with exponentials greater than 20 get displayed as an exponential.
|
|
|
|
return bigNumberValue.e > 20 ? Number(bigNumberValue.toPrecision(PRECISION)) : value
|
|
|
|
}
|