2018-09-17 19:34:29 +02:00
|
|
|
import ethUtil from 'ethereumjs-util'
|
2020-10-06 20:28:38 +02:00
|
|
|
import BigNumber from 'bignumber.js'
|
2018-08-31 21:14:06 +02:00
|
|
|
import { ETH, GWEI, WEI } from '../constants/common'
|
2019-05-20 18:38:08 +02:00
|
|
|
import { conversionUtil, addCurrencies, subtractCurrencies } from './conversion-util'
|
2020-10-06 20:28:38 +02:00
|
|
|
import {
|
|
|
|
formatCurrency,
|
|
|
|
} from './confirm-tx.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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-20 18:38:08 +02:00
|
|
|
export function subtractHexWEIsToDec (aHexWEI, bHexWEI) {
|
|
|
|
return subtractCurrencies(aHexWEI, bHexWEI, {
|
|
|
|
aBase: 16,
|
|
|
|
bBase: 16,
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-13 10:47:05 +02:00
|
|
|
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',
|
|
|
|
})
|
|
|
|
}
|
2020-08-06 18:22:12 +02:00
|
|
|
|
|
|
|
export function decETHToDecWEI (decEth) {
|
|
|
|
return conversionUtil(decEth, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'ETH',
|
|
|
|
toDenomination: 'WEI',
|
|
|
|
})
|
|
|
|
}
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
export function hexWEIToDecETH (hexWEI) {
|
|
|
|
return conversionUtil(hexWEI, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'ETH',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hexMax (...hexNumbers) {
|
|
|
|
let max = hexNumbers[0]
|
|
|
|
hexNumbers.slice(1).forEach((hexNumber) => {
|
|
|
|
if ((new BigNumber(hexNumber, 16)).gt(max, 16)) {
|
|
|
|
max = hexNumber
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addHexes (aHexWEI, bHexWEI) {
|
|
|
|
return addCurrencies(aHexWEI, bHexWEI, {
|
|
|
|
aBase: 16,
|
|
|
|
bBase: 16,
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function sumHexWEIsToRenderableFiat (hexWEIs, convertedCurrency, conversionRate) {
|
|
|
|
const hexWEIsSum = hexWEIs.filter((n) => n).reduce(addHexes)
|
|
|
|
const ethTotal = decEthToConvertedCurrency(
|
|
|
|
getValueFromWeiHex({
|
|
|
|
value: hexWEIsSum, toCurrency: 'ETH', numberOfDecimals: 4,
|
|
|
|
}),
|
|
|
|
convertedCurrency,
|
|
|
|
conversionRate,
|
|
|
|
)
|
|
|
|
return formatCurrency(ethTotal, convertedCurrency)
|
|
|
|
}
|