diff --git a/.changeset/dull-parents-turn.md b/.changeset/dull-parents-turn.md new file mode 100644 index 0000000000..5f12d6bc35 --- /dev/null +++ b/.changeset/dull-parents-turn.md @@ -0,0 +1,24 @@ +--- +'@reown/appkit-adapter-ethers5': patch +'@reown/appkit-adapter-ethers': patch +'@apps/demo': patch +'@apps/gallery': patch +'@apps/laboratory': patch +'@reown/appkit-adapter-polkadot': patch +'@reown/appkit-adapter-solana': patch +'@reown/appkit-adapter-wagmi': patch +'@reown/appkit': patch +'@reown/appkit-utils': 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-siwx': patch +'@reown/appkit-ui': patch +'@reown/appkit-wallet': patch +--- + +Fixes issue where ethers and ethers5 adapters didn't set the proper caip address when switching accounts manually. \ No newline at end of file diff --git a/packages/adapters/ethers/src/client.ts b/packages/adapters/ethers/src/client.ts index 4aa3e36ce4..a79320a68e 100644 --- a/packages/adapters/ethers/src/client.ts +++ b/packages/adapters/ethers/src/client.ts @@ -368,6 +368,7 @@ export class EthersAdapter { // Common cleanup actions SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID) this.appKit?.resetAccount(this.chainNamespace) + this.removeListeners(provider as Provider) }, signMessage: async (message: string) => { const provider = ProviderUtil.getProvider(this.chainNamespace) @@ -624,7 +625,6 @@ export class EthersAdapter { } const activeConfig = providerConfigs[walletId as unknown as keyof typeof providerConfigs] - if (activeConfig?.provider) { this.setProvider(activeConfig.provider, walletId as ProviderIdType) this.setupProviderListeners(activeConfig.provider, walletId as ProviderIdType) @@ -732,9 +732,13 @@ export class EthersAdapter { } const accountsChangedHandler = (accounts: string[]) => { - const currentAccount = accounts?.[0] as CaipAddress | undefined + const currentAccount = accounts?.[0] as Address | undefined + if (currentAccount) { - this.appKit?.setCaipAddress(currentAccount, this.chainNamespace) + const chainId = this.appKit?.getCaipNetwork()?.id + const caipAddress = `${this.chainNamespace}:${chainId}:${currentAccount}` as CaipAddress + + this.appKit?.setCaipAddress(caipAddress, this.chainNamespace) if (providerId === ConstantsUtil.EIP6963_CONNECTOR_ID) { this.appKit?.setAllAccounts( diff --git a/packages/adapters/ethers/src/tests/client.test.ts b/packages/adapters/ethers/src/tests/client.test.ts index fd65ddc274..7c7d0ea2f9 100644 --- a/packages/adapters/ethers/src/tests/client.test.ts +++ b/packages/adapters/ethers/src/tests/client.test.ts @@ -593,12 +593,14 @@ describe('EthersAdapter', () => { it('should handle accountsChanged event', async () => { client['setupProviderListeners'](mockProvider, 'injected') + const address = '0x1234567890123456789012345678901234567890' const accountsChangedHandler = mockProvider.on.mock.calls.find( (call: string[]) => call[0] === 'accountsChanged' )[1] - await accountsChangedHandler(['0x1234567890123456789012345678901234567890']) + await accountsChangedHandler([address]) expect(mockAppKit.setCaipAddress).toHaveBeenCalled() + expect(mockAppKit.setCaipAddress).toHaveBeenCalledWith(`eip155:1:${address}`, 'eip155') }) it('should handle chainChanged event', async () => { diff --git a/packages/adapters/ethers5/src/client.ts b/packages/adapters/ethers5/src/client.ts index f4088f7568..2e09d7f646 100644 --- a/packages/adapters/ethers5/src/client.ts +++ b/packages/adapters/ethers5/src/client.ts @@ -368,6 +368,7 @@ export class Ethers5Adapter { // Common cleanup actions SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID) this.appKit?.resetAccount(this.chainNamespace) + this.removeListeners(provider as Provider) }, signMessage: async (message: string) => { const provider = ProviderUtil.getProvider(this.chainNamespace) @@ -720,7 +721,10 @@ export class Ethers5Adapter { const accountsChangedHandler = (accounts: string[]) => { const currentAccount = accounts?.[0] as CaipAddress | undefined if (currentAccount) { - this.appKit?.setCaipAddress(currentAccount, this.chainNamespace) + const chainId = this.appKit?.getCaipNetwork()?.id + const caipAddress = `${this.chainNamespace}:${chainId}:${currentAccount}` as CaipAddress + + this.appKit?.setCaipAddress(caipAddress, this.chainNamespace) if (providerId === ConstantsUtil.EIP6963_CONNECTOR_ID) { this.appKit?.setAllAccounts( diff --git a/packages/adapters/ethers5/src/tests/client.test.ts b/packages/adapters/ethers5/src/tests/client.test.ts index a591619ee3..63962cc42b 100644 --- a/packages/adapters/ethers5/src/tests/client.test.ts +++ b/packages/adapters/ethers5/src/tests/client.test.ts @@ -596,12 +596,14 @@ describe('EthersAdapter', () => { it('should handle accountsChanged event', async () => { client['setupProviderListeners'](mockProvider, 'injected') + const address = '0x1234567890123456789012345678901234567890' const accountsChangedHandler = mockProvider.on.mock.calls.find( (call: string[]) => call[0] === 'accountsChanged' )[1] - await accountsChangedHandler(['0x1234567890123456789012345678901234567890']) + await accountsChangedHandler([address]) expect(mockAppKit.setCaipAddress).toHaveBeenCalled() + expect(mockAppKit.setCaipAddress).toHaveBeenCalledWith(`eip155:1:${address}`, 'eip155') }) it('should handle chainChanged event', async () => {