1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 22:24:27 +01:00
metamask-extension/ui/app/selectors/custom-gas.js
MetaMask Bot 14b5c389ed
Version v9.3.0 RC (#10739)
* Replace logic for eth swap token in fetchQuotesAndSetQuoteState with getSwapsEthToken call (#10624)

* Move swaps constants to the shared constants directory (#10614)

* Fix: ETH 'token' now only appears once in the swaps to and from dropdowns. (#10650)

* Swaps support for local testnet (#10658)

* Swaps support for local testnet

* Create util method for comparison of token addresses/symbols to default swaps token

* Get chainId from txMeta in _trackSwapsMetrics of transaction controller

* Add comment to document purpose of getTransactionGroupRecipientAddressFilter

* Use isSwapsDefaultTokenSymbol in place of repeated defaultTokenSymbol comparisons in build-quote.js

* Additional swaps network support (#10721)

* Add swaps support for bnc chain

* Use single default token address in shared/constants/swaps

* Ensure swaps gas prices are fetched from the correct chain specific endpoint (#10744)

* Ensure swaps gas prices are fetched from the correct chain specific endpoint

* Just rely on fetchWithCache to cache swaps gas prices, instead of directly using storage in getSwapsPriceEstimatesLastRetrieved

* Empty commit

* update @metamask/etherscan-link to v2.0.0 (#10747)

* Use correct block explorer name and link in swaps when on custom network (#10743)

* Use correct block explorer name and link in swaps when on custom network.

* Fix up custom etherscan link code in build-quote.js

* Use blockExplorerUrl hostname instead of 'blockExplorerBaseUrl'

* Use correct etherscan-link method for token links in build-quote

* Create correct token link in build-quote for mainnet AND custom networks

* Block explorer url improvements in awaiting-swap.js and build-quote.js

* Use swapVerifyTokenExplanation message with substitutable block explorer for all applicable locales

* Ensure that block explorer links are not shown in awaiting-swap if no url is available

* Ensure that the correct default currency symbols are used for fees on the view quote screen (#10753)

* Updating y18n and netmask to resolve dependency issues (#10765)

netmask@1.0.6 -> 2.0.1, y18n@3.2.1 -> 3.2.2, y18n@4.0.0 -> 4.0.1

* Ensure that priceSlippage fiat amounts are always shown in view-quote.js (#10762)

* Ensure that the approval fee in the swaps custom gas modal is in network specific currency (#10763)

* Use network specific swaps contract address when checking swap contract token approval (#10774)

* Set the BSC_CONTRACT_ADDRESS to lowercase (#10800)

* Ensure correct primary currency image is displayed on home screen and token list (#10777)

* [skip e2e] Update changelog for v9.3.0 (#10740)

* Version v9.3.0

* [skip e2e] Update changelog for v9.3.0 (#10803)

Co-authored-by: Dan J Miller <danjm.com@gmail.com>
Co-authored-by: ryanml <ryanlanese@gmail.com>
Co-authored-by: David Walsh <davidwalsh83@gmail.com>
Co-authored-by: MetaMask Bot <metamaskbot@users.noreply.github.com>
2021-04-02 17:00:57 -02:30

358 lines
8.5 KiB
JavaScript

import { addHexPrefix } from '../../../app/scripts/lib/util';
import {
conversionUtil,
conversionGreaterThan,
} from '../helpers/utils/conversion-util';
import { formatCurrency } from '../helpers/utils/confirm-tx.util';
import { decEthToConvertedCurrency as ethTotalToConvertedCurrency } from '../helpers/utils/conversions.util';
import { formatETHFee } from '../helpers/utils/formatters';
import { calcGasTotal } from '../pages/send/send.utils';
import { GAS_ESTIMATE_TYPES } from '../helpers/constants/common';
import {
getCurrentCurrency,
getIsMainnet,
getPreferences,
getGasPrice,
} from '.';
const NUMBER_OF_DECIMALS_SM_BTNS = 5;
export function getCustomGasLimit(state) {
return state.gas.customData.limit;
}
export function getCustomGasPrice(state) {
return state.gas.customData.price;
}
export function getBasicGasEstimateLoadingStatus(state) {
return state.gas.basicEstimateIsLoading;
}
export function getAveragePriceEstimateInHexWEI(state) {
const averagePriceEstimate = state.gas.basicEstimates.average;
return getGasPriceInHexWei(averagePriceEstimate || '0x0');
}
export function getFastPriceEstimateInHexWEI(state) {
const fastPriceEstimate = getFastPriceEstimate(state);
return getGasPriceInHexWei(fastPriceEstimate || '0x0');
}
export function getDefaultActiveButtonIndex(
gasButtonInfo,
customGasPriceInHex,
gasPrice,
) {
return gasButtonInfo
.map(({ priceInHexWei }) => priceInHexWei)
.lastIndexOf(addHexPrefix(customGasPriceInHex || gasPrice));
}
export function getSafeLowEstimate(state) {
const {
gas: {
basicEstimates: { safeLow },
},
} = state;
return safeLow;
}
export function getFastPriceEstimate(state) {
const {
gas: {
basicEstimates: { fast },
},
} = state;
return fast;
}
export function isCustomPriceSafe(state) {
const safeLow = getSafeLowEstimate(state);
const customGasPrice = getCustomGasPrice(state);
if (!customGasPrice) {
return true;
}
if (!safeLow) {
return false;
}
const customPriceSafe = conversionGreaterThan(
{
value: customGasPrice,
fromNumericBase: 'hex',
fromDenomination: 'WEI',
toDenomination: 'GWEI',
},
{ value: safeLow, fromNumericBase: 'dec' },
);
return customPriceSafe;
}
export function isCustomPriceExcessive(state, checkSend = false) {
const customPrice = checkSend ? getGasPrice(state) : getCustomGasPrice(state);
const fastPrice = getFastPriceEstimate(state);
if (!customPrice || !fastPrice) {
return false;
}
// Custom gas should be considered excessive when it is 1.5 times greater than the fastest estimate.
const customPriceExcessive = conversionGreaterThan(
{
value: customPrice,
fromNumericBase: 'hex',
fromDenomination: 'WEI',
toDenomination: 'GWEI',
},
{
fromNumericBase: 'dec',
value: Math.floor(fastPrice * 1.5),
},
);
return customPriceExcessive;
}
export function basicPriceEstimateToETHTotal(
estimate,
gasLimit,
numberOfDecimals = 9,
) {
return conversionUtil(calcGasTotal(gasLimit, estimate), {
fromNumericBase: 'hex',
toNumericBase: 'dec',
fromDenomination: 'GWEI',
numberOfDecimals,
});
}
export function getRenderableEthFee(
estimate,
gasLimit,
numberOfDecimals = 9,
nativeCurrency = 'ETH',
) {
const value = conversionUtil(estimate, {
fromNumericBase: 'dec',
toNumericBase: 'hex',
});
const fee = basicPriceEstimateToETHTotal(value, gasLimit, numberOfDecimals);
return formatETHFee(fee, nativeCurrency);
}
export function getRenderableConvertedCurrencyFee(
estimate,
gasLimit,
convertedCurrency,
conversionRate,
) {
const value = conversionUtil(estimate, {
fromNumericBase: 'dec',
toNumericBase: 'hex',
});
const fee = basicPriceEstimateToETHTotal(value, gasLimit);
const feeInCurrency = ethTotalToConvertedCurrency(
fee,
convertedCurrency,
conversionRate,
);
return formatCurrency(feeInCurrency, convertedCurrency);
}
export function priceEstimateToWei(priceEstimate) {
return conversionUtil(priceEstimate, {
fromNumericBase: 'hex',
toNumericBase: 'hex',
fromDenomination: 'GWEI',
toDenomination: 'WEI',
numberOfDecimals: 9,
});
}
export function getGasPriceInHexWei(price) {
const value = conversionUtil(price, {
fromNumericBase: 'dec',
toNumericBase: 'hex',
});
return addHexPrefix(priceEstimateToWei(value));
}
export function getRenderableGasButtonData(
estimates,
gasLimit,
showFiat,
conversionRate,
currentCurrency,
nativeCurrency,
) {
const { safeLow, average, fast } = estimates;
const slowEstimateData = {
gasEstimateType: GAS_ESTIMATE_TYPES.SLOW,
feeInPrimaryCurrency: getRenderableEthFee(
safeLow,
gasLimit,
9,
nativeCurrency,
),
feeInSecondaryCurrency: showFiat
? getRenderableConvertedCurrencyFee(
safeLow,
gasLimit,
currentCurrency,
conversionRate,
)
: '',
priceInHexWei: getGasPriceInHexWei(safeLow),
};
const averageEstimateData = {
gasEstimateType: GAS_ESTIMATE_TYPES.AVERAGE,
feeInPrimaryCurrency: getRenderableEthFee(
average,
gasLimit,
9,
nativeCurrency,
),
feeInSecondaryCurrency: showFiat
? getRenderableConvertedCurrencyFee(
average,
gasLimit,
currentCurrency,
conversionRate,
)
: '',
priceInHexWei: getGasPriceInHexWei(average),
};
const fastEstimateData = {
gasEstimateType: GAS_ESTIMATE_TYPES.FAST,
feeInPrimaryCurrency: getRenderableEthFee(
fast,
gasLimit,
9,
nativeCurrency,
),
feeInSecondaryCurrency: showFiat
? getRenderableConvertedCurrencyFee(
fast,
gasLimit,
currentCurrency,
conversionRate,
)
: '',
priceInHexWei: getGasPriceInHexWei(fast),
};
return {
slowEstimateData,
averageEstimateData,
fastEstimateData,
};
}
export function getRenderableBasicEstimateData(state, gasLimit) {
if (getBasicGasEstimateLoadingStatus(state)) {
return [];
}
const { showFiatInTestnets } = getPreferences(state);
const isMainnet = getIsMainnet(state);
const showFiat = isMainnet || Boolean(showFiatInTestnets);
const { conversionRate } = state.metamask;
const currentCurrency = getCurrentCurrency(state);
const {
slowEstimateData,
averageEstimateData,
fastEstimateData,
} = getRenderableGasButtonData(
state.gas.basicEstimates,
gasLimit,
showFiat,
conversionRate,
currentCurrency,
);
return [slowEstimateData, averageEstimateData, fastEstimateData];
}
export function getRenderableEstimateDataForSmallButtonsFromGWEI(state) {
if (getBasicGasEstimateLoadingStatus(state)) {
return [];
}
const { showFiatInTestnets } = getPreferences(state);
const isMainnet = getIsMainnet(state);
const showFiat = isMainnet || Boolean(showFiatInTestnets);
const gasLimit =
state.metamask.send.gasLimit || getCustomGasLimit(state) || '0x5208';
const { conversionRate } = state.metamask;
const currentCurrency = getCurrentCurrency(state);
const {
gas: {
basicEstimates: { safeLow, average, fast },
},
} = state;
return [
{
gasEstimateType: GAS_ESTIMATE_TYPES.SLOW,
feeInSecondaryCurrency: showFiat
? getRenderableConvertedCurrencyFee(
safeLow,
gasLimit,
currentCurrency,
conversionRate,
)
: '',
feeInPrimaryCurrency: getRenderableEthFee(
safeLow,
gasLimit,
NUMBER_OF_DECIMALS_SM_BTNS,
),
priceInHexWei: getGasPriceInHexWei(safeLow, true),
},
{
gasEstimateType: GAS_ESTIMATE_TYPES.AVERAGE,
feeInSecondaryCurrency: showFiat
? getRenderableConvertedCurrencyFee(
average,
gasLimit,
currentCurrency,
conversionRate,
)
: '',
feeInPrimaryCurrency: getRenderableEthFee(
average,
gasLimit,
NUMBER_OF_DECIMALS_SM_BTNS,
),
priceInHexWei: getGasPriceInHexWei(average, true),
},
{
gasEstimateType: GAS_ESTIMATE_TYPES.FAST,
feeInSecondaryCurrency: showFiat
? getRenderableConvertedCurrencyFee(
fast,
gasLimit,
currentCurrency,
conversionRate,
)
: '',
feeInPrimaryCurrency: getRenderableEthFee(
fast,
gasLimit,
NUMBER_OF_DECIMALS_SM_BTNS,
),
priceInHexWei: getGasPriceInHexWei(fast, true),
},
];
}