Skip to content

Commit

Permalink
Add missing setProvider call (#3048)
Browse files Browse the repository at this point in the history
  • Loading branch information
KannuSingh authored Oct 9, 2024
1 parent e95ecde commit 2751feb
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 48 deletions.
23 changes: 23 additions & 0 deletions .changeset/fair-wombats-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit-utils': patch
'@apps/laboratory': patch
'@reown/appkit': patch
'@apps/demo': patch
'@apps/gallery': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-polkadot': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-cdn': patch
'@reown/appkit-common': patch
'@reown/appkit-core': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
---

fix: add missing setProvider and setProviderId call for connector other than walletconnect on syncAccount
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import {
} from '@reown/appkit-experimental/smart-session'

export function WagmiRequestPermissionsAsyncTest() {
const { address, isConnected } = useAppKitAccount()
const { address, isConnected, status } = useAppKitAccount()

const { chainId } = useAppKitNetwork()
const isSupported = useMemo(() => isSmartSessionSupported(), [address])
const isSupported = useMemo(() => isSmartSessionSupported(), [status])

if (!isConnected || !address || !chainId) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
} from '@reown/appkit-experimental/smart-session'

export function WagmiRequestPermissionsSyncTest() {
const { address, isConnected } = useAppKitAccount()
const { address, isConnected, status } = useAppKitAccount()
const { chainId } = useAppKitNetwork()
const isSupported = useMemo(() => isSmartSessionSupported(), [address])
const isSupported = useMemo(() => isSmartSessionSupported(), [status])

if (!isConnected || !address || !chainId) {
return (
Expand Down
15 changes: 7 additions & 8 deletions packages/adapters/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
EthersHelpersUtil,
type Provider,
type ProviderType,
type ProviderId,
type Address
} from '@reown/appkit-utils/ethers'
import type { AppKit } from '@reown/appkit'
Expand All @@ -45,7 +44,7 @@ import { WcConstantsUtil } from '@reown/appkit'
import { EthersMethods } from './utils/EthersMethods.js'
import { formatEther, InfuraProvider, JsonRpcProvider } from 'ethers'
import type { PublicStateControllerState } from '@reown/appkit-core'
import { ProviderUtil } from '@reown/appkit/store'
import { ProviderUtil, type ProviderIdType } from '@reown/appkit/store'
import { CoinbaseWalletSDK, type ProviderInterface } from '@coinbase/wallet-sdk'
import { W3mFrameProviderSingleton } from '@reown/appkit/auth-provider'

Expand Down Expand Up @@ -301,7 +300,7 @@ export class EthersAdapter {
}
await this.setProvider(
selectedProvider,
selectedConnector.providerType as ProviderId,
selectedConnector.providerType as ProviderIdType,
info?.name
)
} catch (error) {
Expand Down Expand Up @@ -613,12 +612,12 @@ export class EthersAdapter {
const activeConfig = providerConfigs[walletId as unknown as keyof typeof providerConfigs]

if (activeConfig?.provider) {
this.setProvider(activeConfig.provider, walletId as ProviderId)
this.setupProviderListeners(activeConfig.provider, walletId as ProviderId)
this.setProvider(activeConfig.provider, walletId as ProviderIdType)
this.setupProviderListeners(activeConfig.provider, walletId as ProviderIdType)
}
}

private async setProvider(provider: Provider, providerId: ProviderId, name?: string) {
private async setProvider(provider: Provider, providerId: ProviderIdType, name?: string) {
if (providerId === 'w3mAuth') {
this.setAuthProvider()
} else {
Expand Down Expand Up @@ -685,7 +684,7 @@ export class EthersAdapter {
)
this.appKit?.setSmartAccountDeployed(Boolean(smartAccountDeployed), this.chainNamespace)
ProviderUtil.setProvider<Provider>('eip155', this.authProvider as unknown as Provider)
ProviderUtil.setProviderId('eip155', ConstantsUtil.AUTH_CONNECTOR_ID as ProviderId)
ProviderUtil.setProviderId('eip155', ConstantsUtil.AUTH_CONNECTOR_ID as ProviderIdType)
this.setupProviderListeners(this.authProvider as unknown as Provider, 'w3mAuth')
this.watchModal()
}
Expand All @@ -703,7 +702,7 @@ export class EthersAdapter {
}
}

private setupProviderListeners(provider: Provider, providerId: ProviderId) {
private setupProviderListeners(provider: Provider, providerId: ProviderIdType) {
const disconnectHandler = () => {
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID)
this.removeListeners(provider)
Expand Down
10 changes: 5 additions & 5 deletions packages/adapters/ethers/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { mockOptions } from './mocks/Options'
import { mockCreateEthersConfig } from './mocks/EthersConfig'
import mockAppKit from './mocks/AppKit'
import { mockAuthConnector } from './mocks/AuthConnector'
import { EthersHelpersUtil, type ProviderId, type ProviderType } from '@reown/appkit-utils/ethers'
import { EthersHelpersUtil, type ProviderType } from '@reown/appkit-utils/ethers'
import { CaipNetworksUtil, ConstantsUtil } from '@reown/appkit-utils'
import {
arbitrum as AppkitArbitrum,
Expand All @@ -14,7 +14,7 @@ import {
optimism as AppkitOptimism,
bsc as AppkitBsc
} from '@reown/appkit/networks'
import { ProviderUtil } from '@reown/appkit/store'
import { ProviderUtil, type ProviderIdType } from '@reown/appkit/store'
import { SafeLocalStorage, SafeLocalStorageKeys } from '@reown/appkit-common'
import { type BlockchainApiLookupEnsName } from '@reown/appkit'
import { InfuraProvider, JsonRpcProvider } from 'ethers'
Expand Down Expand Up @@ -813,7 +813,7 @@ describe('EthersAdapter', () => {
vi.spyOn(ProviderUtil.state, 'providerIds', 'get').mockReturnValue({
eip155: ConstantsUtil.EIP6963_CONNECTOR_ID,
solana: undefined
} as Record<ChainNamespace, ProviderId | undefined>)
} as Record<ChainNamespace, ProviderIdType | undefined>)
client['EIP6963Providers'] = [
{
info: { name: 'MetaMask', icon: 'icon-url', uuid: 'test-uuid', rdns: 'com.metamask' },
Expand All @@ -833,7 +833,7 @@ describe('EthersAdapter', () => {
vi.spyOn(ProviderUtil.state, 'providerIds', 'get').mockReturnValue({
eip155: ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID,
solana: undefined
} as Record<ChainNamespace, ProviderId | undefined>)
} as Record<ChainNamespace, ProviderIdType | undefined>)
const mockProvider = {
session: {
peer: {
Expand Down Expand Up @@ -862,7 +862,7 @@ describe('EthersAdapter', () => {
vi.spyOn(ProviderUtil.state, 'providerIds', 'get').mockReturnValue({
eip155: ConstantsUtil.COINBASE_SDK_CONNECTOR_ID,
solana: undefined
} as Record<ChainNamespace, ProviderId | undefined>)
} as Record<ChainNamespace, ProviderIdType | undefined>)
vi.spyOn(mockAppKit, 'getConnectors').mockReturnValue([
{
id: ConstantsUtil.COINBASE_SDK_CONNECTOR_ID,
Expand Down
15 changes: 7 additions & 8 deletions packages/adapters/ethers5/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
EthersHelpersUtil,
type Provider,
type ProviderType,
type ProviderId,
type Address
} from '@reown/appkit-utils/ethers'
import type { AppKit } from '@reown/appkit'
Expand All @@ -45,7 +44,7 @@ import { WcConstantsUtil } from '@reown/appkit'
import { Ethers5Methods } from './utils/Ethers5Methods.js'
import { ethers } from 'ethers'
import type { PublicStateControllerState } from '@reown/appkit-core'
import { ProviderUtil } from '@reown/appkit/store'
import { ProviderUtil, type ProviderIdType } from '@reown/appkit/store'
import { CoinbaseWalletSDK, type ProviderInterface } from '@coinbase/wallet-sdk'
import { W3mFrameProviderSingleton } from '@reown/appkit/auth-provider'

Expand Down Expand Up @@ -301,7 +300,7 @@ export class Ethers5Adapter {
}
await this.setProvider(
selectedProvider,
selectedConnector.providerType as ProviderId,
selectedConnector.providerType as ProviderIdType,
info?.name
)
} catch (error) {
Expand Down Expand Up @@ -589,12 +588,12 @@ export class Ethers5Adapter {
const activeConfig = providerConfigs[walletId as unknown as keyof typeof providerConfigs]

if (activeConfig?.provider) {
this.setProvider(activeConfig.provider, walletId as ProviderId)
this.setupProviderListeners(activeConfig.provider, walletId as ProviderId)
this.setProvider(activeConfig.provider, walletId as ProviderIdType)
this.setupProviderListeners(activeConfig.provider, walletId as ProviderIdType)
}
}

private async setProvider(provider: Provider, providerId: ProviderId, name?: string) {
private async setProvider(provider: Provider, providerId: ProviderIdType, name?: string) {
if (providerId === 'w3mAuth') {
this.setAuthProvider()
} else {
Expand Down Expand Up @@ -661,7 +660,7 @@ export class Ethers5Adapter {
)
this.appKit?.setSmartAccountDeployed(Boolean(smartAccountDeployed), this.chainNamespace)
ProviderUtil.setProvider<Provider>('eip155', this.authProvider as unknown as Provider)
ProviderUtil.setProviderId('eip155', ConstantsUtil.AUTH_CONNECTOR_ID as ProviderId)
ProviderUtil.setProviderId('eip155', ConstantsUtil.AUTH_CONNECTOR_ID as ProviderIdType)
this.setupProviderListeners(this.authProvider as unknown as Provider, 'w3mAuth')
this.watchModal()
}
Expand All @@ -679,7 +678,7 @@ export class Ethers5Adapter {
}
}

private setupProviderListeners(provider: Provider, providerId: ProviderId) {
private setupProviderListeners(provider: Provider, providerId: ProviderIdType) {
const disconnectHandler = () => {
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID)
this.removeListeners(provider)
Expand Down
10 changes: 5 additions & 5 deletions packages/adapters/ethers5/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { mockOptions } from './mocks/Options'
import { mockCreateEthersConfig } from './mocks/EthersConfig'
import mockAppKit from './mocks/AppKit'
import { mockAuthConnector } from './mocks/AuthConnector'
import { EthersHelpersUtil, type ProviderId, type ProviderType } from '@reown/appkit-utils/ethers'
import { EthersHelpersUtil, type ProviderType } from '@reown/appkit-utils/ethers'
import { CaipNetworksUtil, ConstantsUtil } from '@reown/appkit-utils'
import {
arbitrum as AppkitArbitrum,
Expand All @@ -14,7 +14,7 @@ import {
optimism as AppkitOptimism,
bsc as AppkitBsc
} from '@reown/appkit/networks'
import { ProviderUtil } from '@reown/appkit/store'
import { ProviderUtil, type ProviderIdType } from '@reown/appkit/store'
import { SafeLocalStorage, SafeLocalStorageKeys } from '@reown/appkit-common'
import { type BlockchainApiLookupEnsName } from '@reown/appkit'
import { ethers } from 'ethers'
Expand Down Expand Up @@ -824,7 +824,7 @@ describe('EthersAdapter', () => {
vi.spyOn(ProviderUtil.state, 'providerIds', 'get').mockReturnValue({
eip155: ConstantsUtil.EIP6963_CONNECTOR_ID,
solana: undefined
} as Record<ChainNamespace, ProviderId | undefined>)
} as Record<ChainNamespace, ProviderIdType | undefined>)
client['EIP6963Providers'] = [
{
info: { name: 'MetaMask', icon: 'icon-url', uuid: 'test-uuid', rdns: 'com.metamask' },
Expand All @@ -844,7 +844,7 @@ describe('EthersAdapter', () => {
vi.spyOn(ProviderUtil.state, 'providerIds', 'get').mockReturnValue({
eip155: ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID,
solana: undefined
} as Record<ChainNamespace, ProviderId | undefined>)
} as Record<ChainNamespace, ProviderIdType | undefined>)
const mockProvider = {
session: {
peer: {
Expand Down Expand Up @@ -873,7 +873,7 @@ describe('EthersAdapter', () => {
vi.spyOn(ProviderUtil.state, 'providerIds', 'get').mockReturnValue({
eip155: ConstantsUtil.COINBASE_SDK_CONNECTOR_ID,
solana: undefined
} as Record<ChainNamespace, ProviderId | undefined>)
} as Record<ChainNamespace, ProviderIdType | undefined>)
vi.spyOn(mockAppKit, 'getConnectors').mockReturnValue([
{
id: ConstantsUtil.COINBASE_SDK_CONNECTOR_ID,
Expand Down
13 changes: 6 additions & 7 deletions packages/adapters/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import type { AppKit } from '@reown/appkit'
import { walletConnect } from './connectors/UniversalConnector.js'
import { coinbaseWallet } from '@wagmi/connectors'
import { authConnector } from './connectors/AuthConnector.js'
import { ProviderUtil } from '@reown/appkit/store'
import { ProviderUtil, type ProviderIdType } from '@reown/appkit/store'

// -- Types ---------------------------------------------------------------------
export interface AdapterOptions<C extends Config>
Expand Down Expand Up @@ -682,7 +682,7 @@ export class WagmiAdapter implements ChainAdapter {

if (this.wagmiConfig) {
if (connector) {
if (connector && connector.name === 'WalletConnect' && connector.getProvider && address) {
if (connector.name === 'WalletConnect' && connector.getProvider && address) {
const activeCaipNetwork = this.appKit?.getCaipNetwork()
const currentChainId = chainId || (activeCaipNetwork?.id as number | undefined)
const provider = (await connector.getProvider()) as UniversalProvider
Expand Down Expand Up @@ -715,19 +715,18 @@ export class WagmiAdapter implements ChainAdapter {
])
}
} else if (status === 'connected' && address && chainId) {
ProviderUtil.setProvider(this.chainNamespace, await connector.getProvider())
ProviderUtil.setProviderId(this.chainNamespace, connector.id as ProviderIdType)
const caipAddress = `eip155:${chainId}:${address}` as CaipAddress
this.syncNetwork(address, chainId, true)
this.appKit?.setCaipAddress(caipAddress, this.chainNamespace)
await Promise.all([
this.syncProfile(address, chainId),
this.syncBalance(address, chainId),
this.syncConnectedWalletInfo(connector),
this.appKit?.setApprovedCaipNetworksData(this.chainNamespace)
])
if (connector) {
this.syncConnectedWalletInfo(connector)
}

this.appKit?.setCaipAddress(caipAddress, this.chainNamespace)
this.appKit?.setStatus('connected', this.chainNamespace)
// Set by authConnector.onIsConnectedHandler as we need the account type
if (!isAuthConnector && addresses?.length) {
this.appKit?.setAllAccounts(
Expand Down
6 changes: 5 additions & 1 deletion packages/adapters/wagmi/src/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ describe('Wagmi Client', () => {
it('should sync account correctly when connected', async () => {
const mockAddress = '0x1234567890123456789012345678901234567890'
const mockChainId = 1
const mockConnector = { id: 'mockConnector', name: 'Mock Connector' }
const mockConnector = {
id: 'mockConnector',
name: 'Mock Connector',
getProvider: vi.fn().mockResolvedValue({})
}

const setCaipAddressSpy = vi.spyOn(mockAppKit, 'setCaipAddress')

Expand Down
8 changes: 0 additions & 8 deletions packages/appkit-utils/src/ethers/EthersTypesUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,3 @@ export type Chain = {
chain: string
imageId: string | undefined
}

export type ProviderId =
| 'walletConnect'
| 'injected'
| 'coinbaseWallet'
| 'eip6963'
| 'w3mAuth'
| 'coinbaseWalletSDK'
2 changes: 1 addition & 1 deletion packages/appkit/src/store/ProviderUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ProviderStoreUtilState {
providerIds: Record<ChainNamespace, ProviderIdType | undefined>
}

type ProviderIdType =
export type ProviderIdType =
| 'walletConnect'
| 'injected'
| 'coinbaseWallet'
Expand Down
2 changes: 1 addition & 1 deletion packages/appkit/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { ProviderUtil } from './ProviderUtil.js'
export type { ProviderStoreUtilState } from './ProviderUtil.js'
export type { ProviderStoreUtilState, ProviderIdType } from './ProviderUtil.js'

0 comments on commit 2751feb

Please sign in to comment.