Skip to content

Commit

Permalink
fix: defaultChain option setting to base (#2740)
Browse files Browse the repository at this point in the history
  • Loading branch information
enesozturk authored Aug 23, 2024
1 parent c45fed7 commit 8e91107
Show file tree
Hide file tree
Showing 21 changed files with 135 additions and 84 deletions.
1 change: 1 addition & 0 deletions apps/laboratory/src/pages/library/ethers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const modal = createWeb3Modal({
coinbasePreference: 'smartWalletOnly'
}),
chains: EthersConstants.chains,
defaultChain: EthersConstants.chains[1],
projectId: ConstantsUtil.ProjectId,
enableAnalytics: true,
metadata: ConstantsUtil.Metadata,
Expand Down
1 change: 1 addition & 0 deletions apps/laboratory/src/pages/library/ethers5.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const modal = createWeb3Modal({
chains: EthersConstants.chains,
coinbasePreference: 'smartWalletOnly'
}),
defaultChain: EthersConstants.chains[0],
chains: EthersConstants.chains,
projectId: ConstantsUtil.ProjectId,
enableAnalytics: true,
Expand Down
1 change: 1 addition & 0 deletions apps/laboratory/src/pages/library/solana.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const modal = createWeb3Modal({
solanaConfig,
projectId: ConstantsUtil.ProjectId,
metadata: ConstantsUtil.Metadata,
defaultChain: solana,
chains,
enableAnalytics: false,
termsConditionsUrl: 'https://walletconnect.com/terms',
Expand Down
2 changes: 2 additions & 0 deletions apps/laboratory/src/pages/library/wagmi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { ThemeStore } from '../../utils/StoreUtil'
import { getWagmiConfig } from '../../utils/WagmiConstants'
import { ConstantsUtil } from '../../utils/ConstantsUtil'
import { WagmiModalInfo } from '../../components/Wagmi/WagmiModalInfo'
import { mainnet } from 'viem/chains'

const queryClient = new QueryClient()

const wagmiConfig = getWagmiConfig('default')

const modal = createWeb3Modal({
wagmiConfig,
defaultChain: mainnet,
projectId: ConstantsUtil.ProjectId,
enableAnalytics: true,
metadata: ConstantsUtil.Metadata,
Expand Down
4 changes: 0 additions & 4 deletions packages/base/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,6 @@ export class AppKit {
NetworkController.setCaipNetwork(caipNetwork)
}

public setActiveCaipNetwork: (typeof NetworkController)['setCaipNetwork'] = caipNetwork => {
NetworkController.setActiveCaipNetwork(caipNetwork)
}

public getCaipNetwork = () => NetworkController.state.caipNetwork

public setRequestedCaipNetworks: (typeof NetworkController)['setRequestedCaipNetworks'] = (
Expand Down
107 changes: 54 additions & 53 deletions packages/base/utils/TypesUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,57 @@ import type {
} from '@web3modal/core'
import type { SIWEControllerClient, Web3ModalSIWEClient } from '@web3modal/siwe'

export type AppKitOptions = OptionsControllerState & {
/**
* Adapter array to be used by the AppKit.
* @default []
*/
adapters?: ChainAdapter[]
/**
* Sign In With Ethereum configuration object.
* @default undefined
* @see https://docs.walletconnect.com/appkit/react/core/siwe#configure-your-siwe-client
*/
siweConfig?: Web3ModalSIWEClient
/**
* Theme mode configuration flag. By default themeMode option will be set to user system settings.
* @default `system`
* @type `dark` | `light`
* @see https://docs.walletconnect.com/appkit/react/core/theming
*/
themeMode?: ThemeMode
/**
* Theme variable configuration object.
* @default undefined
* @see https://docs.walletconnect.com/appkit/react/core/theming#themevariables
*/
themeVariables?: ThemeVariables
/**
* Allow users to switch to an unsupported chain.
* @see https://docs.walletconnect.com/appkit/react/core/options#allowunsupportedchain
*/
allowUnsupportedChain?: NetworkControllerState['allowUnsupportedChain']
/**
* You can set a desired chain for the initial connection:
* @see https://docs.walletconnect.com/appkit/react/core/options#defaultchain
*/
defaultChain?: NetworkControllerState['caipNetwork']
/**
* Add or override the modal's network images.
* @see https://docs.walletconnect.com/appkit/react/core/options#chainimages
*/
chainImages?: Record<number | string, string>
/**
* Set or override the images of any connector. The key of each property must match the id of the connector.
* @see https://docs.walletconnect.com/appkit/react/core/options#connectorimages
*/
connectorImages?: Record<string, string>
/**
* Tokens for AppKit to show the user's balance of.
* @see https://docs.walletconnect.com/appkit/react/core/options#tokens
*/
tokens?: Record<number, Token>
// -- Internal options ---------------------------------- //
siweControllerClient?: SIWEControllerClient
}
export type AppKitOptions<ChainType = NetworkControllerState['caipNetwork']> =
OptionsControllerState & {
/**
* Adapter array to be used by the AppKit.
* @default []
*/
adapters?: ChainAdapter[]
/**
* Sign In With Ethereum configuration object.
* @default undefined
* @see https://docs.walletconnect.com/appkit/react/core/siwe#configure-your-siwe-client
*/
siweConfig?: Web3ModalSIWEClient
/**
* Theme mode configuration flag. By default themeMode option will be set to user system settings.
* @default `system`
* @type `dark` | `light`
* @see https://docs.walletconnect.com/appkit/react/core/theming
*/
themeMode?: ThemeMode
/**
* Theme variable configuration object.
* @default undefined
* @see https://docs.walletconnect.com/appkit/react/core/theming#themevariables
*/
themeVariables?: ThemeVariables
/**
* Allow users to switch to an unsupported chain.
* @see https://docs.walletconnect.com/appkit/react/core/options#allowunsupportedchain
*/
allowUnsupportedChain?: NetworkControllerState['allowUnsupportedChain']
/**
* You can set a desired chain for the initial connection:
* @see https://docs.walletconnect.com/appkit/react/core/options#defaultchain
*/
defaultChain?: ChainType
/**
* Add or override the modal's network images.
* @see https://docs.walletconnect.com/appkit/react/core/options#chainimages
*/
chainImages?: Record<number | string, string>
/**
* Set or override the images of any connector. The key of each property must match the id of the connector.
* @see https://docs.walletconnect.com/appkit/react/core/options#connectorimages
*/
connectorImages?: Record<string, string>
/**
* Tokens for AppKit to show the user's balance of.
* @see https://docs.walletconnect.com/appkit/react/core/options#tokens
*/
tokens?: Record<number, Token>
// -- Internal options ---------------------------------- //
siweControllerClient?: SIWEControllerClient
}
6 changes: 5 additions & 1 deletion packages/core/src/controllers/EnsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { NetworkController } from './NetworkController.js'
import { NetworkUtil } from '@web3modal/common'
import { EnsUtil } from '../utils/EnsUtil.js'
import { ConstantsUtil } from '@web3modal/common'
import { ChainController } from './ChainController.js'

// -- Types --------------------------------------------- //
type Suggestion = {
Expand Down Expand Up @@ -142,7 +143,10 @@ export const EnsController = {
message
})

AccountController.setProfileName(`${name}${ConstantsUtil.WC_NAME_SUFFIX}`, network.chain)
AccountController.setProfileName(
`${name}${ConstantsUtil.WC_NAME_SUFFIX}`,
ChainController.state.activeChain
)
RouterController.replace('RegisterAccountNameSuccess')
} catch (e) {
const errorMessage = this.parseEnsApiError(e, `Error registering name ${name}`)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/controllers/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const NetworkController = {

setDefaultCaipNetwork(caipNetwork: NetworkControllerState['caipNetwork']) {
if (caipNetwork) {
ChainController.setCaipNetwork(caipNetwork.chain, caipNetwork)
ChainController.setCaipNetwork(caipNetwork.chain, caipNetwork, true)
ChainController.setChainNetworkData(caipNetwork.chain, { isDefaultCaipNetwork: true })
PublicStateController.set({ selectedNetworkId: caipNetwork.id })
}
Expand Down
10 changes: 8 additions & 2 deletions packages/ethers/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AppKit } from '@web3modal/base'
import type { AppKitOptions } from '@web3modal/base'
import { EVMEthersClient, type AdapterOptions } from '@web3modal/base/adapters/evm/ethers'
import { ConstantsUtil } from '@web3modal/scaffold-utils'
import { type Chain } from '@web3modal/scaffold-utils/ethers'

// -- Types -------------------------------------------------------------
export type { AdapterOptions } from '@web3modal/base/adapters/evm/ethers'
Expand All @@ -10,18 +11,23 @@ export type { AdapterOptions } from '@web3modal/base/adapters/evm/ethers'
export { defaultConfig } from '@web3modal/base/adapters/evm/ethers'

// -- Setup -------------------------------------------------------------
export type EthersAppKitOptions = Omit<AppKitOptions, 'adapters' | 'sdkType' | 'sdkVersion'> &
export type EthersAppKitOptions = Omit<
AppKitOptions<Chain>,
'adapters' | 'sdkType' | 'sdkVersion'
> &
AdapterOptions

export function createWeb3Modal(options: EthersAppKitOptions) {
const ethersAdapter = new EVMEthersClient({
ethersConfig: options.ethersConfig,
siweConfig: options.siweConfig,
chains: options.chains
chains: options.chains,
defaultChain: options.defaultChain
})

return new AppKit({
...options,
defaultChain: ethersAdapter.defaultChain,
adapters: [ethersAdapter],
sdkType: 'w3m',
sdkVersion: `html-ethers-${ConstantsUtil.VERSION}`
Expand Down
11 changes: 8 additions & 3 deletions packages/ethers/exports/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AppKit } from '@web3modal/base'
import type { AppKitOptions } from '@web3modal/base'
import { EVMEthersClient, type AdapterOptions } from '@web3modal/base/adapters/evm/ethers'
import { ConstantsUtil } from '@web3modal/scaffold-utils'
import { EthersStoreUtil } from '@web3modal/scaffold-utils/ethers'
import { EthersStoreUtil, type Chain } from '@web3modal/scaffold-utils/ethers'
import { getWeb3Modal } from '@web3modal/base/utils/library/react'
import { useSnapshot } from 'valtio'
import type { Eip1193Provider } from 'ethers'
Expand All @@ -16,17 +16,22 @@ export { defaultConfig } from '@web3modal/base/adapters/evm/ethers'
let appkit: AppKit | undefined = undefined
let ethersAdapter: EVMEthersClient | undefined = undefined

export type EthersAppKitOptions = Omit<AppKitOptions, 'adapters' | 'sdkType' | 'sdkVersion'> &
export type EthersAppKitOptions = Omit<
AppKitOptions<Chain>,
'adapters' | 'sdkType' | 'sdkVersion'
> &
AdapterOptions

export function createWeb3Modal(options: EthersAppKitOptions) {
ethersAdapter = new EVMEthersClient({
ethersConfig: options.ethersConfig,
siweConfig: options.siweConfig,
chains: options.chains
chains: options.chains,
defaultChain: options.defaultChain
})
appkit = new AppKit({
...options,
defaultChain: ethersAdapter.defaultChain,
adapters: [ethersAdapter],
sdkType: 'w3m',
sdkVersion: `react-ethers-${ConstantsUtil.VERSION}`
Expand Down
7 changes: 5 additions & 2 deletions packages/ethers/exports/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AppKit } from '@web3modal/base'
import type { AppKitOptions } from '@web3modal/base'
import { EVMEthersClient, type AdapterOptions } from '@web3modal/base/adapters/evm/ethers'
import { ConstantsUtil } from '@web3modal/scaffold-utils'
import { type Chain } from '@web3modal/scaffold-utils/ethers'
import { getWeb3Modal } from '@web3modal/base/utils/library/vue'
import { onUnmounted, ref } from 'vue'
import type { Eip1193Provider } from 'ethers'
Expand All @@ -13,17 +14,19 @@ export { defaultConfig } from '@web3modal/base/adapters/evm/ethers'
let appkit: AppKit | undefined = undefined
let ethersAdapter: EVMEthersClient | undefined = undefined

type EthersAppKitOptions = Omit<AppKitOptions, 'adapters' | 'sdkType' | 'sdkVersion'> &
type EthersAppKitOptions = Omit<AppKitOptions<Chain>, 'adapters' | 'sdkType' | 'sdkVersion'> &
AdapterOptions

export function createWeb3Modal(options: EthersAppKitOptions) {
ethersAdapter = new EVMEthersClient({
ethersConfig: options.ethersConfig,
siweConfig: options.siweConfig,
chains: options.chains
chains: options.chains,
defaultChain: options.defaultChain
})
appkit = new AppKit({
...options,
defaultChain: ethersAdapter.defaultChain,
adapters: [ethersAdapter],
sdkType: 'w3m',
sdkVersion: `vue-ethers-${ConstantsUtil.VERSION}`
Expand Down
7 changes: 5 additions & 2 deletions packages/ethers5/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AppKit } from '@web3modal/base'
import type { AppKitOptions } from '@web3modal/base'
import { EVMEthers5Client, type AdapterOptions } from '@web3modal/base/adapters/evm/ethers5'
import { ConstantsUtil } from '@web3modal/scaffold-utils'
import { type Chain } from '@web3modal/scaffold-utils/ethers'

// -- Types -------------------------------------------------------------
export type { AdapterOptions } from '@web3modal/base/adapters/evm/ethers'
Expand All @@ -10,18 +11,20 @@ export type { AdapterOptions } from '@web3modal/base/adapters/evm/ethers'
export { defaultConfig } from '@web3modal/base/adapters/evm/ethers'

// -- Setup -------------------------------------------------------------
type EthersAppKitOptions = Omit<AppKitOptions, 'adapters' | 'sdkType' | 'sdkVersion'> &
type EthersAppKitOptions = Omit<AppKitOptions<Chain>, 'adapters' | 'sdkType' | 'sdkVersion'> &
AdapterOptions

export function createWeb3Modal(options: EthersAppKitOptions) {
const ethers5Adapter = new EVMEthers5Client({
ethersConfig: options.ethersConfig,
siweConfig: options.siweConfig,
chains: options.chains
chains: options.chains,
defaultChain: options.defaultChain
})

return new AppKit({
...options,
defaultChain: ethers5Adapter.defaultChain,
adapters: [ethers5Adapter],
sdkType: 'w3m',
sdkVersion: `html-ethers5-${ConstantsUtil.VERSION}`
Expand Down
10 changes: 8 additions & 2 deletions packages/ethers5/exports/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { EthersStoreUtil } from '@web3modal/scaffold-utils/ethers'
import { getWeb3Modal } from '@web3modal/base/utils/library/react'
import { useSnapshot } from 'valtio'
import { ethers } from 'ethers'
import { type Chain } from '@web3modal/scaffold-utils/ethers'

// -- Configs -----------------------------------------------------------
export { defaultConfig } from '@web3modal/base/adapters/evm/ethers'
Expand All @@ -16,17 +17,22 @@ export { defaultConfig } from '@web3modal/base/adapters/evm/ethers'
let appkit: AppKit | undefined = undefined
let ethersAdapter: EVMEthersClient | undefined = undefined

export type Ethers5AppKitOptions = Omit<AppKitOptions, 'adapters' | 'sdkType' | 'sdkVersion'> &
export type Ethers5AppKitOptions = Omit<
AppKitOptions<Chain>,
'adapters' | 'sdkType' | 'sdkVersion'
> &
AdapterOptions

export function createWeb3Modal(options: Ethers5AppKitOptions) {
ethersAdapter = new EVMEthersClient({
ethersConfig: options.ethersConfig,
siweConfig: options.siweConfig,
chains: options.chains
chains: options.chains,
defaultChain: options.defaultChain
})
appkit = new AppKit({
...options,
defaultChain: ethersAdapter.defaultChain,
adapters: [ethersAdapter],
sdkType: 'w3m',
sdkVersion: `react-ethers5-${ConstantsUtil.VERSION}`
Expand Down
7 changes: 5 additions & 2 deletions packages/ethers5/exports/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ConstantsUtil } from '@web3modal/scaffold-utils'
import { getWeb3Modal } from '@web3modal/base/utils/library/vue'
import { onUnmounted, ref } from 'vue'
import { ethers } from 'ethers'
import { type Chain } from '@web3modal/scaffold-utils/ethers'

// -- Configs -----------------------------------------------------------
export { defaultConfig } from '@web3modal/base/adapters/evm/ethers'
Expand All @@ -13,17 +14,19 @@ export { defaultConfig } from '@web3modal/base/adapters/evm/ethers'
let appkit: AppKit | undefined = undefined
let ethersAdapter: EVMEthersClient | undefined = undefined

type EthersAppKitOptions = Omit<AppKitOptions, 'adapters' | 'sdkType' | 'sdkVersion'> &
type EthersAppKitOptions = Omit<AppKitOptions<Chain>, 'adapters' | 'sdkType' | 'sdkVersion'> &
AdapterOptions

export function createWeb3Modal(options: EthersAppKitOptions) {
ethersAdapter = new EVMEthersClient({
ethersConfig: options.ethersConfig,
siweConfig: options.siweConfig,
chains: options.chains
chains: options.chains,
defaultChain: options.defaultChain
})
appkit = new AppKit({
...options,
defaultChain: ethersAdapter.defaultChain,
adapters: [ethersAdapter],
sdkType: 'w3m',
sdkVersion: `vue-ethers5-${ConstantsUtil.VERSION}`
Expand Down
Loading

0 comments on commit 8e91107

Please sign in to comment.