diff --git a/app/scripts/controllers/swaps.js b/app/scripts/controllers/swaps.js index 150b4fece..5f3ec9851 100644 --- a/app/scripts/controllers/swaps.js +++ b/app/scripts/controllers/swaps.js @@ -79,6 +79,7 @@ const initialState = { routeState: '', swapsFeatureIsLive: true, useNewSwapsApi: false, + isFetchingQuotes: false, swapsQuoteRefreshTime: FALLBACK_QUOTE_REFRESH_TIME, swapsQuotePrefetchingRefreshTime: FALLBACK_QUOTE_REFRESH_TIME, }, @@ -229,6 +230,8 @@ export default class SwapsController { const indexOfCurrentCall = this.indexOfNewestCallInFlight + 1; this.indexOfNewestCallInFlight = indexOfCurrentCall; + this.setIsFetchingQuotes(true); + let [newQuotes] = await Promise.all([ this._fetchTradesInfo(fetchParams, { ...fetchParamsMetaData, @@ -237,6 +240,20 @@ export default class SwapsController { this._setSwapsRefreshRates(), ]); + const { + swapsState: { isFetchingQuotes }, + } = this.store.getState(); + + // If isFetchingQuotes is false, it means a user left Swaps (we cleaned the state) + // and we don't want to set any API response with quotes into state. + if (!isFetchingQuotes) { + return [ + {}, // quotes + null, // selectedAggId + ]; + } + this.setIsFetchingQuotes(false); + newQuotes = mapValues(newQuotes, (quote) => ({ ...quote, sourceTokenInfo: fetchParamsMetaData.sourceTokenInfo, @@ -540,6 +557,13 @@ export default class SwapsController { this.store.updateState({ swapsState: { ...swapsState, routeState } }); } + setIsFetchingQuotes(status) { + const { swapsState } = this.store.getState(); + this.store.updateState({ + swapsState: { ...swapsState, isFetchingQuotes: status }, + }); + } + setSwapsLiveness(swapsLiveness) { const { swapsState } = this.store.getState(); const { swapsFeatureIsLive, useNewSwapsApi } = swapsLiveness; @@ -570,7 +594,6 @@ export default class SwapsController { this.store.updateState({ swapsState: { ...initialState.swapsState, - tokens: swapsState.tokens, swapsQuoteRefreshTime: swapsState.swapsQuoteRefreshTime, swapsQuotePrefetchingRefreshTime: swapsState.swapsQuotePrefetchingRefreshTime, diff --git a/app/scripts/controllers/swaps.test.js b/app/scripts/controllers/swaps.test.js index d6127c6db..978c705a8 100644 --- a/app/scripts/controllers/swaps.test.js +++ b/app/scripts/controllers/swaps.test.js @@ -135,6 +135,7 @@ const EMPTY_INIT_STATE = { swapsQuoteRefreshTime: 60000, swapsQuotePrefetchingRefreshTime: 60000, swapsUserFeeLevel: '', + isFetchingQuotes: false, }, };