diff --git a/src/state/assets/userAssets.ts b/src/state/assets/userAssets.ts index 480a7b65045..5c62c49a88b 100644 --- a/src/state/assets/userAssets.ts +++ b/src/state/assets/userAssets.ts @@ -49,21 +49,38 @@ export interface UserAssetsState { setUserAssets: (userAssets: Map | ParsedSearchAsset[]) => void; } +type UserAssetsStateToPersist = Omit< + Partial, + | 'currentAbortController' + | 'inputSearchQuery' + | 'searchCache' + | 'getBalanceSortedChainList' + | 'getChainsWithBalance' + | 'getFilteredUserAssetIds' + | 'getHighestValueEth' + | 'getUserAsset' + | 'getUserAssets' + | 'selectUserAssetIds' + | 'selectUserAssets' + | 'setSearchCache' + | 'setSearchQuery' + | 'setUserAssets' +>; + // NOTE: We are serializing Map as an Array<[UniqueId, ParsedSearchAsset]> -type UserAssetsStateWithTransforms = Omit, 'chainBalances' | 'idsByChain' | 'userAssets'> & { +type UserAssetsStateToPersistWithTransforms = Omit & { chainBalances: Array<[ChainId, number]>; idsByChain: Array<[UserAssetFilter, UniqueId[]]>; userAssets: Array<[UniqueId, ParsedSearchAsset]>; }; -function serializeUserAssetsState(state: Partial, version?: number) { +function serializeUserAssetsState(state: UserAssetsStateToPersist, version?: number) { try { - const transformedStateToPersist: UserAssetsStateWithTransforms = { + const transformedStateToPersist: UserAssetsStateToPersistWithTransforms = { ...state, chainBalances: state.chainBalances ? Array.from(state.chainBalances.entries()) : [], idsByChain: state.idsByChain ? Array.from(state.idsByChain.entries()) : [], userAssets: state.userAssets ? Array.from(state.userAssets.entries()) : [], - searchCache: undefined, }; return JSON.stringify({ @@ -77,7 +94,7 @@ function serializeUserAssetsState(state: Partial, version?: num } function deserializeUserAssetsState(serializedState: string) { - let parsedState: { state: UserAssetsStateWithTransforms; version: number }; + let parsedState: { state: UserAssetsStateToPersistWithTransforms; version: number }; try { parsedState = JSON.parse(serializedState); } catch (error) { @@ -114,14 +131,11 @@ function deserializeUserAssetsState(serializedState: string) { logger.error(new RainbowError(`[userAssetsStore]: Failed to convert userAssets from user assets storage`), { error }); } - const searchCache = new Map(); - return { state: { ...state, chainBalances, idsByChain, - searchCache, userAssets: userAssetsData, }, version, @@ -336,7 +350,13 @@ export const createUserAssetsStore = (address: Address | string) => }), { storageKey: `userAssets_${address}`, - version: 0, + partialize: state => ({ + chainBalances: state.chainBalances, + filter: state.filter, + idsByChain: state.idsByChain, + userAssets: state.userAssets, + }), + version: 1, serializer: serializeUserAssetsState, deserializer: deserializeUserAssetsState, }