diff --git a/packages/adapters/ethers/src/tests/client.test.ts b/packages/adapters/ethers/src/tests/client.test.ts index 4e6b1fa64a..8e096c94c6 100644 --- a/packages/adapters/ethers/src/tests/client.test.ts +++ b/packages/adapters/ethers/src/tests/client.test.ts @@ -80,7 +80,8 @@ const mockAuthProvider = { const mockNetworks = [mainnet] const mockCaipNetworks = CaipNetworksUtil.extendCaipNetworks(mockNetworks, { projectId: 'test-project-id', - customNetworkImageUrls: {} + customNetworkImageUrls: {}, + enableCustomRpc: true }) describe('EthersAdapter', () => { diff --git a/packages/adapters/ethers5/src/tests/client.test.ts b/packages/adapters/ethers5/src/tests/client.test.ts index 2787d53785..b21b79f51f 100644 --- a/packages/adapters/ethers5/src/tests/client.test.ts +++ b/packages/adapters/ethers5/src/tests/client.test.ts @@ -72,7 +72,8 @@ const mockAuthProvider = { const mockNetworks = [mainnet] const mockCaipNetworks = CaipNetworksUtil.extendCaipNetworks(mockNetworks, { projectId: 'test-project-id', - customNetworkImageUrls: {} + customNetworkImageUrls: {}, + enableCustomRpc: true }) describe('Ethers5Adapter', () => { diff --git a/packages/adapters/solana/src/tests/client.test.ts b/packages/adapters/solana/src/tests/client.test.ts index daa96e590b..913978d63b 100644 --- a/packages/adapters/solana/src/tests/client.test.ts +++ b/packages/adapters/solana/src/tests/client.test.ts @@ -58,7 +58,8 @@ const mockAuthProvider = { const mockNetworks = [solana] const mockCaipNetworks = CaipNetworksUtil.extendCaipNetworks(mockNetworks, { projectId: 'test-project-id', - customNetworkImageUrls: {} + customNetworkImageUrls: {}, + enableCustomRpc: true }) describe('SolanaAdapter', () => { diff --git a/packages/adapters/wagmi/src/client.ts b/packages/adapters/wagmi/src/client.ts index a20b3fd858..0037ec03ed 100644 --- a/packages/adapters/wagmi/src/client.ts +++ b/packages/adapters/wagmi/src/client.ts @@ -1,51 +1,50 @@ -import type UniversalProvider from '@walletconnect/universal-provider' import type { AppKitNetwork, BaseNetwork, CaipNetwork } from '@reown/appkit-common' -import { AdapterBlueprint } from '@reown/appkit/adapters' import { CoreHelperUtil } from '@reown/appkit-core' +import { AdapterBlueprint } from '@reown/appkit/adapters' import { connect, - disconnect as wagmiDisconnect, createConfig, - type Config, - type CreateConfigParameters, - type CreateConnectorFn, - getConnections, - switchChain, - injected, - type Connector, - watchAccount, - watchConnections, + getAccount, getBalance, - getEnsName, + getConnections, getEnsAvatar, + getEnsName, + injected, + prepareTransactionRequest, + reconnect, signMessage, + switchChain, + disconnect as wagmiDisconnect, estimateGas as wagmiEstimateGas, - sendTransaction as wagmiSendTransaction, getEnsAddress as wagmiGetEnsAddress, + sendTransaction as wagmiSendTransaction, writeContract as wagmiWriteContract, waitForTransactionReceipt, - getAccount, - prepareTransactionRequest, - reconnect, + watchAccount, + watchConnections, + type Config, + type Connector, + type CreateConfigParameters, + type CreateConnectorFn, watchPendingTransactions } from '@wagmi/core' import { type Chain } from '@wagmi/core/chains' +import type UniversalProvider from '@walletconnect/universal-provider' +import { AppKit, WcHelpersUtil, type AppKitOptions } from '@reown/appkit' import { ConstantsUtil as CommonConstantsUtil, isReownName, NetworkUtil } from '@reown/appkit-common' -import { authConnector } from './connectors/AuthConnector.js' -import { AppKit, WcHelpersUtil, type AppKitOptions } from '@reown/appkit' -import { walletConnect } from './connectors/UniversalConnector.js' -import { coinbaseWallet } from '@wagmi/connectors' import { ConstantsUtil as CoreConstantsUtil, type ConnectorType, type Provider } from '@reown/appkit-core' import { CaipNetworksUtil, ConstantsUtil, PresetsUtil } from '@reown/appkit-utils' +import type { W3mFrameProvider } from '@reown/appkit-wallet' +import { coinbaseWallet } from '@wagmi/connectors' import { formatUnits, parseUnits, @@ -53,8 +52,9 @@ import { type Hex, type HttpTransport } from 'viem' -import type { W3mFrameProvider } from '@reown/appkit-wallet' import { normalize } from 'viem/ens' +import { authConnector } from './connectors/AuthConnector.js' +import { walletConnect } from './connectors/UniversalConnector.js' import { parseWalletCapabilities } from './utils/helpers.js' export class WagmiAdapter extends AdapterBlueprint { @@ -66,13 +66,15 @@ export class WagmiAdapter extends AdapterBlueprint { configParams: Partial & { networks: AppKitNetwork[] projectId: string + enableCustomRpc?: boolean } ) { super({ projectId: configParams.projectId, networks: CaipNetworksUtil.extendCaipNetworks(configParams.networks, { projectId: configParams.projectId, - customNetworkImageUrls: {} + customNetworkImageUrls: {}, + enableCustomRpc: configParams.enableCustomRpc }) as [CaipNetwork, ...CaipNetwork[]] }) this.namespace = CommonConstantsUtil.CHAIN.EVM @@ -80,7 +82,8 @@ export class WagmiAdapter extends AdapterBlueprint { ...configParams, networks: CaipNetworksUtil.extendCaipNetworks(configParams.networks, { projectId: configParams.projectId, - customNetworkImageUrls: {} + customNetworkImageUrls: {}, + enableCustomRpc: configParams.enableCustomRpc }) as [CaipNetwork, ...CaipNetwork[]], projectId: configParams.projectId }) @@ -119,11 +122,13 @@ export class WagmiAdapter extends AdapterBlueprint { configParams: Partial & { networks: CaipNetwork[] projectId: string + enableCustomRpc?: boolean } ) { this.caipNetworks = CaipNetworksUtil.extendCaipNetworks(configParams.networks, { projectId: configParams.projectId, - customNetworkImageUrls: {} + customNetworkImageUrls: {}, + enableCustomRpc: configParams.enableCustomRpc }) as [CaipNetwork, ...CaipNetwork[]] this.wagmiChains = this.caipNetworks.filter( diff --git a/packages/adapters/wagmi/src/tests/client.test.ts b/packages/adapters/wagmi/src/tests/client.test.ts index 44bd4936ca..0fc86ccd8b 100644 --- a/packages/adapters/wagmi/src/tests/client.test.ts +++ b/packages/adapters/wagmi/src/tests/client.test.ts @@ -1,25 +1,25 @@ -import { describe, it, expect, beforeEach, vi } from 'vitest' -import { WagmiAdapter } from '../client' +import { CaipNetworksUtil } from '@reown/appkit-utils' import type { Config } from '@wagmi/core' import { - disconnect as wagmiDisconnect, - getConnections, - switchChain, + estimateGas, + getAccount, getBalance, - getEnsName, + getConnections, getEnsAvatar, + getEnsName, signMessage, - estimateGas, - sendTransaction as wagmiSendTransaction, + switchChain, + disconnect as wagmiDisconnect, getEnsAddress as wagmiGetEnsAddress, + sendTransaction as wagmiSendTransaction, writeContract as wagmiWriteContract, waitForTransactionReceipt, - getAccount, watchPendingTransactions } from '@wagmi/core' import { mainnet } from '@wagmi/core/chains' -import { CaipNetworksUtil } from '@reown/appkit-utils' import type UniversalProvider from '@walletconnect/universal-provider' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { WagmiAdapter } from '../client' vi.mock('@wagmi/core', async () => { const actual = await vi.importActual('@wagmi/core') @@ -55,7 +55,8 @@ const mockProjectId = 'test-project-id' const mockNetworks = [mainnet] const mockCaipNetworks = CaipNetworksUtil.extendCaipNetworks(mockNetworks, { projectId: mockProjectId, - customNetworkImageUrls: {} + customNetworkImageUrls: {}, + enableCustomRpc: false }) const mockWagmiConfig = { @@ -95,7 +96,21 @@ describe('WagmiAdapter', () => { expect(adapter.adapterType).toBe('wagmi') expect(adapter.namespace).toBe('eip155') }) - + it('should be set to reown RPC by default', () => { + expect(adapter.wagmiChains?.[0].rpcUrls.default.http[0]).toBe( + `https://rpc.walletconnect.org/v1/?chainId=eip155%3A1&projectId=${mockProjectId}` + ) + }) + it('should be set to customRPC rpc if enableCustomRpc is true', () => { + const adapterWithCustomRpc = new WagmiAdapter({ + networks: mockNetworks, + projectId: mockProjectId, + enableCustomRpc: true + }) + expect(adapterWithCustomRpc.wagmiChains?.[0].rpcUrls.default.http[0]).toBe( + `https://cloudflare-eth.com` + ) + }) it('should not set info property for injected connector', () => { const mockConnectors = [ { diff --git a/packages/appkit-new/src/client.ts b/packages/appkit-new/src/client.ts index 1d0981775b..6d9b4b4674 100644 --- a/packages/appkit-new/src/client.ts +++ b/packages/appkit-new/src/client.ts @@ -1,62 +1,56 @@ /* eslint-disable max-depth */ import { - type EventsControllerState, - type PublicStateControllerState, - type ThemeControllerState, - type ModalControllerState, - type ConnectedWalletInfo, - type RouterControllerState, - type ChainAdapter, - type SdkVersion, - type UseAppKitAccountReturn, - type UseAppKitNetworkReturn, - type NetworkControllerClient, - type ConnectionControllerClient, - ConstantsUtil as CoreConstantsUtil, - type ConnectorType, - type WriteContractArgs, - type Provider, - type SendTransactionArgs, - type EstimateGasTransactionArgs, - type AccountControllerState, - type AdapterNetworkState, - SIWXUtil -} from '@reown/appkit-core' + ConstantsUtil, + SafeLocalStorage, + SafeLocalStorageKeys, + type CaipAddress, + type CaipNetwork, + type CaipNetworkId, + type ChainNamespace +} from '@reown/appkit-common' import { AccountController, + AlertController, + ApiController, + AssetUtil, BlockchainApiController, + ChainController, ConnectionController, ConnectorController, + ConstantsUtil as CoreConstantsUtil, CoreHelperUtil, + EnsController, EventsController, ModalController, - ChainController, + OptionsController, PublicStateController, - ThemeController, - SnackController, RouterController, - EnsController, - OptionsController, - AssetUtil, - ApiController, - AlertController, - StorageUtil + SIWXUtil, + SnackController, + StorageUtil, + ThemeController, + type AccountControllerState, + type AdapterNetworkState, + type ChainAdapter, + type ConnectedWalletInfo, + type ConnectionControllerClient, + type ConnectorType, + type EstimateGasTransactionArgs, + type EventsControllerState, + type ModalControllerState, + type NetworkControllerClient, + type Provider, + type PublicStateControllerState, + type RouterControllerState, + type SdkVersion, + type SendTransactionArgs, + type ThemeControllerState, + type UseAppKitAccountReturn, + type UseAppKitNetworkReturn, + type WriteContractArgs } from '@reown/appkit-core' +import type { AppKitNetwork } from '@reown/appkit-new/networks' import { setColorTheme, setThemeVariables } from '@reown/appkit-ui-new' -import { - ConstantsUtil, - type CaipNetwork, - type ChainNamespace, - SafeLocalStorage, - SafeLocalStorageKeys, - type CaipAddress, - type CaipNetworkId -} from '@reown/appkit-common' -import type { AppKitOptions } from './utils/TypesUtil.js' -import { - UniversalAdapter, - UniversalAdapter as UniversalAdapterClient -} from './universal-adapter/client.js' import { CaipNetworksUtil, ErrorUtil, @@ -68,13 +62,17 @@ import { type W3mFrameProvider, type W3mFrameTypes } from '@reown/appkit-wallet' -import { ProviderUtil } from './store/ProviderUtil.js' -import type { AppKitNetwork } from '@reown/appkit-new/networks' -import type { AdapterBlueprint } from './adapters/ChainAdapterBlueprint.js' -import UniversalProvider from '@walletconnect/universal-provider' import type { SessionTypes } from '@walletconnect/types' import type { UniversalProviderOpts } from '@walletconnect/universal-provider' +import UniversalProvider from '@walletconnect/universal-provider' +import type { AdapterBlueprint } from './adapters/ChainAdapterBlueprint.js' import { W3mFrameProviderSingleton } from './auth-provider/W3MFrameProviderSingleton.js' +import { ProviderUtil } from './store/ProviderUtil.js' +import { + UniversalAdapter, + UniversalAdapter as UniversalAdapterClient +} from './universal-adapter/client.js' +import type { AppKitOptions } from './utils/TypesUtil.js' declare global { interface Window { @@ -685,7 +683,8 @@ export class AppKit { private extendCaipNetworks(options: AppKitOptions) { const extendedNetworks = CaipNetworksUtil.extendCaipNetworks(options.networks, { customNetworkImageUrls: options.chainImages, - projectId: options.projectId + projectId: options.projectId, + enableCustomRpc: options.enableCustomRpc }) return extendedNetworks @@ -696,7 +695,8 @@ export class AppKit { const extendedNetwork = defaultNetwork ? CaipNetworksUtil.extendCaipNetwork(defaultNetwork, { customNetworkImageUrls: options.chainImages, - projectId: options.projectId + projectId: options.projectId, + enableCustomRpc: options.enableCustomRpc }) : undefined diff --git a/packages/appkit-new/src/utils/TypesUtil.ts b/packages/appkit-new/src/utils/TypesUtil.ts index d9ce7c5982..7c3d1c5bd0 100644 --- a/packages/appkit-new/src/utils/TypesUtil.ts +++ b/packages/appkit-new/src/utils/TypesUtil.ts @@ -72,6 +72,8 @@ export type AppKitOptions = { * @see https://cloud.walletconnect.com/ */ metadata?: Metadata + + enableCustomRpc?: boolean } & OptionsControllerState export type AppKitOptionsWithCaipNetworks = Omit & { diff --git a/packages/appkit-utils/src/CaipNetworkUtil.ts b/packages/appkit-utils/src/CaipNetworkUtil.ts index d19e5ad267..57a9e2646d 100644 --- a/packages/appkit-utils/src/CaipNetworkUtil.ts +++ b/packages/appkit-utils/src/CaipNetworkUtil.ts @@ -4,8 +4,8 @@ import { type CaipNetwork, type CaipNetworkId } from '@reown/appkit-common' -import { PresetsUtil } from './PresetsUtil.js' import { fallback, http } from 'viem' +import { PresetsUtil } from './PresetsUtil.js' const RPC_URL_HOST = 'rpc.walletconnect.org' @@ -59,6 +59,7 @@ const WC_HTTP_RPC_SUPPORTED_CHAINS = [ type ExtendCaipNetworkParams = { customNetworkImageUrls: Record | undefined projectId: string + enableCustomRpc: boolean | undefined } export const CaipNetworksUtil = { @@ -109,11 +110,18 @@ export const CaipNetworksUtil = { return `${ConstantsUtil.CHAIN.EVM}:${network.id}` as CaipNetworkId }, - getRpcUrl(caipNetwork: AppKitNetwork, caipNetworkId: CaipNetworkId, projectId: string) { + getRpcUrl( + caipNetwork: AppKitNetwork, + caipNetworkId: CaipNetworkId, + extraParam: { projectId: string; enableCustomRpc?: boolean } + ) { const defaultRpcUrl = caipNetwork.rpcUrls?.default?.http?.[0] - if (WC_HTTP_RPC_SUPPORTED_CHAINS.includes(caipNetworkId)) { - return getBlockchainApiRpcUrl(caipNetworkId, projectId) + if (extraParam.enableCustomRpc === false) { + return getBlockchainApiRpcUrl(caipNetworkId, extraParam.projectId) + } + + return defaultRpcUrl || '' } return defaultRpcUrl || '' @@ -130,11 +138,11 @@ export const CaipNetworksUtil = { */ extendCaipNetwork( caipNetwork: AppKitNetwork, - { customNetworkImageUrls, projectId }: ExtendCaipNetworkParams + { customNetworkImageUrls, projectId, enableCustomRpc = false }: ExtendCaipNetworkParams ): CaipNetwork { const caipNetworkId = this.getCaipNetworkId(caipNetwork) const chainNamespace = this.getChainNamespace(caipNetwork) - const rpcUrl = this.getRpcUrl(caipNetwork, caipNetworkId, projectId) + const rpcUrl = this.getRpcUrl(caipNetwork, caipNetworkId, { projectId, enableCustomRpc }) return { ...caipNetwork, @@ -168,11 +176,12 @@ export const CaipNetworksUtil = { */ extendCaipNetworks( caipNetworks: AppKitNetwork[], - { customNetworkImageUrls, projectId }: ExtendCaipNetworkParams + { customNetworkImageUrls, projectId, enableCustomRpc }: ExtendCaipNetworkParams ) { return caipNetworks.map(caipNetwork => CaipNetworksUtil.extendCaipNetwork(caipNetwork, { customNetworkImageUrls, + enableCustomRpc, projectId }) ) as [CaipNetwork, ...CaipNetwork[]] diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index 5f4e351915..f9fa2d5d52 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -24,9 +24,9 @@ import { SIWXUtil, type ConnectionStatus, type OptionsControllerState, - type WalletFeature, + type SocialProvider, type ConnectMethod, - type SocialProvider + type WalletFeature } from '@reown/appkit-core' import { AccountController, @@ -51,18 +51,13 @@ import { import { setColorTheme, setThemeVariables } from '@reown/appkit-ui' import { ConstantsUtil, - type CaipNetwork, - type ChainNamespace, SafeLocalStorage, SafeLocalStorageKeys, type CaipAddress, - type CaipNetworkId + type CaipNetwork, + type CaipNetworkId, + type ChainNamespace } from '@reown/appkit-common' -import type { AppKitOptions } from './utils/TypesUtil.js' -import { - UniversalAdapter, - UniversalAdapter as UniversalAdapterClient -} from './universal-adapter/client.js' import { CaipNetworksUtil, ErrorUtil, @@ -76,11 +71,16 @@ import { } from '@reown/appkit-wallet' import { ProviderUtil, type ProviderStoreUtilState } from './store/ProviderUtil.js' import type { AppKitNetwork } from '@reown/appkit/networks' -import type { AdapterBlueprint } from './adapters/ChainAdapterBlueprint.js' -import UniversalProvider from '@walletconnect/universal-provider' import type { SessionTypes } from '@walletconnect/types' import type { UniversalProviderOpts } from '@walletconnect/universal-provider' +import UniversalProvider from '@walletconnect/universal-provider' +import type { AdapterBlueprint } from './adapters/ChainAdapterBlueprint.js' import { W3mFrameProviderSingleton } from './auth-provider/W3MFrameProviderSingleton.js' +import { + UniversalAdapter, + UniversalAdapter as UniversalAdapterClient +} from './universal-adapter/client.js' +import type { AppKitOptions } from './utils/TypesUtil.js' declare global { interface Window { @@ -766,7 +766,8 @@ export class AppKit { private extendCaipNetworks(options: AppKitOptions) { const extendedNetworks = CaipNetworksUtil.extendCaipNetworks(options.networks, { customNetworkImageUrls: options.chainImages, - projectId: options.projectId + projectId: options.projectId, + enableCustomRpc: options.enableCustomRpc }) return extendedNetworks @@ -777,7 +778,8 @@ export class AppKit { const extendedNetwork = defaultNetwork ? CaipNetworksUtil.extendCaipNetwork(defaultNetwork, { customNetworkImageUrls: options.chainImages, - projectId: options.projectId + projectId: options.projectId, + enableCustomRpc: options.enableCustomRpc }) : undefined diff --git a/packages/appkit/src/utils/TypesUtil.ts b/packages/appkit/src/utils/TypesUtil.ts index d9ce7c5982..6f9d509511 100644 --- a/packages/appkit/src/utils/TypesUtil.ts +++ b/packages/appkit/src/utils/TypesUtil.ts @@ -72,6 +72,12 @@ export type AppKitOptions = { * @see https://cloud.walletconnect.com/ */ metadata?: Metadata + + /** + * Returns the Chain custom RPC URL instead of Reown's default RPC. + * @default false + */ + enableCustomRpc?: boolean } & OptionsControllerState export type AppKitOptionsWithCaipNetworks = Omit & {