From 5173f98172d6c72e8b824f87324e517c43492568 Mon Sep 17 00:00:00 2001 From: enesozturk Date: Tue, 10 Dec 2024 19:08:08 +0300 Subject: [PATCH] chore: save changes --- packages/adapters/solana/src/client.ts | 8 +++---- packages/appkit/src/client.ts | 24 +++++++++---------- .../appkit/src/universal-adapter/client.ts | 1 + .../core/src/controllers/ChainController.ts | 13 ++++------ packages/core/src/utils/StorageUtil.ts | 14 +++++++++++ 5 files changed, 36 insertions(+), 24 deletions(-) diff --git a/packages/adapters/solana/src/client.ts b/packages/adapters/solana/src/client.ts index 98b5daefef..d8b026f760 100644 --- a/packages/adapters/solana/src/client.ts +++ b/packages/adapters/solana/src/client.ts @@ -9,6 +9,7 @@ import { AlertController, CoreHelperUtil, EventsController, + StorageUtil, type ConnectorType, type Provider } from '@reown/appkit-core' @@ -331,6 +332,7 @@ export class SolanaAdapter extends AdapterBlueprint { const { caipNetwork, provider, providerType } = params if (providerType === 'ID_AUTH') { + console.log('>>> solana.switchNetwork', caipNetwork) await (provider as unknown as W3mFrameProvider).switchNetwork(caipNetwork.id) const user = await (provider as unknown as W3mFrameProvider).getUser({ chainId: caipNetwork.id @@ -452,10 +454,8 @@ export class SolanaAdapter extends AdapterBlueprint { // For standard Solana wallets const address = await selectedProvider.connect() - const caipNetwork = - NetworkUtil.getFirstNetworkByNamespace(this.caipNetworks, CommonConstantsUtil.CHAIN.SOLANA) || - solana - const chainId = caipNetwork.id as string + const { chainId: activeChainId } = StorageUtil.getActiveNetworkProps() + const chainId = activeChainId || solana.id this.listenProviderEvents(selectedProvider as unknown as WalletStandardProvider) diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index d7b34efd33..049a8a005e 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -1024,6 +1024,7 @@ export class AppKit { const providerType = ProviderUtil.state.providerIds[ChainController.state.activeChain as ChainNamespace] + console.log('>>> adapter?.switchNetwork', caipNetwork) await adapter?.switchNetwork({ caipNetwork, provider, providerType }) this.setCaipNetwork(caipNetwork) await this.syncAccount({ @@ -1489,26 +1490,25 @@ export class AppKit { }: Pick & { chainNamespace: ChainNamespace }) { - const activeNamespace = StorageUtil.getActiveNamespace() + const { namespace: activeNamespace, chainId: activeChainId } = + StorageUtil.getActiveNetworkProps() + const chainIdToUse = chainId || activeChainId + // Only update state when needed if (address.toLowerCase() !== AccountController.state.address?.toLowerCase()) { this.setCaipAddress(`${chainNamespace}:${chainId}:${address}`, chainNamespace) - await this.syncIdentity({ address, chainId, chainNamespace }) + await this.syncIdentity({ address, chainId: chainId, chainNamespace }) } this.setStatus('connected', chainNamespace) - if (chainNamespace === ChainController.state.activeChain) { - const caipNetwork = this.caipNetworks?.find( - n => n.id === chainId && n.chainNamespace === chainNamespace - ) + if (chainIdToUse && chainNamespace === activeNamespace) { + const caipNetwork = this.caipNetworks?.find(n => n.id === chainIdToUse) const fallBackCaipNetwork = this.caipNetworks?.find(n => n.chainNamespace === chainNamespace) - - if (activeNamespace === chainNamespace) { - this.setCaipNetwork(caipNetwork || fallBackCaipNetwork) - this.syncConnectedWalletInfo(chainNamespace) - } - await this.syncBalance({ address, chainId, chainNamespace }) + console.log('>>> THIS', caipNetwork || fallBackCaipNetwork) + this.setCaipNetwork(caipNetwork || fallBackCaipNetwork) + this.syncConnectedWalletInfo(chainNamespace) + await this.syncBalance({ address, chainId: chainIdToUse, chainNamespace }) } } diff --git a/packages/appkit/src/universal-adapter/client.ts b/packages/appkit/src/universal-adapter/client.ts index 8e189f748e..0902dc4741 100644 --- a/packages/appkit/src/universal-adapter/client.ts +++ b/packages/appkit/src/universal-adapter/client.ts @@ -182,6 +182,7 @@ export class UniversalAdapter extends AdapterBlueprint { // eslint-disable-next-line @typescript-eslint/require-await public async switchNetwork(params: AdapterBlueprint.SwitchNetworkParams) { const { caipNetwork } = params + console.log('>>> upa.switchNetwork', caipNetwork) const connector = this.connectors.find(c => c.type === 'WALLET_CONNECT') const provider = connector?.provider as UniversalProvider diff --git a/packages/core/src/controllers/ChainController.ts b/packages/core/src/controllers/ChainController.ts index ba8832f7b8..773baf8133 100644 --- a/packages/core/src/controllers/ChainController.ts +++ b/packages/core/src/controllers/ChainController.ts @@ -101,14 +101,11 @@ export const ChainController = { }, initialize(adapters: ChainAdapter[], caipNetworks: CaipNetwork[] | undefined) { - const activeNamespace = StorageUtil.getActiveNamespace() - const activeCaipNetwork = caipNetworks?.find( - network => network.chainNamespace === activeNamespace - ) - const activeAdapterFromStorage = adapters.find( - adapter => adapter?.namespace === activeNamespace - ) - const adapterToActivate = activeAdapterFromStorage || adapters?.[0] + const { chainId: activeChainId, namespace: activeNamespace } = + StorageUtil.getActiveNetworkProps() + const activeCaipNetwork = caipNetworks?.find(network => network.id === activeChainId) + const defaultAdapter = adapters.find(adapter => adapter?.namespace === activeNamespace) + const adapterToActivate = defaultAdapter || adapters?.[0] if (adapters?.length === 0 || !adapterToActivate) { state.noAdapters = true diff --git a/packages/core/src/utils/StorageUtil.ts b/packages/core/src/utils/StorageUtil.ts index 536ba9f525..9ac41ba2c7 100644 --- a/packages/core/src/utils/StorageUtil.ts +++ b/packages/core/src/utils/StorageUtil.ts @@ -9,6 +9,20 @@ import type { WcWallet, ConnectorType, SocialProvider, ConnectionStatus } from ' // -- Utility ----------------------------------------------------------------- export const StorageUtil = { + getActiveNetworkProps() { + const activeNamespace = StorageUtil.getActiveNamespace() + const activeCaipNetworkId = StorageUtil.getActiveCaipNetworkId() + const caipNetworkIdFromStorage = activeCaipNetworkId + ? activeCaipNetworkId.split(':')[1] + : undefined + + return { + namespace: activeNamespace, + caipNetworkId: activeCaipNetworkId, + chainId: caipNetworkIdFromStorage + } + }, + setWalletConnectDeepLink({ name, href }: { href: string; name: string }) { try { SafeLocalStorage.setItem(SafeLocalStorageKeys.DEEPLINK_CHOICE, JSON.stringify({ href, name }))