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
feature/default_network_editable
Dan J Miller 4 years ago committed by GitHub
parent 594665c45e
commit cc19d250e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      ui/app/ducks/swaps/swaps.js
  2. 2
      ui/app/pages/swaps/swaps.util.js

@ -2,8 +2,6 @@ import { createSlice } from '@reduxjs/toolkit';
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import log from 'loglevel'; import log from 'loglevel';
import { getStorageItem, setStorageItem } from '../../../lib/storage-helpers';
import { import {
addToken, addToken,
addUnapprovedTransaction, addUnapprovedTransaction,
@ -83,7 +81,6 @@ const initialState = {
limit: null, limit: null,
loading: GAS_PRICES_LOADING_STATES.INITIAL, loading: GAS_PRICES_LOADING_STATES.INITIAL,
priceEstimates: {}, priceEstimates: {},
priceEstimatesLastRetrieved: 0,
fallBackPrice: null, fallBackPrice: null,
}, },
}; };
@ -145,8 +142,6 @@ const slice = createSlice({
swapGasPriceEstimatesFetchCompleted: (state, action) => { swapGasPriceEstimatesFetchCompleted: (state, action) => {
state.customGas.priceEstimates = action.payload.priceEstimates; state.customGas.priceEstimates = action.payload.priceEstimates;
state.customGas.loading = GAS_PRICES_LOADING_STATES.COMPLETED; state.customGas.loading = GAS_PRICES_LOADING_STATES.COMPLETED;
state.customGas.priceEstimatesLastRetrieved =
action.payload.priceEstimatesLastRetrieved;
}, },
retrievedFallbackSwapsGasPrice: (state, action) => { retrievedFallbackSwapsGasPrice: (state, action) => {
state.customGas.fallBackPrice = action.payload; state.customGas.fallBackPrice = action.payload;
@ -190,9 +185,6 @@ export const swapGasEstimateLoadingHasFailed = (state) =>
export const getSwapGasPriceEstimateData = (state) => export const getSwapGasPriceEstimateData = (state) =>
state.swaps.customGas.priceEstimates; state.swaps.customGas.priceEstimates;
export const getSwapsPriceEstimatesLastRetrieved = (state) =>
state.swaps.customGas.priceEstimatesLastRetrieved;
export const getSwapsFallbackGasPrice = (state) => export const getSwapsFallbackGasPrice = (state) =>
state.swaps.customGas.fallBackPrice; state.swaps.customGas.fallBackPrice;
@ -747,26 +739,13 @@ export const signAndSendTransactions = (history, metaMetricsEvent) => {
export function fetchMetaSwapsGasPriceEstimates() { export function fetchMetaSwapsGasPriceEstimates() {
return async (dispatch, getState) => { return async (dispatch, getState) => {
const state = getState(); const state = getState();
const priceEstimatesLastRetrieved = getSwapsPriceEstimatesLastRetrieved( const chainId = getCurrentChainId(state);
state,
);
const timeLastRetrieved =
priceEstimatesLastRetrieved ||
(await getStorageItem('METASWAP_GAS_PRICE_ESTIMATES_LAST_RETRIEVED')) ||
0;
dispatch(swapGasPriceEstimatesFetchStarted()); dispatch(swapGasPriceEstimatesFetchStarted());
let priceEstimates; let priceEstimates;
try { try {
if (Date.now() - timeLastRetrieved > 30000) { priceEstimates = await fetchSwapsGasPrices(chainId);
priceEstimates = await fetchSwapsGasPrices();
} else {
const cachedPriceEstimates = await getStorageItem(
'METASWAP_GAS_PRICE_ESTIMATES',
);
priceEstimates = cachedPriceEstimates || (await fetchSwapsGasPrices());
}
} catch (e) { } catch (e) {
log.warn('Fetching swaps gas prices failed:', 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( dispatch(
swapGasPriceEstimatesFetchCompleted({ swapGasPriceEstimatesFetchCompleted({
priceEstimates, priceEstimates,
priceEstimatesLastRetrieved: timeRetrieved,
}), }),
); );
return priceEstimates; return priceEstimates;

@ -392,7 +392,7 @@ export async function fetchSwapsGasPrices(chainId) {
const response = await fetchWithCache( const response = await fetchWithCache(
gasPricesUrl, gasPricesUrl,
{ method: 'GET' }, { method: 'GET' },
{ cacheRefreshTime: 15000 }, { cacheRefreshTime: 30000 },
); );
const responseIsValid = validateData( const responseIsValid = validateData(
SWAP_GAS_PRICE_VALIDATOR, SWAP_GAS_PRICE_VALIDATOR,

Loading…
Cancel
Save