From 475d4b9f78637196ca40b322cb0dd397de320b09 Mon Sep 17 00:00:00 2001 From: IF <139582705+infiniteflower@users.noreply.github.com> Date: Thu, 27 Mar 2025 17:48:02 -0400 Subject: [PATCH] feat: allow immediate fetching of topAssets without changing the network --- src/SwapsController.test.ts | 25 ++++++++++--------------- src/SwapsController.ts | 19 ++++++++----------- src/constants.ts | 1 - src/types.ts | 1 - 4 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/SwapsController.test.ts b/src/SwapsController.test.ts index e1b5345b..f6fd05f2 100644 --- a/src/SwapsController.test.ts +++ b/src/SwapsController.test.ts @@ -3,6 +3,7 @@ import SwapsController from './SwapsController'; import { APIFetchQuotesMetadata, APIFetchQuotesParams, + ChainData, Quote, SwapsControllerMessenger, SwapsControllerOptions, @@ -404,7 +405,6 @@ describe('SwapsController', () => { approvalTransaction: null, aggregatorMetadataLastFetched: 0, quotesLastFetched: 0, - topAssetsLastFetched: 0, error: { key: null, description: null }, topAggId: null, tokensLastFetched: 0, @@ -470,7 +470,6 @@ describe('SwapsController', () => { approvalTransaction: null, aggregatorMetadataLastFetched: 0, quotesLastFetched: 0, - topAssetsLastFetched: 0, error: { key: null, description: null }, topAggId: null, tokensLastFetched: 0, @@ -565,7 +564,10 @@ describe('SwapsController', () => { selectedNetworkClientId: networkClientId, }); - expect(controller.state).toMatchObject(cachedData); + // topAssetsLastFetched is not part of the state + const { topAssetsLastFetched, ...rest } = cachedData; + + expect(controller.state).toMatchObject(rest); }); it('clears the main part of state and initializes the cached data for the new chain ID if none previously existed', async () => { @@ -616,7 +618,6 @@ describe('SwapsController', () => { tokens: null, topAssets: null, aggregatorMetadataLastFetched: 0, - topAssetsLastFetched: 0, tokensLastFetched: 0, chainCache: { [chainId]: { @@ -1736,7 +1737,6 @@ describe('SwapsController', () => { expect(controller.state.topAssets).toStrictEqual( invocations[1].fetchedTokens, ); - expect(controller.state.topAssetsLastFetched).toStrictEqual(Date.now()); expect(controller.state.chainCache).toStrictEqual({ '0x1': { aggregatorMetadata: null, @@ -1834,7 +1834,6 @@ describe('SwapsController', () => { expect(controller.state.topAssets).toStrictEqual( invocations[0].fetchedTokens, ); - expect(controller.state.topAssetsLastFetched).toStrictEqual(0); expect(controller.state.chainCache).toStrictEqual({ '0x1': { aggregatorMetadata: null, @@ -1945,7 +1944,6 @@ describe('SwapsController', () => { expect(controller.state.topAssets).toStrictEqual( invocations[1].fetchedTokens, ); - expect(controller.state.topAssetsLastFetched).toStrictEqual(Date.now()); expect(controller.state.chainCache).toStrictEqual({ '0x1': { aggregatorMetadata: null, @@ -2043,7 +2041,6 @@ describe('SwapsController', () => { expect(controller.state.topAssets).toStrictEqual( invocations[0].fetchedTokens, ); - expect(controller.state.topAssetsLastFetched).toStrictEqual(0); expect(controller.state.chainCache).toStrictEqual({ '0x1': { aggregatorMetadata: null, @@ -2154,7 +2151,6 @@ describe('SwapsController', () => { expect(controller.state.topAssets).toStrictEqual( invocations[1].fetchedTokens, ); - expect(controller.state.topAssetsLastFetched).toStrictEqual(Date.now()); expect(controller.state.chainCache).toStrictEqual({ '0x1': { aggregatorMetadata: null, @@ -2252,7 +2248,6 @@ describe('SwapsController', () => { expect(controller.state.topAssets).toStrictEqual( invocations[0].fetchedTokens, ); - expect(controller.state.topAssetsLastFetched).toStrictEqual(0); expect(controller.state.chainCache).toStrictEqual({ '0x1': { aggregatorMetadata: null, @@ -2323,7 +2318,6 @@ describe('SwapsController', () => { symbol: 'TOKEN9999', }, ], - topAssetsLastFetched: Date.now() - fetchTopAssetsThreshold - 1, }, }); mockNetworkControllerGetNetworkClientById({ @@ -2371,7 +2365,6 @@ describe('SwapsController', () => { expect(controller.state.topAssets).toStrictEqual( invocations[1].fetchedTokens, ); - expect(controller.state.topAssetsLastFetched).toStrictEqual(Date.now()); expect(controller.state.chainCache).toStrictEqual({ '0x1': { aggregatorMetadata: null, @@ -2429,7 +2422,6 @@ describe('SwapsController', () => { symbol: 'TOKEN9999', }, ], - topAssetsLastFetched: Date.now() - fetchTopAssetsThreshold - 1, }, }); mockNetworkControllerGetNetworkClientById({ @@ -2477,7 +2469,6 @@ describe('SwapsController', () => { expect(controller.state.topAssets).toStrictEqual( invocations[0].fetchedTokens, ); - expect(controller.state.topAssetsLastFetched).toStrictEqual(0); expect(controller.state.chainCache).toStrictEqual({ '0x1': { aggregatorMetadata: null, @@ -2510,7 +2501,11 @@ describe('SwapsController', () => { symbol: 'TEST', }, ], - topAssetsLastFetched: Date.now(), + chainCache: { + '0x89': { + topAssetsLastFetched: Date.now(), + } as ChainData, + }, }, }); mockNetworkControllerGetNetworkClientById({ diff --git a/src/SwapsController.ts b/src/SwapsController.ts index 7bd05169..d6782dad 100644 --- a/src/SwapsController.ts +++ b/src/SwapsController.ts @@ -82,7 +82,6 @@ const metadata: StateMetadata = { approvalTransaction: { persist: false, anonymous: false }, aggregatorMetadataLastFetched: { persist: false, anonymous: true }, quotesLastFetched: { persist: false, anonymous: true }, - topAssetsLastFetched: { persist: false, anonymous: true }, error: { persist: false, anonymous: false }, topAggId: { persist: false, anonymous: false }, tokensLastFetched: { persist: false, anonymous: true }, @@ -145,7 +144,6 @@ export default class SwapsController extends BaseController< _state.tokens = null; _state.topAssets = null; _state.aggregatorMetadataLastFetched = 0; - _state.topAssetsLastFetched = 0; _state.tokensLastFetched = 0; _state.chainCache = getNewChainCache(chainCache, chainId, { ...INITIAL_CHAIN_DATA, @@ -162,7 +160,6 @@ export default class SwapsController extends BaseController< _state.topAssets = cachedData.topAssets; _state.aggregatorMetadataLastFetched = cachedData.aggregatorMetadataLastFetched; - _state.topAssetsLastFetched = cachedData.topAssetsLastFetched; _state.tokensLastFetched = cachedData.tokensLastFetched; }); }; @@ -1029,12 +1026,15 @@ export default class SwapsController extends BaseController< return; } - const { topAssets, topAssetsLastFetched } = this.state; + const { topAssets } = this.state; - if ( - !topAssets || - this.#fetchTopAssetsThreshold < Date.now() - topAssetsLastFetched - ) { + const topAssetsLastFetchedForChain = + this.state.chainCache[chainId]?.topAssetsLastFetched ?? 0; + + const isPastThresholdForChain = + this.#fetchTopAssetsThreshold < Date.now() - topAssetsLastFetchedForChain; + + if (!topAssets || isPastThresholdForChain) { const releaseLock = await this.#mutex.acquire(); try { const newTopAssets = await fetchTopAssets(chainId, this.#clientId); @@ -1044,7 +1044,6 @@ export default class SwapsController extends BaseController< }; this.update((_state) => { _state.topAssets = data.topAssets; - _state.topAssetsLastFetched = data.topAssetsLastFetched; _state.chainCache = getNewChainCache( _state.chainCache, chainId, @@ -1054,7 +1053,6 @@ export default class SwapsController extends BaseController< } catch { const data = { topAssetsLastFetched: 0 }; this.update((_state) => { - _state.topAssetsLastFetched = data.topAssetsLastFetched; _state.chainCache = getNewChainCache( _state.chainCache, chainId, @@ -1155,7 +1153,6 @@ export default class SwapsController extends BaseController< }); _state.isInPolling = false; _state.tokensLastFetched = currentState.tokensLastFetched; - _state.topAssetsLastFetched = currentState.topAssetsLastFetched; _state.aggregatorMetadataLastFetched = currentState.aggregatorMetadataLastFetched; _state.tokens = currentState.tokens; diff --git a/src/constants.ts b/src/constants.ts index 2e33a28d..669f22a3 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -260,7 +260,6 @@ export const getDefaultSwapsControllerState = (): SwapsControllerState => ({ approvalTransaction: null, aggregatorMetadataLastFetched: 0, quotesLastFetched: 0, - topAssetsLastFetched: 0, error: { key: null, description: null }, topAggId: null, tokensLastFetched: 0, diff --git a/src/types.ts b/src/types.ts index 4c48ddca..5c83b1e8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -314,7 +314,6 @@ export type SwapsControllerState = { tokens: null | SwapsToken[]; tokensLastFetched: number; topAssets: null | SwapsAsset[]; - topAssetsLastFetched: number; chainCache: ChainCache; };