Skip to content

Commit

Permalink
chore: save changes
Browse files Browse the repository at this point in the history
  • Loading branch information
enesozturk committed Dec 10, 2024
1 parent b6a8dc5 commit 5173f98
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
8 changes: 4 additions & 4 deletions packages/adapters/solana/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
AlertController,
CoreHelperUtil,
EventsController,
StorageUtil,
type ConnectorType,
type Provider
} from '@reown/appkit-core'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
24 changes: 12 additions & 12 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -1489,26 +1490,25 @@ export class AppKit {
}: Pick<AdapterBlueprint.ConnectResult, 'address' | 'chainId'> & {
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 })
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/appkit/src/universal-adapter/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 5 additions & 8 deletions packages/core/src/controllers/ChainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/utils/StorageUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down

0 comments on commit 5173f98

Please sign in to comment.