From cc19d250e8f902b50f1cad1c320511b9f6e76e6b Mon Sep 17 00:00:00 2001 From: Dan J Miller Date: Sun, 28 Mar 2021 10:48:44 -0230 Subject: [PATCH] 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 --- ui/app/ducks/swaps/swaps.js | 36 ++------------------------------ ui/app/pages/swaps/swaps.util.js | 2 +- 2 files changed, 3 insertions(+), 35 deletions(-) diff --git a/ui/app/ducks/swaps/swaps.js b/ui/app/ducks/swaps/swaps.js index ae3198629..64e78c125 100644 --- a/ui/app/ducks/swaps/swaps.js +++ b/ui/app/ducks/swaps/swaps.js @@ -2,8 +2,6 @@ import { createSlice } from '@reduxjs/toolkit'; import BigNumber from 'bignumber.js'; import log from 'loglevel'; -import { getStorageItem, setStorageItem } from '../../../lib/storage-helpers'; - import { addToken, addUnapprovedTransaction, @@ -83,7 +81,6 @@ const initialState = { limit: null, loading: GAS_PRICES_LOADING_STATES.INITIAL, priceEstimates: {}, - priceEstimatesLastRetrieved: 0, fallBackPrice: null, }, }; @@ -145,8 +142,6 @@ const slice = createSlice({ swapGasPriceEstimatesFetchCompleted: (state, action) => { state.customGas.priceEstimates = action.payload.priceEstimates; state.customGas.loading = GAS_PRICES_LOADING_STATES.COMPLETED; - state.customGas.priceEstimatesLastRetrieved = - action.payload.priceEstimatesLastRetrieved; }, retrievedFallbackSwapsGasPrice: (state, action) => { state.customGas.fallBackPrice = action.payload; @@ -190,9 +185,6 @@ export const swapGasEstimateLoadingHasFailed = (state) => export const getSwapGasPriceEstimateData = (state) => state.swaps.customGas.priceEstimates; -export const getSwapsPriceEstimatesLastRetrieved = (state) => - state.swaps.customGas.priceEstimatesLastRetrieved; - export const getSwapsFallbackGasPrice = (state) => state.swaps.customGas.fallBackPrice; @@ -747,26 +739,13 @@ export const signAndSendTransactions = (history, metaMetricsEvent) => { export function fetchMetaSwapsGasPriceEstimates() { return async (dispatch, getState) => { const state = getState(); - const priceEstimatesLastRetrieved = getSwapsPriceEstimatesLastRetrieved( - state, - ); - const timeLastRetrieved = - priceEstimatesLastRetrieved || - (await getStorageItem('METASWAP_GAS_PRICE_ESTIMATES_LAST_RETRIEVED')) || - 0; + const chainId = getCurrentChainId(state); dispatch(swapGasPriceEstimatesFetchStarted()); let priceEstimates; try { - if (Date.now() - timeLastRetrieved > 30000) { - priceEstimates = await fetchSwapsGasPrices(); - } else { - const cachedPriceEstimates = await getStorageItem( - 'METASWAP_GAS_PRICE_ESTIMATES', - ); - priceEstimates = cachedPriceEstimates || (await fetchSwapsGasPrices()); - } + priceEstimates = await fetchSwapsGasPrices(chainId); } catch (e) { log.warn('Fetching swaps gas prices failed:', e); @@ -791,20 +770,9 @@ export function fetchMetaSwapsGasPriceEstimates() { } } - const timeRetrieved = Date.now(); - - await Promise.all([ - setStorageItem('METASWAP_GAS_PRICE_ESTIMATES', priceEstimates), - setStorageItem( - 'METASWAP_GAS_PRICE_ESTIMATES_LAST_RETRIEVED', - timeRetrieved, - ), - ]); - dispatch( swapGasPriceEstimatesFetchCompleted({ priceEstimates, - priceEstimatesLastRetrieved: timeRetrieved, }), ); return priceEstimates; diff --git a/ui/app/pages/swaps/swaps.util.js b/ui/app/pages/swaps/swaps.util.js index 8e2c89bac..21370786d 100644 --- a/ui/app/pages/swaps/swaps.util.js +++ b/ui/app/pages/swaps/swaps.util.js @@ -392,7 +392,7 @@ export async function fetchSwapsGasPrices(chainId) { const response = await fetchWithCache( gasPricesUrl, { method: 'GET' }, - { cacheRefreshTime: 15000 }, + { cacheRefreshTime: 30000 }, ); const responseIsValid = validateData( SWAP_GAS_PRICE_VALIDATOR,