2018-09-17 19:34:29 +02:00
|
|
|
import ethUtil from 'ethereumjs-util'
|
2018-08-31 21:14:06 +02:00
|
|
|
import { ETH, GWEI, WEI } from '../constants/common'
|
2018-09-13 10:47:05 +02:00
|
|
|
import { conversionUtil, addCurrencies } from '../conversion-util'
|
2018-07-31 07:03:20 +02:00
|
|
|
|
2018-09-17 19:34:29 +02:00
|
|
|
export function bnToHex (inputBn) {
|
|
|
|
return ethUtil.addHexPrefix(inputBn.toString(16))
|
|
|
|
}
|
|
|
|
|
2018-07-31 07:03:20 +02:00
|
|
|
export function hexToDecimal (hexValue) {
|
|
|
|
return conversionUtil(hexValue, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-17 19:34:29 +02:00
|
|
|
export function decimalToHex (decimal) {
|
|
|
|
return conversionUtil(decimal, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-26 10:26:43 +02:00
|
|
|
export function getEthConversionFromWeiHex ({ value, fromCurrency = ETH, conversionRate, numberOfDecimals = 6 }) {
|
|
|
|
const denominations = [fromCurrency, 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,
|
2018-10-26 10:26:43 +02:00
|
|
|
fromCurrency,
|
|
|
|
toCurrency: fromCurrency,
|
2018-08-31 21:14:06 +02:00
|
|
|
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,
|
2018-10-26 10:26:43 +02:00
|
|
|
fromCurrency = ETH,
|
2018-07-31 07:03:20 +02:00
|
|
|
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-10-26 10:26:43 +02:00
|
|
|
fromCurrency,
|
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,
|
|
|
|
})
|
|
|
|
}
|
2018-10-17 01:03:29 +02:00
|
|
|
|
|
|
|
export function getWeiHexFromDecimalValue ({
|
|
|
|
value,
|
|
|
|
fromCurrency,
|
|
|
|
conversionRate,
|
|
|
|
fromDenomination,
|
|
|
|
invertConversionRate,
|
|
|
|
}) {
|
|
|
|
return conversionUtil(value, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
toCurrency: ETH,
|
|
|
|
fromCurrency,
|
|
|
|
conversionRate,
|
|
|
|
invertConversionRate,
|
|
|
|
fromDenomination,
|
|
|
|
toDenomination: WEI,
|
|
|
|
})
|
|
|
|
}
|
2018-09-13 10:47:05 +02:00
|
|
|
|
|
|
|
export function addHexWEIsToDec (aHexWEI, bHexWEI) {
|
|
|
|
return addCurrencies(aHexWEI, bHexWEI, {
|
|
|
|
aBase: 16,
|
|
|
|
bBase: 16,
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decEthToConvertedCurrency (ethTotal, convertedCurrency, conversionRate) {
|
|
|
|
return conversionUtil(ethTotal, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromCurrency: 'ETH',
|
|
|
|
toCurrency: convertedCurrency,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decGWEIToHexWEI (decGWEI) {
|
|
|
|
return conversionUtil(decGWEI, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
fromDenomination: 'GWEI',
|
|
|
|
toDenomination: 'WEI',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hexWEIToDecGWEI (decGWEI) {
|
|
|
|
return conversionUtil(decGWEI, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'GWEI',
|
|
|
|
})
|
|
|
|
}
|