mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
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
This commit is contained in:
parent
594665c45e
commit
cc19d250e8
@ -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;
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user