2021-02-04 19:15:23 +01:00
|
|
|
import { ETH, GWEI, WEI } from '../constants/common';
|
2021-04-28 21:53:59 +02: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,
|
2021-07-06 19:48:49 +02:00
|
|
|
} from '../../../shared/modules/conversion.utils';
|
2021-02-04 19:15:23 +01:00
|
|
|
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) {
|
2021-02-04 19:15:23 +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 getEthConversionFromWeiHex({
|
|
|
|
value,
|
|
|
|
fromCurrency = ETH,
|
|
|
|
conversionRate,
|
|
|
|
numberOfDecimals = 6,
|
|
|
|
}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const denominations = [fromCurrency, GWEI, WEI];
|
2018-08-31 21:14:06 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let nonZeroDenomination;
|
2018-08-31 21:14:06 +02:00
|
|
|
|
|
|
|
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],
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-08-31 21:14:06 +02:00
|
|
|
|
|
|
|
if (convertedValue !== '0' || i === denominations.length - 1) {
|
2021-02-04 19:15:23 +01:00
|
|
|
nonZeroDenomination = `${convertedValue} ${denominations[i]}`;
|
|
|
|
break;
|
2018-08-31 21:14:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-07-31 07:03:20 +02:00
|
|
|
}
|
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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-17 01:03:29 +02:00
|
|
|
}
|
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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-13 10:47:05 +02:00
|
|
|
}
|
|
|
|
|
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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-05-20 18:38:08 +02:00
|
|
|
}
|
|
|
|
|
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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-13 10:47:05 +02:00
|
|
|
}
|
|
|
|
|
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',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-09-13 10:47:05 +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',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-08-06 18:22:12 +02:00
|
|
|
}
|
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',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
2022-08-09 19:56:52 +02:00
|
|
|
export function decWEIToDecETH(hexWEI) {
|
|
|
|
return conversionUtil(hexWEI, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'ETH',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 19:30:34 +02:00
|
|
|
export function subtractHexes(aHexWEI, bHexWEI) {
|
2021-08-06 21:31:30 +02:00
|
|
|
return subtractCurrencies(aHexWEI, bHexWEI, {
|
2021-07-28 19:30:34 +02:00
|
|
|
aBase: 16,
|
|
|
|
bBase: 16,
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:39:45 +01:00
|
|
|
export function sumHexWEIs(hexWEIs) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return hexWEIs.filter(Boolean).reduce(addHexes);
|
2020-11-10 18:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sumHexWEIsToUnformattedFiat(
|
2020-11-03 00:41:28 +01:00
|
|
|
hexWEIs,
|
|
|
|
convertedCurrency,
|
|
|
|
conversionRate,
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const hexWEIsSum = sumHexWEIs(hexWEIs);
|
2020-11-10 18:39:45 +01:00
|
|
|
const convertedTotal = decEthToConvertedCurrency(
|
2020-10-06 20:28:38 +02:00
|
|
|
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,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return convertedTotal;
|
2020-11-10 18:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sumHexWEIsToRenderableFiat(
|
|
|
|
hexWEIs,
|
|
|
|
convertedCurrency,
|
|
|
|
conversionRate,
|
|
|
|
) {
|
|
|
|
const convertedTotal = sumHexWEIsToUnformattedFiat(
|
|
|
|
hexWEIs,
|
|
|
|
convertedCurrency,
|
|
|
|
conversionRate,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return formatCurrency(convertedTotal, convertedCurrency);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|