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'
|
2020-11-06 22:18:00 +01:00
|
|
|
import { addHexPrefix } from '../../../../app/scripts/lib/util'
|
2020-10-06 20:28:38 +02:00
|
|
|
import {
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionUtil,
|
|
|
|
addCurrencies,
|
|
|
|
subtractCurrencies,
|
|
|
|
} from './conversion-util'
|
|
|
|
import { formatCurrency } from './confirm-tx.util'
|
2018-07-31 07:03:20 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function bnToHex(inputBn) {
|
2020-11-06 22:18:00 +01:00
|
|
|
return addHexPrefix(inputBn.toString(16))
|
2018-09-17 19:34:29 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function hexToDecimal(hexValue) {
|
2018-07-31 07:03:20 +02:00
|
|
|
return conversionUtil(hexValue, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function decimalToHex(decimal) {
|
2018-09-17 19:34:29 +02:00
|
|
|
return conversionUtil(decimal, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getEthConversionFromWeiHex({
|
|
|
|
value,
|
|
|
|
fromCurrency = ETH,
|
|
|
|
conversionRate,
|
|
|
|
numberOfDecimals = 6,
|
|
|
|
}) {
|
2018-10-26 10:26:43 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getValueFromWeiHex({
|
2018-07-31 07:03:20 +02:00
|
|
|
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
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getWeiHexFromDecimalValue({
|
2018-10-17 01:03:29 +02:00
|
|
|
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
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function addHexWEIsToDec(aHexWEI, bHexWEI) {
|
2018-09-13 10:47:05 +02:00
|
|
|
return addCurrencies(aHexWEI, bHexWEI, {
|
|
|
|
aBase: 16,
|
|
|
|
bBase: 16,
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function subtractHexWEIsToDec(aHexWEI, bHexWEI) {
|
2019-05-20 18:38:08 +02:00
|
|
|
return subtractCurrencies(aHexWEI, bHexWEI, {
|
|
|
|
aBase: 16,
|
|
|
|
bBase: 16,
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function decEthToConvertedCurrency(
|
|
|
|
ethTotal,
|
|
|
|
convertedCurrency,
|
|
|
|
conversionRate,
|
|
|
|
) {
|
2018-09-13 10:47:05 +02:00
|
|
|
return conversionUtil(ethTotal, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromCurrency: 'ETH',
|
|
|
|
toCurrency: convertedCurrency,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
conversionRate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function decGWEIToHexWEI(decGWEI) {
|
2018-09-13 10:47:05 +02:00
|
|
|
return conversionUtil(decGWEI, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
fromDenomination: 'GWEI',
|
|
|
|
toDenomination: 'WEI',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function hexWEIToDecGWEI(decGWEI) {
|
2018-09-13 10:47:05 +02:00
|
|
|
return conversionUtil(decGWEI, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'GWEI',
|
|
|
|
})
|
|
|
|
}
|
2020-08-06 18:22:12 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function decETHToDecWEI(decEth) {
|
2020-08-06 18:22:12 +02:00
|
|
|
return conversionUtil(decEth, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'ETH',
|
|
|
|
toDenomination: 'WEI',
|
|
|
|
})
|
|
|
|
}
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function hexWEIToDecETH(hexWEI) {
|
2020-10-06 20:28:38 +02:00
|
|
|
return conversionUtil(hexWEI, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'ETH',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function hexMax(...hexNumbers) {
|
2020-10-06 20:28:38 +02:00
|
|
|
let max = hexNumbers[0]
|
|
|
|
hexNumbers.slice(1).forEach((hexNumber) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
if (new BigNumber(hexNumber, 16).gt(max, 16)) {
|
2020-10-06 20:28:38 +02:00
|
|
|
max = hexNumber
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function addHexes(aHexWEI, bHexWEI) {
|
2020-10-06 20:28:38 +02:00
|
|
|
return addCurrencies(aHexWEI, bHexWEI, {
|
|
|
|
aBase: 16,
|
|
|
|
bBase: 16,
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function sumHexWEIsToRenderableFiat(
|
|
|
|
hexWEIs,
|
|
|
|
convertedCurrency,
|
|
|
|
conversionRate,
|
|
|
|
) {
|
2020-10-06 20:28:38 +02:00
|
|
|
const hexWEIsSum = hexWEIs.filter((n) => n).reduce(addHexes)
|
|
|
|
const ethTotal = decEthToConvertedCurrency(
|
|
|
|
getValueFromWeiHex({
|
2020-11-03 00:41:28 +01:00
|
|
|
value: hexWEIsSum,
|
|
|
|
toCurrency: 'ETH',
|
|
|
|
numberOfDecimals: 4,
|
2020-10-06 20:28:38 +02:00
|
|
|
}),
|
|
|
|
convertedCurrency,
|
|
|
|
conversionRate,
|
|
|
|
)
|
|
|
|
return formatCurrency(ethTotal, convertedCurrency)
|
|
|
|
}
|