Skip to content

Commit

Permalink
refactor: update local storage get and sets
Browse files Browse the repository at this point in the history
  • Loading branch information
enesozturk committed Sep 18, 2024
1 parent a288e87 commit bb68d9c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 57 deletions.
23 changes: 5 additions & 18 deletions apps/laboratory/src/pages/library/multichain-wagmi-solana.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,21 @@ 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'
import { MultiChainTestsWagmiSolana } from '../../components/MultiChainTestsWagmiSolana'

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,
Expand All @@ -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: {
Expand Down
4 changes: 0 additions & 4 deletions packages/adapters/solana/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions packages/adapters/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
37 changes: 12 additions & 25 deletions packages/appkit/src/universal-adapter/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
}
Expand Down
18 changes: 13 additions & 5 deletions packages/common/src/utils/SafeLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -32,23 +34,29 @@ 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 extends keyof SafeLocalStorageItems>(key: Key): SafeLocalStorageItems[Key] | null {
getItem<Key extends keyof SafeLocalStorageItems>(
key: Key
): SafeLocalStorageItems[Key] | undefined {
if (isSafe()) {
const value = localStorage.getItem(key)

if (value) {
try {
return JSON.parse(value)
} catch {
return value
return undefined
}
}
}

return null
return undefined
},
removeItem<Key extends keyof SafeLocalStorageItems>(key: Key): void {
if (isSafe()) {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/controllers/ChainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
},

Expand Down

0 comments on commit bb68d9c

Please sign in to comment.