2018-07-31 07:03:20 +02:00
|
|
|
import { conversionUtil } from '../conversion-util'
|
2018-08-31 21:14:06 +02:00
|
|
|
import { ETH, GWEI, WEI } from '../constants/common'
|
2018-07-31 07:03:20 +02:00
|
|
|
|
|
|
|
export function hexToDecimal (hexValue) {
|
|
|
|
return conversionUtil(hexValue, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:14:06 +02:00
|
|
|
export function getEthConversionFromWeiHex ({ value, conversionRate, numberOfDecimals = 6 }) {
|
2018-08-31 22:23:16 +02:00
|
|
|
const denominations = [ETH, GWEI, WEI]
|
2018-08-31 21:14:06 +02:00
|
|
|
|
|
|
|
let nonZeroDenomination
|
|
|
|
|
|
|
|
for (let i = 0; i < denominations.length; i++) {
|
|
|
|
const convertedValue = getValueFromWeiHex({
|
|
|
|
value,
|
|
|
|
conversionRate,
|
|
|
|
toCurrency: ETH,
|
|
|
|
numberOfDecimals,
|
|
|
|
toDenomination: denominations[i],
|
|
|
|
})
|
|
|
|
|
|
|
|
if (convertedValue !== '0' || i === denominations.length - 1) {
|
|
|
|
nonZeroDenomination = `${convertedValue} ${denominations[i]}`
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nonZeroDenomination
|
2018-07-31 07:03:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getValueFromWeiHex ({
|
|
|
|
value,
|
|
|
|
toCurrency,
|
|
|
|
conversionRate,
|
|
|
|
numberOfDecimals,
|
2018-08-31 21:14:06 +02:00
|
|
|
toDenomination,
|
2018-07-31 07:03:20 +02:00
|
|
|
}) {
|
|
|
|
return conversionUtil(value, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
2018-08-31 21:14:06 +02:00
|
|
|
fromCurrency: ETH,
|
2018-07-31 07:03:20 +02:00
|
|
|
toCurrency,
|
|
|
|
numberOfDecimals,
|
2018-08-31 21:14:06 +02:00
|
|
|
fromDenomination: WEI,
|
|
|
|
toDenomination,
|
2018-07-31 07:03:20 +02:00
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
}
|