@ -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 ;