Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions src/SwapsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SwapsController from './SwapsController';
import {
APIFetchQuotesMetadata,
APIFetchQuotesParams,
ChainData,
Quote,
SwapsControllerMessenger,
SwapsControllerOptions,
Expand Down Expand Up @@ -404,7 +405,6 @@ describe('SwapsController', () => {
approvalTransaction: null,
aggregatorMetadataLastFetched: 0,
quotesLastFetched: 0,
topAssetsLastFetched: 0,
error: { key: null, description: null },
topAggId: null,
tokensLastFetched: 0,
Expand Down Expand Up @@ -470,7 +470,6 @@ describe('SwapsController', () => {
approvalTransaction: null,
aggregatorMetadataLastFetched: 0,
quotesLastFetched: 0,
topAssetsLastFetched: 0,
error: { key: null, description: null },
topAggId: null,
tokensLastFetched: 0,
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -616,7 +618,6 @@ describe('SwapsController', () => {
tokens: null,
topAssets: null,
aggregatorMetadataLastFetched: 0,
topAssetsLastFetched: 0,
tokensLastFetched: 0,
chainCache: {
[chainId]: {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -2323,7 +2318,6 @@ describe('SwapsController', () => {
symbol: 'TOKEN9999',
},
],
topAssetsLastFetched: Date.now() - fetchTopAssetsThreshold - 1,
},
});
mockNetworkControllerGetNetworkClientById({
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -2429,7 +2422,6 @@ describe('SwapsController', () => {
symbol: 'TOKEN9999',
},
],
topAssetsLastFetched: Date.now() - fetchTopAssetsThreshold - 1,
},
});
mockNetworkControllerGetNetworkClientById({
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -2510,7 +2501,11 @@ describe('SwapsController', () => {
symbol: 'TEST',
},
],
topAssetsLastFetched: Date.now(),
chainCache: {
'0x89': {
topAssetsLastFetched: Date.now(),
} as ChainData,
},
},
});
mockNetworkControllerGetNetworkClientById({
Expand Down
19 changes: 8 additions & 11 deletions src/SwapsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ const metadata: StateMetadata<SwapsControllerState> = {
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 },
Expand Down Expand Up @@ -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,
Expand All @@ -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;
});
};
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ export type SwapsControllerState = {
tokens: null | SwapsToken[];
tokensLastFetched: number;
topAssets: null | SwapsAsset[];
topAssetsLastFetched: number;
chainCache: ChainCache;
};

Expand Down