From bb68d9cf6e4d48024b1d1b014fb4281410e1e90a Mon Sep 17 00:00:00 2001 From: enesozturk Date: Wed, 18 Sep 2024 13:58:19 +0300 Subject: [PATCH] refactor: update local storage get and sets --- .../pages/library/multichain-wagmi-solana.tsx | 23 +++--------- packages/adapters/solana/src/client.ts | 4 -- packages/adapters/wagmi/src/client.ts | 4 -- .../appkit/src/universal-adapter/client.ts | 37 ++++++------------- packages/common/src/utils/SafeLocalStorage.ts | 18 ++++++--- .../core/src/controllers/ChainController.ts | 4 +- 6 files changed, 33 insertions(+), 57 deletions(-) diff --git a/apps/laboratory/src/pages/library/multichain-wagmi-solana.tsx b/apps/laboratory/src/pages/library/multichain-wagmi-solana.tsx index 26add712ef..25c12cbad2 100644 --- a/apps/laboratory/src/pages/library/multichain-wagmi-solana.tsx +++ b/apps/laboratory/src/pages/library/multichain-wagmi-solana.tsx @@ -7,17 +7,13 @@ import { ConstantsUtil } from '../../utils/ConstantsUtil' import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { WagmiProvider } from 'wagmi' import { - arbitrum, mainnet, polygon, - base, - binanceSmartChain, solana, - solanaTestnet, - solanaDevnet, + arbitrum, optimism, - zkSync, - sepolia + solanaTestnet, + solanaDevnet } from '@reown/appkit/networks' import { AppKitButtons } from '../../components/AppKitButtons' import { HuobiWalletAdapter, SolflareWalletAdapter } from '@solana/wallet-adapter-wallets' @@ -25,7 +21,7 @@ import { MultiChainTestsWagmiSolana } from '../../components/MultiChainTestsWagm const queryClient = new QueryClient() -const networks = [mainnet, optimism, polygon, zkSync, arbitrum, sepolia] +const networks = [mainnet, polygon, solana, arbitrum, optimism, solanaTestnet, solanaDevnet] const wagmiAdapter = new WagmiAdapter({ ssr: true, @@ -39,16 +35,7 @@ const solanaWeb3JsAdapter = new SolanaAdapter({ const modal = createAppKit({ adapters: [wagmiAdapter, solanaWeb3JsAdapter], - networks: [ - mainnet, - polygon, - base, - binanceSmartChain, - arbitrum, - solana, - solanaTestnet, - solanaDevnet - ], + networks, defaultNetwork: mainnet, projectId: ConstantsUtil.ProjectId, features: { diff --git a/packages/adapters/solana/src/client.ts b/packages/adapters/solana/src/client.ts index 3a88327966..90d80ff2b9 100644 --- a/packages/adapters/solana/src/client.ts +++ b/packages/adapters/solana/src/client.ts @@ -140,10 +140,6 @@ export class SolanaAdapter implements ChainAdapter { this.networkControllerClient = { switchCaipNetwork: async caipNetwork => { if (caipNetwork) { - SafeLocalStorage.setItem( - SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK, - JSON.stringify(caipNetwork) - ) try { await this.switchNetwork(caipNetwork) } catch (error) { diff --git a/packages/adapters/wagmi/src/client.ts b/packages/adapters/wagmi/src/client.ts index 683eef9349..766bcb2733 100644 --- a/packages/adapters/wagmi/src/client.ts +++ b/packages/adapters/wagmi/src/client.ts @@ -230,10 +230,6 @@ export class WagmiAdapter implements ChainAdapter { this.networkControllerClient = { switchCaipNetwork: async caipNetwork => { - SafeLocalStorage.setItem( - SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK, - JSON.stringify(caipNetwork) - ) const chainId = Number(NetworkUtil.caipNetworkIdToNumber(caipNetwork?.id)) if (chainId && this.wagmiConfig) { diff --git a/packages/appkit/src/universal-adapter/client.ts b/packages/appkit/src/universal-adapter/client.ts index 563cfe7b7e..f615deb697 100644 --- a/packages/appkit/src/universal-adapter/client.ts +++ b/packages/appkit/src/universal-adapter/client.ts @@ -95,10 +95,6 @@ export class UniversalAdapterClient { // @ts-expect-error switchCaipNetwork is async for some adapter but not for this adapter switchCaipNetwork: caipNetwork => { if (caipNetwork) { - SafeLocalStorage.setItem( - SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK, - JSON.stringify(caipNetwork) - ) try { this.switchNetwork(caipNetwork) } catch (error) { @@ -397,32 +393,23 @@ export class UniversalAdapterClient { }) const storedCaipNetwork = SafeLocalStorage.getItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK) + const activeCaipNetwork = ChainController.state.activeCaipNetwork - if (storedCaipNetwork) { - try { - const parsedCaipNetwork = JSON.parse(storedCaipNetwork) as CaipNetwork - if (parsedCaipNetwork) { - NetworkController.setActiveCaipNetwork(parsedCaipNetwork) - } - } catch (error) { - console.warn('>>> Error setting active caip network', error) + try { + if (storedCaipNetwork) { + NetworkController.setActiveCaipNetwork(storedCaipNetwork) + } else if (!activeCaipNetwork) { + this.setDefaultNetwork(nameSpaces) + } else if ( + !NetworkController.state.approvedCaipNetworkIds?.includes(activeCaipNetwork.id) + ) { + this.setDefaultNetwork(nameSpaces) } - } else if (!ChainController.state.activeCaipNetwork) { - this.setDefaultNetwork(nameSpaces) - } else if ( - !NetworkController.state.approvedCaipNetworkIds?.includes( - ChainController.state.activeCaipNetwork.id - ) - ) { - this.setDefaultNetwork(nameSpaces) + } catch (error) { + console.warn('>>> Error setting active caip network', error) } } - SafeLocalStorage.setItem( - SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK, - JSON.stringify(this.appKit?.getCaipNetwork()) - ) - this.syncAccount() this.watchWalletConnect() } diff --git a/packages/common/src/utils/SafeLocalStorage.ts b/packages/common/src/utils/SafeLocalStorage.ts index deef953139..5fc7aabb24 100644 --- a/packages/common/src/utils/SafeLocalStorage.ts +++ b/packages/common/src/utils/SafeLocalStorage.ts @@ -1,9 +1,11 @@ +import type { CaipNetwork } from './TypeUtil' + export type SafeLocalStorageItems = { '@appkit/wallet_id': string '@appkit/wallet_name': string '@appkit/solana_wallet': string '@appkit/solana_caip_chain': string - '@appkit/active_caip_network': string + '@appkit/active_caip_network': CaipNetwork '@appkit/active_caip_network_id': string '@appkit/connected_connector': string '@appkit/connected_social': string @@ -32,10 +34,16 @@ export const SafeLocalStorage = { value: SafeLocalStorageItems[Key] ): void { if (isSafe()) { - localStorage.setItem(key, value) + if (typeof value === 'string') { + localStorage.setItem(key, value) + } else { + localStorage.setItem(key, JSON.stringify(value)) + } } }, - getItem(key: Key): SafeLocalStorageItems[Key] | null { + getItem( + key: Key + ): SafeLocalStorageItems[Key] | undefined { if (isSafe()) { const value = localStorage.getItem(key) @@ -43,12 +51,12 @@ export const SafeLocalStorage = { try { return JSON.parse(value) } catch { - return value + return undefined } } } - return null + return undefined }, removeItem(key: Key): void { if (isSafe()) { diff --git a/packages/core/src/controllers/ChainController.ts b/packages/core/src/controllers/ChainController.ts index a344f7e241..76194d9b3b 100644 --- a/packages/core/src/controllers/ChainController.ts +++ b/packages/core/src/controllers/ChainController.ts @@ -262,6 +262,8 @@ export const ChainController = { if (caipNetwork.chainNamespace !== state.activeChain) { this.setActiveChain(caipNetwork.chainNamespace, caipNetwork) + SafeLocalStorage.setItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK, caipNetwork) + SafeLocalStorage.setItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK_ID, caipNetwork.id) return } @@ -273,7 +275,7 @@ export const ChainController = { selectedNetworkId: caipNetwork?.id }) - SafeLocalStorage.setItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK, JSON.stringify(caipNetwork)) + SafeLocalStorage.setItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK, caipNetwork) SafeLocalStorage.setItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK_ID, caipNetwork.id) },