1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/ui/helpers/utils/conversions.util.js

208 lines
4.3 KiB
JavaScript
Raw Normal View History

import { ETH, GWEI, WEI } from '../constants/common';
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 '../../../shared/modules/conversion.utils';
import { formatCurrency } from './confirm-tx.util';
2020-11-03 00:41:28 +01:00
export function bnToHex(inputBn) {
return addHexPrefix(inputBn.toString(16));
}
2020-11-03 00:41:28 +01:00
export function hexToDecimal(hexValue) {
return conversionUtil(hexValue, {
fromNumericBase: 'hex',
toNumericBase: 'dec',
});
}
2020-11-03 00:41:28 +01:00
export function decimalToHex(decimal) {
return conversionUtil(decimal, {
fromNumericBase: 'dec',
toNumericBase: 'hex',
});
}
2020-11-03 00:41:28 +01:00
export function getEthConversionFromWeiHex({
value,
fromCurrency = ETH,
conversionRate,
numberOfDecimals = 6,
}) {
const denominations = [fromCurrency, GWEI, WEI];
let nonZeroDenomination;
for (let i = 0; i < denominations.length; i++) {
const convertedValue = getValueFromWeiHex({
value,
conversionRate,
fromCurrency,
toCurrency: fromCurrency,
numberOfDecimals,
toDenomination: denominations[i],
});
if (convertedValue !== '0' || i === denominations.length - 1) {
nonZeroDenomination = `${convertedValue} ${denominations[i]}`;
break;
}
}
return nonZeroDenomination;
}
2020-11-03 00:41:28 +01:00
export function getValueFromWeiHex({
value,
fromCurrency = ETH,
toCurrency,
conversionRate,
numberOfDecimals,
toDenomination,
}) {
return conversionUtil(value, {
fromNumericBase: 'hex',
toNumericBase: 'dec',
fromCurrency,
toCurrency,
numberOfDecimals,
fromDenomination: WEI,
toDenomination,
conversionRate,
});
}
2020-11-03 00:41:28 +01:00
export function getWeiHexFromDecimalValue({
value,
fromCurrency,
conversionRate,
fromDenomination,
invertConversionRate,
}) {
return conversionUtil(value, {
fromNumericBase: 'dec',
toNumericBase: 'hex',
toCurrency: ETH,
fromCurrency,
conversionRate,
invertConversionRate,
fromDenomination,
toDenomination: WEI,
});
}
2020-11-03 00:41:28 +01:00
export function addHexWEIsToDec(aHexWEI, bHexWEI) {
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) {
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,
) {
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) {
return conversionUtil(decGWEI, {
fromNumericBase: 'dec',
toNumericBase: 'hex',
fromDenomination: 'GWEI',
toDenomination: 'WEI',
});
}
2020-11-03 00:41:28 +01:00
export function hexWEIToDecGWEI(decGWEI) {
return conversionUtil(decGWEI, {
fromNumericBase: 'hex',
toNumericBase: 'dec',
fromDenomination: 'WEI',
toDenomination: 'GWEI',
});
}
2020-11-03 00:41:28 +01:00
export function decETHToDecWEI(decEth) {
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-10-06 20:28:38 +02:00
}
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-10-06 20:28:38 +02:00
}
export function sumHexWEIs(hexWEIs) {
return hexWEIs.filter(Boolean).reduce(addHexes);
}
export function sumHexWEIsToUnformattedFiat(
2020-11-03 00:41:28 +01:00
hexWEIs,
convertedCurrency,
conversionRate,
) {
const hexWEIsSum = sumHexWEIs(hexWEIs);
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,
);
return convertedTotal;
}
export function sumHexWEIsToRenderableFiat(
hexWEIs,
convertedCurrency,
conversionRate,
) {
const convertedTotal = sumHexWEIsToUnformattedFiat(
hexWEIs,
convertedCurrency,
conversionRate,
);
return formatCurrency(convertedTotal, convertedCurrency);
2020-10-06 20:28:38 +02:00
}