Skip to content

Commit

Permalink
chore: part 4 - active caip network handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir committed Sep 27, 2024
1 parent efbbe89 commit c18b22a
Show file tree
Hide file tree
Showing 20 changed files with 132 additions and 254 deletions.
2 changes: 1 addition & 1 deletion packages/adapters/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ export class EthersAdapter {
}
} else {
this.appKit?.resetWcConnection()
this.appKit?.resetNetwork()
this.appKit?.resetNetwork(this.chainNamespace)
this.appKit?.setAllAccounts([], this.chainNamespace)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/ethers5/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ export class Ethers5Adapter {
}
} else {
this.appKit?.resetWcConnection()
this.appKit?.resetNetwork()
this.appKit?.resetNetwork(this.chainNamespace)
this.appKit?.setAllAccounts([], this.chainNamespace)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/solana/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export class SolanaAdapter implements ChainAdapter {
await this.syncNetwork(address)
} else {
this.appKit?.resetWcConnection()
this.appKit?.resetNetwork()
this.appKit?.resetNetwork(this.chainNamespace)
this.appKit?.resetAccount(this.chainNamespace)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ export class WagmiAdapter implements ChainAdapter {
if (status === 'disconnected' && !isConnected) {
this.appKit?.resetAccount(this.chainNamespace)
this.appKit?.resetWcConnection()
this.appKit?.resetNetwork()
this.appKit?.resetNetwork(this.chainNamespace)
this.appKit?.setAllAccounts([], this.chainNamespace)
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID)

Expand Down
2 changes: 1 addition & 1 deletion packages/appkit/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export * from '@reown/appkit-scaffold-ui'
export * from '../src/utils/index.js'
export type * from '@reown/appkit-core'
export type { CaipNetwork, CaipAddress, CaipNetworkId } from '@reown/appkit-common'
export { CoreHelperUtil, AccountController, NetworkController } from '@reown/appkit-core'
export { CoreHelperUtil, AccountController } from '@reown/appkit-core'

type CreateAppKit = Omit<AppKitOptions, 'sdkType' | 'sdkVersion'>

Expand Down
2 changes: 1 addition & 1 deletion packages/appkit/exports/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export * from '../src/library/react/index.js'
export * from '../src/utils/index.js'
export type * from '@reown/appkit-core'
export type { CaipNetwork, CaipAddress, CaipNetworkId } from '@reown/appkit-common'
export { CoreHelperUtil, AccountController, NetworkController } from '@reown/appkit-core'
export { CoreHelperUtil, AccountController } from '@reown/appkit-core'

export let modal: AppKit | undefined = undefined

Expand Down
2 changes: 1 addition & 1 deletion packages/appkit/exports/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export * from '../src/library/vue/index.js'
export * from '../src/utils/index.js'
export type * from '@reown/appkit-core'
export type { CaipNetwork, CaipAddress, CaipNetworkId } from '@reown/appkit-common'
export { CoreHelperUtil, AccountController, NetworkController } from '@reown/appkit-core'
export { CoreHelperUtil, AccountController } from '@reown/appkit-core'

let modal: AppKit | undefined = undefined

Expand Down
11 changes: 5 additions & 6 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
RouterController,
EnsController,
OptionsController,
NetworkController,
AssetUtil,
ApiController
} from '@reown/appkit-core'
Expand All @@ -43,7 +42,7 @@ import type { W3mFrameTypes } from '@reown/appkit-wallet'
import { ProviderUtil } from './store/ProviderUtil.js'

// -- Export Controllers -------------------------------------------------------
export { AccountController, NetworkController }
export { AccountController }

// -- Types --------------------------------------------------------------------
export interface OpenOptions {
Expand Down Expand Up @@ -107,7 +106,7 @@ export class AppKit {
}

public switchNetwork(caipNetwork: CaipNetwork) {
return NetworkController.switchActiveNetwork(caipNetwork)
return ChainController.switchActiveNetwork(caipNetwork)
}

public getWalletProvider() {
Expand Down Expand Up @@ -161,7 +160,7 @@ export class AppKit {
}

public subscribeCaipNetworkChange(callback: (newState?: CaipNetwork) => void) {
NetworkController.subscribeKey('caipNetwork', callback)
ChainController.subscribeKey('activeCaipNetwork', callback)
}

public getState() {
Expand Down Expand Up @@ -317,8 +316,8 @@ export class AppKit {
public setApprovedCaipNetworksData: (typeof ChainController)['setApprovedCaipNetworksData'] =
namespace => ChainController.setApprovedCaipNetworksData(namespace)

public resetNetwork: (typeof NetworkController)['resetNetwork'] = () => {
NetworkController.resetNetwork()
public resetNetwork: (typeof ChainController)['resetNetwork'] = (namespace: ChainNamespace) => {
ChainController.resetNetwork(namespace)
}

public setConnectors: (typeof ConnectorController)['setConnectors'] = connectors => {
Expand Down
7 changes: 3 additions & 4 deletions packages/appkit/src/tests/appkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { describe, it, expect, beforeEach, vi } from 'vitest'
import { AppKit } from '../client'
import {
AccountController,
NetworkController,
ModalController,
ThemeController,
PublicStateController,
Expand Down Expand Up @@ -112,7 +111,7 @@ describe('Base', () => {
it('should subscribe to CAIP network changes', () => {
const callback = vi.fn()
appKit.subscribeCaipNetworkChange(callback)
expect(NetworkController.subscribeKey).toHaveBeenCalledWith('caipNetwork', callback)
expect(ChainController.subscribeKey).toHaveBeenCalledWith('activeCaipNetwork', callback)
})

it('should get state', () => {
Expand Down Expand Up @@ -332,8 +331,8 @@ describe('Base', () => {
})

it('should reset network', () => {
appKit.resetNetwork()
expect(NetworkController.resetNetwork).toHaveBeenCalled()
appKit.resetNetwork('eip155')
expect(ChainController.resetNetwork).toHaveBeenCalled()
})

it('should reset WC connection', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/appkit/src/universal-adapter/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ export class UniversalAdapterClient {
})
} else {
this.appKit?.resetWcConnection()
this.appKit?.resetNetwork()
this.appKit?.resetNetwork(this.chainNamespace)
this.syncAccounts(true)
}
}
Expand Down
12 changes: 3 additions & 9 deletions packages/appkit/src/utils/TypesUtil.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import type { CaipNetwork, ThemeVariables } from '@reown/appkit-common'
import type {
ChainAdapter,
Metadata,
NetworkControllerState,
OptionsControllerState,
ThemeMode
} from '@reown/appkit-core'
import type { ChainAdapter, Metadata, OptionsControllerState, ThemeMode } from '@reown/appkit-core'
import type { AppKitSIWEClient } from '@reown/appkit-siwe'

export type AppKitOptions = {
Expand Down Expand Up @@ -42,7 +36,7 @@ export type AppKitOptions = {
* Allow users to switch to an unsupported chain.
* @see https://docs.reown.com/appkit/react/core/options#allowunsupportedchain
*/
allowUnsupportedChain?: NetworkControllerState['allowUnsupportedCaipNetwork']
allowUnsupportedChain?: boolean
/**
* You can set the desired caipnetworks for the app:
* @see https://docs.reown.com/appkit/react/core/options#defaultchain
Expand All @@ -52,7 +46,7 @@ export type AppKitOptions = {
* You can set a desired caipnetwork for the initial connection:
* @see https://docs.reown.com/appkit/react/core/options#defaultchain
*/
defaultNetwork?: NetworkControllerState['caipNetwork']
defaultNetwork?: CaipNetwork
/**
* Add or override the modal's network images.
* @see https://docs.reown.com/appkit/react/core/options#chainimages
Expand Down
6 changes: 0 additions & 6 deletions packages/core/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ export type { AccountControllerState } from '../src/controllers/AccountControlle
export { ChainController } from '../src/controllers/ChainController.js'
export type { ChainControllerState } from '../src/controllers/ChainController.js'

export { NetworkController } from '../src/controllers/NetworkController.js'
export type {
NetworkControllerClient,
NetworkControllerState
} from '../src/controllers/NetworkController.js'

export { OnRampController } from '../src/controllers/OnRampController.js'
export type { OnRampControllerState, OnRampProvider } from '../src/controllers/OnRampController.js'

Expand Down
41 changes: 25 additions & 16 deletions packages/core/src/controllers/ChainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { StorageUtil } from '../utils/StorageUtil.js'
import { CoreHelperUtil } from '../utils/CoreHelperUtil.js'
import { ConstantsUtil } from '../utils/ConstantsUtil.js'
import { ModalController } from './ModalController.js'
import { EventsController } from './EventsController.js'

// -- Types --------------------------------------------- //
export interface ChainControllerState {
Expand Down Expand Up @@ -258,22 +259,22 @@ export const ChainController = {
}
},

/**
* The setCaipNetwork function is being called for different purposes and it needs to be controlled if it should replace the NetworkController state or not.
* While we initializing the adapters, we need to set the caipNetwork without replacing the state.
* But when we switch the network, we need to replace the state.
* @param chain
* @param caipNetwork
* @param shouldReplace - if true, it will replace the NetworkController state
*/
setCaipNetwork(chain: ChainNamespace, caipNetwork: AdapterNetworkState['caipNetwork']) {
state.activeChain = caipNetwork?.chainNamespace
state.activeCaipNetwork = caipNetwork
PublicStateController.set({
activeChain: state.activeChain,
selectedNetworkId: state.activeCaipNetwork?.id
})
this.setAdapterNetworkState(chain, { caipNetwork })
async switchActiveNetwork(network: CaipNetwork) {
const networkControllerClient = this.getNetworkControllerClient(network.chainNamespace)

if (networkControllerClient) {
await networkControllerClient.switchCaipNetwork(network)
}

this.setActiveCaipNetwork(network)

if (network) {
EventsController.sendEvent({
type: 'track',
event: 'SWITCH_NETWORK',
properties: { network: network.id }
})
}
},

setActiveConnector(connector: ChainControllerState['activeConnector']) {
Expand Down Expand Up @@ -485,6 +486,14 @@ export const ChainController = {
}, 300)
},

resetNetwork(namespace: ChainNamespace) {
ChainController.setAdapterNetworkState(namespace, {
approvedCaipNetworkIds: undefined,
supportsAllNetworks: true,
smartAccountEnabledNetworks: []
})
},

resetAccount(chain: ChainNamespace | undefined) {
const chainToWrite = chain

Expand Down
92 changes: 0 additions & 92 deletions packages/core/src/controllers/NetworkController.ts

This file was deleted.

9 changes: 8 additions & 1 deletion packages/core/src/utils/TypeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type {
SdkFramework,
AppKitSdkVersion
} from '@reown/appkit-common'
import type { NetworkControllerClient } from '../controllers/NetworkController.js'
import type { ConnectionControllerClient } from '../controllers/ConnectionController.js'
import type { AccountControllerState } from '../controllers/AccountController.js'
import type { OnRampProviderOption } from '../controllers/OnRampController.js'
Expand Down Expand Up @@ -832,6 +831,14 @@ export interface WriteContractArgs {
abi: any
}

export interface NetworkControllerClient {
switchCaipNetwork: (network: CaipNetwork) => Promise<void>
getApprovedCaipNetworksData: () => Promise<{
approvedCaipNetworkIds: CaipNetworkId[]
supportsAllNetworks: boolean
}>
}

export type AdapterNetworkState = {
supportsAllNetworks: boolean
isUnsupportedChain?: boolean
Expand Down
Loading

0 comments on commit c18b22a

Please sign in to comment.