2021-02-04 19:15:23 +01:00
|
|
|
import currencyFormatter from 'currency-formatter';
|
|
|
|
import currencies from 'currency-formatter/currencies';
|
2023-02-02 19:35:05 +01:00
|
|
|
import { BigNumber } from 'bignumber.js';
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
import { Numeric } from '../../../shared/modules/Numeric';
|
|
|
|
import { EtherDenomination } from '../../../shared/constants/common';
|
2023-02-02 19:35:05 +01:00
|
|
|
import { TransactionMeta } from '../../../shared/constants/transaction';
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2023-02-02 19:35:05 +01:00
|
|
|
export function getHexGasTotal({ gasLimit = '0x0', gasPrice = '0x0' }): string {
|
2023-01-24 15:44:49 +01:00
|
|
|
return new Numeric(gasLimit, 16)
|
|
|
|
.times(new Numeric(gasPrice, 16))
|
|
|
|
.toPrefixedHexString();
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2023-02-02 19:35:05 +01:00
|
|
|
export function addEth(firstValue: string, ...otherValues: string[]): string {
|
2023-01-24 15:44:49 +01:00
|
|
|
return otherValues
|
|
|
|
.reduce((numericAcc, ethAmount) => {
|
|
|
|
return numericAcc.add(new Numeric(ethAmount, 10)).round(6);
|
|
|
|
}, new Numeric(firstValue, 10))
|
|
|
|
.toString();
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2023-02-02 19:35:05 +01:00
|
|
|
export function addFiat(firstValue: string, ...otherValues: string[]): string {
|
2023-01-24 15:44:49 +01:00
|
|
|
return otherValues
|
|
|
|
.reduce((numericAcc, fiatAmount) => {
|
|
|
|
return numericAcc.add(new Numeric(fiatAmount, 10)).round(2);
|
|
|
|
}, new Numeric(firstValue, 10))
|
|
|
|
.toString();
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getTransactionFee({
|
2018-06-23 08:52:45 +02:00
|
|
|
value,
|
2023-02-02 19:35:05 +01:00
|
|
|
fromCurrency = EtherDenomination.ETH,
|
2018-06-23 08:52:45 +02:00
|
|
|
toCurrency,
|
|
|
|
conversionRate,
|
|
|
|
numberOfDecimals,
|
2023-02-02 19:35:05 +01:00
|
|
|
}: {
|
|
|
|
value: string;
|
|
|
|
fromCurrency: EtherDenomination;
|
|
|
|
toCurrency: string;
|
|
|
|
conversionRate: number;
|
|
|
|
numberOfDecimals: number;
|
|
|
|
}): string {
|
2023-01-24 15:44:49 +01:00
|
|
|
let fee = new Numeric(value, 16, EtherDenomination.WEI)
|
|
|
|
.toDenomination(EtherDenomination.ETH)
|
|
|
|
.toBase(10);
|
|
|
|
|
|
|
|
if (fromCurrency !== toCurrency && conversionRate) {
|
|
|
|
fee = fee.applyConversionRate(conversionRate);
|
|
|
|
}
|
|
|
|
return fee.round(numberOfDecimals).toString();
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2023-02-02 19:35:05 +01:00
|
|
|
export function formatCurrency(value: string, currencyCode: string): string {
|
2021-02-04 19:15:23 +01:00
|
|
|
const upperCaseCurrencyCode = currencyCode.toUpperCase();
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
return currencies.find((currency) => currency.code === upperCaseCurrencyCode)
|
2020-11-03 00:41:28 +01:00
|
|
|
? currencyFormatter.format(Number(value), {
|
|
|
|
code: upperCaseCurrencyCode,
|
|
|
|
})
|
2021-02-04 19:15:23 +01:00
|
|
|
: value;
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function convertTokenToFiat({
|
2018-07-14 22:47:07 +02:00
|
|
|
value,
|
2023-02-02 19:35:05 +01:00
|
|
|
fromCurrency = EtherDenomination.ETH,
|
2018-07-14 22:47:07 +02:00
|
|
|
toCurrency,
|
|
|
|
conversionRate,
|
|
|
|
contractExchangeRate,
|
2023-02-02 19:35:05 +01:00
|
|
|
}: {
|
|
|
|
value: string;
|
|
|
|
fromCurrency: EtherDenomination;
|
|
|
|
toCurrency: string;
|
|
|
|
conversionRate: number;
|
|
|
|
contractExchangeRate: number;
|
|
|
|
}): string {
|
2021-02-04 19:15:23 +01:00
|
|
|
const totalExchangeRate = conversionRate * contractExchangeRate;
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
let tokenInFiat = new Numeric(value, 10);
|
|
|
|
|
|
|
|
if (fromCurrency !== toCurrency && totalExchangeRate) {
|
|
|
|
tokenInFiat = tokenInFiat.applyConversionRate(totalExchangeRate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenInFiat.round(2).toString();
|
2018-07-14 22:47:07 +02:00
|
|
|
}
|
2018-07-24 05:25:04 +02:00
|
|
|
|
2020-09-09 06:28:24 +02:00
|
|
|
/**
|
|
|
|
* Rounds the given decimal string to 4 significant digits.
|
|
|
|
*
|
2023-02-02 19:35:05 +01:00
|
|
|
* @param decimalString - The base-ten number to round.
|
|
|
|
* @returns The rounded number, or the original number if no
|
2020-09-09 06:28:24 +02:00
|
|
|
* rounding was necessary.
|
|
|
|
*/
|
2023-02-02 19:35:05 +01:00
|
|
|
export function roundExponential(decimalString: string): string {
|
2021-02-04 19:15:23 +01:00
|
|
|
const PRECISION = 4;
|
|
|
|
const bigNumberValue = new BigNumber(decimalString);
|
2018-07-26 21:15:34 +02:00
|
|
|
|
|
|
|
// In JS, numbers with exponentials greater than 20 get displayed as an exponential.
|
2020-11-03 00:41:28 +01:00
|
|
|
return bigNumberValue.e > 20
|
|
|
|
? bigNumberValue.toPrecision(PRECISION)
|
2021-02-04 19:15:23 +01:00
|
|
|
: decimalString;
|
2018-07-26 21:15:34 +02:00
|
|
|
}
|
2021-07-28 00:31:53 +02:00
|
|
|
|
2023-02-02 19:35:05 +01:00
|
|
|
export function areDappSuggestedAndTxParamGasFeesTheSame(
|
|
|
|
txData: TransactionMeta,
|
|
|
|
): boolean {
|
|
|
|
const { txParams, dappSuggestedGasFees } = txData ?? {};
|
2021-07-28 00:31:53 +02:00
|
|
|
const {
|
|
|
|
gasPrice: txParamsGasPrice,
|
|
|
|
maxFeePerGas: txParamsMaxFeePerGas,
|
|
|
|
maxPriorityFeePerGas: txParamsMaxPriorityFeePerGas,
|
|
|
|
} = txParams || {};
|
|
|
|
const {
|
|
|
|
gasPrice: dappGasPrice,
|
|
|
|
maxFeePerGas: dappMaxFeePerGas,
|
|
|
|
maxPriorityFeePerGas: dappMaxPriorityFeePerGas,
|
|
|
|
} = dappSuggestedGasFees || {};
|
|
|
|
|
|
|
|
const txParamsDoesNotHaveFeeProperties =
|
|
|
|
!txParamsGasPrice && !txParamsMaxFeePerGas && !txParamsMaxPriorityFeePerGas;
|
|
|
|
const dappDidNotSuggestFeeProperties =
|
|
|
|
!dappGasPrice && !dappMaxFeePerGas && !dappMaxPriorityFeePerGas;
|
|
|
|
if (txParamsDoesNotHaveFeeProperties || dappDidNotSuggestFeeProperties) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const txParamsGasPriceMatchesDappSuggestedGasPrice =
|
|
|
|
txParamsGasPrice && txParamsGasPrice === dappGasPrice;
|
|
|
|
const txParamsEIP1559FeesMatchDappSuggestedGasPrice = [
|
|
|
|
txParamsMaxFeePerGas,
|
|
|
|
txParamsMaxPriorityFeePerGas,
|
|
|
|
].every((fee) => fee === dappGasPrice);
|
|
|
|
const txParamsEIP1559FeesMatchDappSuggestedEIP1559Fees =
|
|
|
|
txParamsMaxFeePerGas &&
|
|
|
|
txParamsMaxFeePerGas === dappMaxFeePerGas &&
|
|
|
|
txParamsMaxPriorityFeePerGas === dappMaxPriorityFeePerGas;
|
|
|
|
|
2023-02-02 19:35:05 +01:00
|
|
|
return Boolean(
|
2021-07-28 00:31:53 +02:00
|
|
|
txParamsGasPriceMatchesDappSuggestedGasPrice ||
|
2023-02-02 19:35:05 +01:00
|
|
|
txParamsEIP1559FeesMatchDappSuggestedGasPrice ||
|
|
|
|
txParamsEIP1559FeesMatchDappSuggestedEIP1559Fees,
|
2021-07-28 00:31:53 +02:00
|
|
|
);
|
|
|
|
}
|