From 3191d9e01454fa01415256efeeb9d0d48b52cbd7 Mon Sep 17 00:00:00 2001 From: Dan J Miller Date: Sat, 17 Oct 2020 06:43:51 -0230 Subject: [PATCH] Help users avoid insufficient gas prices in swaps (#9624) * Don't show average (slowest) button in custom gas modal while in swaps * In swaps, show warning in custom gas modal if price set below average price * Update ui/app/selectors/custom-gas.js Co-authored-by: Erik Marks <25517051+rekmarks@users.noreply.github.com> * Fix typo Co-authored-by: Erik Marks <25517051+rekmarks@users.noreply.github.com> --- .../gas-modal-page-container.container.js | 2 +- ui/app/selectors/custom-gas.js | 31 ++++++++++++++----- ui/app/selectors/tests/custom-gas.test.js | 7 ----- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/ui/app/components/app/gas-customization/gas-modal-page-container/gas-modal-page-container.container.js b/ui/app/components/app/gas-customization/gas-modal-page-container/gas-modal-page-container.container.js index 54465f674..52aaf5162 100644 --- a/ui/app/components/app/gas-customization/gas-modal-page-container/gas-modal-page-container.container.js +++ b/ui/app/components/app/gas-customization/gas-modal-page-container/gas-modal-page-container.container.js @@ -158,7 +158,7 @@ const mapStateToProps = (state, ownProps) => { newTotalFiat, currentTimeEstimate, blockTime: getBasicGasEstimateBlockTime(state), - customPriceIsSafe: isCustomPriceSafe(state), + customPriceIsSafe: isCustomPriceSafe(state, isSwap), maxModeOn, gasPriceButtonGroupProps: { buttonDataLoading, diff --git a/ui/app/selectors/custom-gas.js b/ui/app/selectors/custom-gas.js index 4c4968d7e..27be49af4 100644 --- a/ui/app/selectors/custom-gas.js +++ b/ui/app/selectors/custom-gas.js @@ -88,16 +88,31 @@ export function getSafeLowEstimate (state) { return safeLow } -export function isCustomPriceSafe (state) { +export function getAverageEstimate (state) { + const { + gas: { + basicEstimates: { + average, + }, + }, + } = state + + return average +} + +export function isCustomPriceSafe (state, averageIsSafe) { const safeLow = getSafeLowEstimate(state) + const average = getAverageEstimate(state) + const safeMinimumPrice = averageIsSafe ? average : safeLow + const customGasPrice = getCustomGasPrice(state) if (!customGasPrice) { return true } - if (safeLow === null) { - return null + if (safeMinimumPrice === null) { + return false } const customPriceSafe = conversionGreaterThan( @@ -107,7 +122,7 @@ export function isCustomPriceSafe (state) { fromDenomination: 'WEI', toDenomination: 'GWEI', }, - { value: safeLow, fromNumericBase: 'dec' }, + { value: safeMinimumPrice, fromNumericBase: 'dec' }, ) return customPriceSafe @@ -216,7 +231,7 @@ export function getRenderableBasicEstimateData (state, gasLimit, useFastestButto }, } = state - const slowEstimatData = { + const slowEstimateData = { gasEstimateType: GAS_ESTIMATE_TYPES.SLOW, feeInPrimaryCurrency: getRenderableEthFee(safeLow, gasLimit), feeInSecondaryCurrency: showFiat @@ -234,7 +249,7 @@ export function getRenderableBasicEstimateData (state, gasLimit, useFastestButto timeEstimate: avgWait && getRenderableTimeEstimate(avgWait), priceInHexWei: getGasPriceInHexWei(average), } - const fastEstimatData = { + const fastEstimateData = { gasEstimateType: GAS_ESTIMATE_TYPES.FAST, feeInPrimaryCurrency: getRenderableEthFee(fast, gasLimit), feeInSecondaryCurrency: showFiat @@ -254,8 +269,8 @@ export function getRenderableBasicEstimateData (state, gasLimit, useFastestButto } return useFastestButtons - ? [averageEstimateData, fastEstimatData, fastestEstimateData] - : [slowEstimatData, averageEstimateData, fastEstimatData] + ? [fastEstimateData, fastestEstimateData] + : [slowEstimateData, averageEstimateData, fastEstimateData] } export function getRenderableEstimateDataForSmallButtonsFromGWEI (state) { diff --git a/ui/app/selectors/tests/custom-gas.test.js b/ui/app/selectors/tests/custom-gas.test.js index f9d2e67f0..6bbc71cd9 100644 --- a/ui/app/selectors/tests/custom-gas.test.js +++ b/ui/app/selectors/tests/custom-gas.test.js @@ -346,13 +346,6 @@ describe('custom-gas selectors', function () { }, { expectedResult: [ - { - gasEstimateType: 'AVERAGE', - feeInPrimaryCurrency: '0.000147 ETH', - feeInSecondaryCurrency: '$0.38', - priceInHexWei: '0x1a13b8600', - timeEstimate: '~10 min 6 sec', - }, { gasEstimateType: 'FAST', feeInSecondaryCurrency: '$0.54',