diff --git a/examples/next-wagmi/src/config/index.ts b/examples/next-wagmi/src/config/index.ts index 64d12c9337..0795b185a4 100644 --- a/examples/next-wagmi/src/config/index.ts +++ b/examples/next-wagmi/src/config/index.ts @@ -17,7 +17,6 @@ export const config = defaultWagmiConfig({ url: 'https://myapp.com', icons: ['https://myapp.com/favicon.ico'] }, - enableInjected: true, enableWalletConnect: true, enableEIP6963: true, enableCoinbase: true, diff --git a/packages/ethers/src/client.ts b/packages/ethers/src/client.ts index d5336d99e5..a579056f12 100644 --- a/packages/ethers/src/client.ts +++ b/packages/ethers/src/client.ts @@ -245,19 +245,7 @@ export class Web3Modal extends Web3ModalScaffold { info: Info provider: Provider }) => { - if (id === ConstantsUtil.INJECTED_CONNECTOR_ID) { - const InjectedProvider = ethersConfig.injected - if (!InjectedProvider) { - throw new Error('connectionControllerClient:connectInjected - provider is undefined') - } - try { - EthersStoreUtil.setError(undefined) - await InjectedProvider.request({ method: 'eth_requestAccounts' }) - this.setInjectedProvider(ethersConfig) - } catch (error) { - EthersStoreUtil.setError(error) - } - } else if (id === ConstantsUtil.EIP6963_CONNECTOR_ID && info && provider) { + if (id === ConstantsUtil.EIP6963_CONNECTOR_ID && info && provider) { try { EthersStoreUtil.setError(undefined) await provider.request({ method: 'eth_requestAccounts' }) @@ -288,12 +276,6 @@ export class Web3Modal extends Web3ModalScaffold { return Boolean(window.ethereum) } - if (ethersConfig.injected) { - if (!window?.ethereum) { - return false - } - } - return ids.some(id => Boolean(window.ethereum?.[String(id)])) }, @@ -494,9 +476,6 @@ export class Web3Modal extends Web3ModalScaffold { this.syncEmailConnector(w3mOptions.projectId) } - if (ethersConfig.injected) { - this.checkActiveInjectedProvider(ethersConfig) - } if (ethersConfig.coinbase) { this.checkActiveCoinbaseProvider(ethersConfig) } @@ -565,7 +544,7 @@ export class Web3Modal extends Web3ModalScaffold { localStorage.removeItem(EthersConstantsUtil.WALLET_ID) EthersStoreUtil.reset() - if (providerType === 'injected' || providerType === 'eip6963') { + if (providerType === 'eip6963') { provider?.emit('disconnect') } else { const walletConnectProvider = provider as unknown as EthereumProvider @@ -654,18 +633,6 @@ export class Web3Modal extends Web3ModalScaffold { } } - private checkActiveInjectedProvider(config: ProviderType) { - const InjectedProvider = config.injected - const walletId = localStorage.getItem(EthersConstantsUtil.WALLET_ID) - - if (InjectedProvider) { - if (walletId === ConstantsUtil.INJECTED_CONNECTOR_ID) { - this.setInjectedProvider(config) - this.watchInjected(config) - } - } - } - private checkActiveCoinbaseProvider(config: ProviderType) { const CoinbaseProvider = config.coinbase as unknown as ExternalProvider const walletId = localStorage.getItem(EthersConstantsUtil.WALLET_ID) @@ -711,23 +678,6 @@ export class Web3Modal extends Web3ModalScaffold { } } - private async setInjectedProvider(config: ProviderType) { - window?.localStorage.setItem(EthersConstantsUtil.WALLET_ID, ConstantsUtil.INJECTED_CONNECTOR_ID) - const InjectedProvider = config.injected - - if (InjectedProvider) { - const { address, chainId } = await EthersHelpersUtil.getUserInfo(InjectedProvider) - if (address && chainId) { - EthersStoreUtil.setChainId(chainId) - EthersStoreUtil.setProviderType('injected') - EthersStoreUtil.setProvider(config.injected) - EthersStoreUtil.setIsConnected(true) - this.setAddress(address) - this.watchCoinbase(config) - } - } - } - private async setEIP6963Provider(provider: Provider, name: string) { window?.localStorage.setItem(EthersConstantsUtil.WALLET_ID, name) @@ -820,45 +770,6 @@ export class Web3Modal extends Web3ModalScaffold { } } - private watchInjected(config: ProviderType) { - const provider = config.injected - - function disconnectHandler() { - localStorage.removeItem(EthersConstantsUtil.WALLET_ID) - EthersStoreUtil.reset() - - provider?.removeListener('disconnect', disconnectHandler) - provider?.removeListener('accountsChanged', accountsChangedHandler) - provider?.removeListener('chainChanged', chainChangedHandler) - } - - function accountsChangedHandler(accounts: string[]) { - const currentAccount = accounts?.[0] - if (currentAccount) { - EthersStoreUtil.setAddress(getOriginalAddress(currentAccount) as Address) - } else { - localStorage.removeItem(EthersConstantsUtil.WALLET_ID) - EthersStoreUtil.reset() - } - } - - function chainChangedHandler(chainId: string) { - if (chainId) { - const chain = - typeof chainId === 'string' - ? EthersHelpersUtil.hexStringToNumber(chainId) - : Number(chainId) - EthersStoreUtil.setChainId(chain) - } - } - - if (provider) { - provider.on('disconnect', disconnectHandler) - provider.on('accountsChanged', accountsChangedHandler) - provider.on('chainChanged', chainChangedHandler) - } - } - private watchEIP6963(provider: Provider) { function disconnectHandler() { localStorage.removeItem(EthersConstantsUtil.WALLET_ID) @@ -1199,29 +1110,6 @@ export class Web3Modal extends Web3ModalScaffold { } } } - } else if (providerType === ConstantsUtil.INJECTED_CONNECTOR_ID && chain) { - const InjectedProvider = provider - if (InjectedProvider) { - try { - await InjectedProvider.request({ - method: 'wallet_switchEthereumChain', - params: [{ chainId: EthersHelpersUtil.numberToHexString(chain.chainId) }] - }) - EthersStoreUtil.setChainId(chain.chainId) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - } catch (switchError: any) { - if ( - switchError.code === EthersConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID || - switchError.code === EthersConstantsUtil.ERROR_CODE_DEFAULT || - switchError?.data?.originalError?.code === - EthersConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID - ) { - await EthersHelpersUtil.addEthereumChain(InjectedProvider, chain) - } else { - throw new Error('Chain is not supported') - } - } - } } else if (providerType === ConstantsUtil.EIP6963_CONNECTOR_ID && chain) { const EIP6963Provider = provider @@ -1305,21 +1193,6 @@ export class Web3Modal extends Web3ModalScaffold { }) } - if (config.injected) { - const injectedConnectorType = - PresetsUtil.ConnectorTypesMap[ConstantsUtil.INJECTED_CONNECTOR_ID] - if (injectedConnectorType) { - w3mConnectors.push({ - id: ConstantsUtil.INJECTED_CONNECTOR_ID, - explorerId: PresetsUtil.ConnectorExplorerIds[ConstantsUtil.INJECTED_CONNECTOR_ID], - imageId: PresetsUtil.ConnectorImageIds[ConstantsUtil.INJECTED_CONNECTOR_ID], - imageUrl: this.options?.connectorImages?.[ConstantsUtil.INJECTED_CONNECTOR_ID], - name: PresetsUtil.ConnectorNamesMap[ConstantsUtil.INJECTED_CONNECTOR_ID], - type: injectedConnectorType - }) - } - } - if (config.coinbase) { w3mConnectors.push({ id: ConstantsUtil.COINBASE_CONNECTOR_ID, diff --git a/packages/ethers/src/utils/defaultConfig.ts b/packages/ethers/src/utils/defaultConfig.ts index 7c2148bc05..1959b8fd6d 100644 --- a/packages/ethers/src/utils/defaultConfig.ts +++ b/packages/ethers/src/utils/defaultConfig.ts @@ -4,9 +4,13 @@ import { CoinbaseWalletSDK } from '@coinbase/wallet-sdk' export interface ConfigOptions { enableEIP6963?: boolean - enableInjected?: boolean enableCoinbase?: boolean enableEmail?: boolean + /** + * Use enableEIP6963 to show all injected wallets + * @deprecated + */ + enableInjected?: boolean rpcUrl?: string defaultChainId?: number metadata: Metadata @@ -15,40 +19,19 @@ export interface ConfigOptions { export function defaultConfig(options: ConfigOptions) { const { enableEIP6963 = true, - enableInjected = true, enableCoinbase = true, enableEmail = false, + metadata, rpcUrl, defaultChainId } = options - // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents - let injectedProvider: Provider | undefined = undefined // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents let coinbaseProvider: Provider | undefined = undefined const providers: ProviderType = { metadata } - function getInjectedProvider() { - if (injectedProvider) { - return injectedProvider - } - - if (typeof window === 'undefined') { - return undefined - } - - if (!window.ethereum) { - return undefined - } - - // @ts-expect-error window.ethereum satisfies Provider - injectedProvider = window.ethereum - - return injectedProvider - } - function getCoinbaseProvider() { if (coinbaseProvider) { return coinbaseProvider @@ -70,10 +53,6 @@ export function defaultConfig(options: ConfigOptions) { return coinbaseProvider } - if (enableInjected) { - providers.injected = getInjectedProvider() - } - if (enableCoinbase && rpcUrl && defaultChainId) { providers.coinbase = getCoinbaseProvider() } diff --git a/packages/ethers5/src/client.ts b/packages/ethers5/src/client.ts index dbc953ec01..adbd3fc359 100644 --- a/packages/ethers5/src/client.ts +++ b/packages/ethers5/src/client.ts @@ -224,19 +224,7 @@ export class Web3Modal extends Web3ModalScaffold { info: Info provider: Provider }) => { - if (id === ConstantsUtil.INJECTED_CONNECTOR_ID) { - const InjectedProvider = ethersConfig.injected - if (!InjectedProvider) { - throw new Error('connectionControllerClient:connectInjected - provider is undefined') - } - try { - EthersStoreUtil.setError(undefined) - await InjectedProvider.request({ method: 'eth_requestAccounts' }) - this.setInjectedProvider(ethersConfig) - } catch (error) { - EthersStoreUtil.setError(error) - } - } else if (id === ConstantsUtil.EIP6963_CONNECTOR_ID && info && provider) { + if (id === ConstantsUtil.EIP6963_CONNECTOR_ID && info && provider) { try { EthersStoreUtil.setError(undefined) await provider.request({ method: 'eth_requestAccounts' }) @@ -265,12 +253,6 @@ export class Web3Modal extends Web3ModalScaffold { return Boolean(window.ethereum) } - if (ethersConfig.injected) { - if (!window?.ethereum) { - return false - } - } - return ids.some(id => Boolean(window.ethereum?.[String(id)])) }, @@ -378,9 +360,6 @@ export class Web3Modal extends Web3ModalScaffold { } } - if (ethersConfig.injected) { - this.checkActiveInjectedProvider(ethersConfig) - } if (ethersConfig.coinbase) { this.checkActiveCoinbaseProvider(ethersConfig) } @@ -448,7 +427,7 @@ export class Web3Modal extends Web3ModalScaffold { localStorage.removeItem(EthersConstantsUtil.WALLET_ID) EthersStoreUtil.reset() - if (providerType === 'injected' || providerType === 'eip6963') { + if (providerType === 'eip6963') { provider?.emit('disconnect') } else { await (provider as unknown as EthereumProvider).disconnect() @@ -529,18 +508,6 @@ export class Web3Modal extends Web3ModalScaffold { } } - private checkActiveInjectedProvider(config: ProviderType) { - const InjectedProvider = config.injected - const walletId = localStorage.getItem(EthersConstantsUtil.WALLET_ID) - - if (InjectedProvider) { - if (walletId === ConstantsUtil.INJECTED_CONNECTOR_ID) { - this.setInjectedProvider(config) - this.watchInjected(config) - } - } - } - private checkActiveCoinbaseProvider(config: ProviderType) { const CoinbaseProvider = config.coinbase as unknown as ExternalProvider const walletId = localStorage.getItem(EthersConstantsUtil.WALLET_ID) @@ -586,23 +553,6 @@ export class Web3Modal extends Web3ModalScaffold { } } - private async setInjectedProvider(config: ProviderType) { - window?.localStorage.setItem(EthersConstantsUtil.WALLET_ID, ConstantsUtil.INJECTED_CONNECTOR_ID) - const InjectedProvider = config.injected - - if (InjectedProvider) { - const { address, chainId } = await EthersHelpersUtil.getUserInfo(InjectedProvider) - if (address && chainId) { - EthersStoreUtil.setChainId(chainId) - EthersStoreUtil.setProviderType('injected') - EthersStoreUtil.setProvider(config.injected) - EthersStoreUtil.setIsConnected(true) - this.setAddress(address) - this.watchCoinbase(config) - } - } - } - private async setEIP6963Provider(provider: Provider, name: string) { window?.localStorage.setItem(EthersConstantsUtil.WALLET_ID, name) @@ -668,45 +618,6 @@ export class Web3Modal extends Web3ModalScaffold { } } - private watchInjected(config: ProviderType) { - const InjectedProvider = config.injected - - function disconnectHandler() { - localStorage.removeItem(EthersConstantsUtil.WALLET_ID) - EthersStoreUtil.reset() - - InjectedProvider?.removeListener('disconnect', disconnectHandler) - InjectedProvider?.removeListener('accountsChanged', accountsChangedHandler) - InjectedProvider?.removeListener('chainChanged', chainChangedHandler) - } - - function accountsChangedHandler(accounts: string[]) { - const currentAccount = accounts?.[0] - if (currentAccount) { - EthersStoreUtil.setAddress(utils.getAddress(currentAccount) as Address) - } else { - localStorage.removeItem(EthersConstantsUtil.WALLET_ID) - EthersStoreUtil.reset() - } - } - - function chainChangedHandler(chainId: string) { - if (chainId) { - const chain = - typeof chainId === 'string' - ? EthersHelpersUtil.hexStringToNumber(chainId) - : Number(chainId) - EthersStoreUtil.setChainId(chain) - } - } - - if (InjectedProvider) { - InjectedProvider.on('disconnect', disconnectHandler) - InjectedProvider.on('accountsChanged', accountsChangedHandler) - InjectedProvider.on('chainChanged', chainChangedHandler) - } - } - private watchEIP6963(provider: Provider) { function disconnectHandler() { localStorage.removeItem(EthersConstantsUtil.WALLET_ID) @@ -957,29 +868,6 @@ export class Web3Modal extends Web3ModalScaffold { } } } - } else if (providerType === ConstantsUtil.INJECTED_CONNECTOR_ID && chain) { - const InjectedProvider = provider - if (InjectedProvider) { - try { - await InjectedProvider.request({ - method: 'wallet_switchEthereumChain', - params: [{ chainId: EthersHelpersUtil.numberToHexString(chain.chainId) }] - }) - EthersStoreUtil.setChainId(chain.chainId) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - } catch (switchError: any) { - if ( - switchError.code === EthersConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID || - switchError.code === EthersConstantsUtil.ERROR_CODE_DEFAULT || - switchError?.data?.originalError?.code === - EthersConstantsUtil.ERROR_CODE_UNRECOGNIZED_CHAIN_ID - ) { - await EthersHelpersUtil.addEthereumChain(InjectedProvider, chain) - } else { - throw new Error('Chain is not supported') - } - } - } } else if (providerType === ConstantsUtil.EIP6963_CONNECTOR_ID && chain) { const EIP6963Provider = provider @@ -1044,21 +932,6 @@ export class Web3Modal extends Web3ModalScaffold { }) } - if (config.injected) { - const injectedConnectorType = - PresetsUtil.ConnectorTypesMap[ConstantsUtil.INJECTED_CONNECTOR_ID] - if (injectedConnectorType) { - w3mConnectors.push({ - id: ConstantsUtil.INJECTED_CONNECTOR_ID, - explorerId: PresetsUtil.ConnectorExplorerIds[ConstantsUtil.INJECTED_CONNECTOR_ID], - imageId: PresetsUtil.ConnectorImageIds[ConstantsUtil.INJECTED_CONNECTOR_ID], - imageUrl: this.options?.connectorImages?.[ConstantsUtil.INJECTED_CONNECTOR_ID], - name: PresetsUtil.ConnectorNamesMap[ConstantsUtil.INJECTED_CONNECTOR_ID], - type: injectedConnectorType - }) - } - } - if (config.coinbase) { w3mConnectors.push({ id: ConstantsUtil.COINBASE_CONNECTOR_ID, diff --git a/packages/ethers5/src/utils/defaultConfig.ts b/packages/ethers5/src/utils/defaultConfig.ts index 4df823e52f..3ef5f201b8 100644 --- a/packages/ethers5/src/utils/defaultConfig.ts +++ b/packages/ethers5/src/utils/defaultConfig.ts @@ -4,47 +4,24 @@ import { CoinbaseWalletSDK } from '@coinbase/wallet-sdk' export interface ConfigOptions { enableEIP6963?: boolean - enableInjected?: boolean enableCoinbase?: boolean + /** + * Use enableEIP6963 to show all injected wallets + * @deprecated + */ + enableInjected?: boolean rpcUrl?: string defaultChainId?: number metadata: Metadata } export function defaultConfig(options: ConfigOptions) { - const { - enableEIP6963 = true, - enableInjected = true, - enableCoinbase = true, - metadata, - rpcUrl, - defaultChainId - } = options + const { enableEIP6963 = true, enableCoinbase = true, metadata, rpcUrl, defaultChainId } = options - let injectedProvider: Provider | undefined = undefined let coinbaseProvider: Provider | undefined = undefined const providers: ProviderType = { metadata } - function getInjectedProvider() { - if (injectedProvider) { - return injectedProvider - } - - if (typeof window === 'undefined') { - return undefined - } - - if (!window.ethereum) { - return undefined - } - - // @ts-expect-error window.ethereum satisfies Provider - injectedProvider = window.ethereum - - return injectedProvider - } - function getCoinbaseProvider() { if (coinbaseProvider) { return coinbaseProvider @@ -66,10 +43,6 @@ export function defaultConfig(options: ConfigOptions) { return coinbaseProvider } - if (enableInjected) { - providers.injected = getInjectedProvider() - } - if (enableCoinbase && rpcUrl && defaultChainId) { providers.coinbase = getCoinbaseProvider() } diff --git a/packages/scaffold/src/views/w3m-connect-view/index.ts b/packages/scaffold/src/views/w3m-connect-view/index.ts index af8ffb401e..c4c7688e7f 100644 --- a/packages/scaffold/src/views/w3m-connect-view/index.ts +++ b/packages/scaffold/src/views/w3m-connect-view/index.ts @@ -145,7 +145,7 @@ export class W3mConnectView extends LitElement { private announcedTemplate() { return this.connectors.map(connector => { - if (connector.type !== 'ANNOUNCED') { + if (connector.type !== 'ANNOUNCED' || CoreHelperUtil.isMobile()) { return null } @@ -155,6 +155,8 @@ export class W3mConnectView extends LitElement { name=${connector.name ?? 'Unknown'} @click=${() => this.onConnector(connector)} tagVariant="success" + tagLabel="installed" + data-testid=${`wallet-selector-${connector.id}`} .installed=${true} > @@ -164,7 +166,7 @@ export class W3mConnectView extends LitElement { private injectedTemplate() { return this.connectors.map(connector => { - if (connector.type !== 'INJECTED') { + if (connector.type !== 'INJECTED' || CoreHelperUtil.isMobile()) { return null } @@ -177,6 +179,9 @@ export class W3mConnectView extends LitElement { imageSrc=${ifDefined(AssetUtil.getConnectorImage(connector))} .installed=${true} name=${connector.name ?? 'Unknown'} + tagVariant="success" + tagLabel="installed" + data-testid=${`wallet-selector-${connector.id}`} @click=${() => this.onConnector(connector)} > diff --git a/packages/wagmi/src/utils/defaultWagmiCoreConfig.ts b/packages/wagmi/src/utils/defaultWagmiCoreConfig.ts index 1c2fe54c57..82585a73f0 100644 --- a/packages/wagmi/src/utils/defaultWagmiCoreConfig.ts +++ b/packages/wagmi/src/utils/defaultWagmiCoreConfig.ts @@ -2,7 +2,7 @@ import '@web3modal/polyfills' import type { CreateConfigParameters, CreateConnectorFn } from '@wagmi/core' import { createConfig } from '@wagmi/core' -import { coinbaseWallet, injected } from '@wagmi/connectors' +import { coinbaseWallet } from '@wagmi/connectors' import { emailConnector } from '../connectors/EmailConnector.js' import { alphaWalletConnect } from '../connectors/alphaWalletConnect.js' @@ -11,10 +11,14 @@ import { getTransport } from './helpers.js' export type ConfigOptions = Partial & { chains: CreateConfigParameters['chains'] projectId: string - enableInjected?: boolean enableEIP6963?: boolean enableCoinbase?: boolean enableEmail?: boolean + /** + * Use enableEIP6963 to show all injected wallets + * @deprecated + */ + enableInjected?: boolean enableWalletConnect?: boolean metadata: { name: string @@ -28,7 +32,6 @@ export function defaultWagmiConfig({ projectId, chains, metadata, - enableInjected, enableCoinbase, enableEmail, enableWalletConnect, @@ -47,10 +50,6 @@ export function defaultWagmiConfig({ connectors.push(alphaWalletConnect({ projectId, metadata, showQrModal: false })) } - if (enableInjected !== false) { - connectors.push(injected({ shimDisconnect: true })) - } - if (enableCoinbase !== false) { connectors.push( coinbaseWallet({ diff --git a/packages/wagmi/src/utils/defaultWagmiReactConfig.ts b/packages/wagmi/src/utils/defaultWagmiReactConfig.ts index 5f7f96c458..4fd17ea44c 100644 --- a/packages/wagmi/src/utils/defaultWagmiReactConfig.ts +++ b/packages/wagmi/src/utils/defaultWagmiReactConfig.ts @@ -2,7 +2,7 @@ import '@web3modal/polyfills' import type { CreateConfigParameters, CreateConnectorFn, Config } from 'wagmi' import { createConfig } from 'wagmi' -import { coinbaseWallet, injected } from 'wagmi/connectors' +import { coinbaseWallet } from 'wagmi/connectors' import { emailConnector } from '../connectors/EmailConnector.js' import { alphaWalletConnect } from '../connectors/alphaWalletConnect.js' @@ -11,10 +11,14 @@ import { getTransport } from './helpers.js' export type ConfigOptions = Partial & { chains: CreateConfigParameters['chains'] projectId: string - enableInjected?: boolean enableEIP6963?: boolean enableCoinbase?: boolean enableEmail?: boolean + /** + * Use enableEIP6963 to show all injected wallets + * @deprecated + */ + enableInjected?: boolean enableWalletConnect?: boolean metadata: { name: string @@ -28,10 +32,10 @@ export function defaultWagmiConfig({ projectId, chains, metadata, - enableInjected, enableCoinbase, enableEmail, enableWalletConnect, + enableEIP6963, ...wagmiConfig }: ConfigOptions): Config { @@ -47,10 +51,6 @@ export function defaultWagmiConfig({ connectors.push(alphaWalletConnect({ projectId, metadata, showQrModal: false })) } - if (enableInjected !== false) { - connectors.push(injected({ shimDisconnect: true })) - } - if (enableCoinbase !== false) { connectors.push( coinbaseWallet({