From aaa22cfdb620d44da52466c1cc9270ad88f4c81b Mon Sep 17 00:00:00 2001 From: MK <53529533+magiziz@users.noreply.github.com> Date: Mon, 23 Sep 2024 22:09:57 +0100 Subject: [PATCH] fix: deeplinks not working (#2882) Co-authored-by: tomiir --- .changeset/selfish-books-applaud.md | 38 +++++++++++++++++++ packages/adapters/ethers5/src/client.ts | 1 - packages/appkit/src/client.ts | 10 +---- packages/appkit/src/tests/appkit.test.ts | 11 +----- .../appkit/src/universal-adapter/client.ts | 3 +- packages/common/src/utils/SafeLocalStorage.ts | 8 +++- .../core/src/controllers/AccountController.ts | 13 ------- .../core/src/controllers/ChainController.ts | 2 - packages/core/src/utils/StorageUtil.ts | 6 +-- .../controllers/AccountController.test.ts | 7 ---- .../tests/controllers/ChainController.test.ts | 1 - packages/core/tests/utils/StorageUtil.test.ts | 19 +++++++--- .../views/w3m-select-addresses-view/index.ts | 4 +- 13 files changed, 67 insertions(+), 56 deletions(-) create mode 100644 .changeset/selfish-books-applaud.md diff --git a/.changeset/selfish-books-applaud.md b/.changeset/selfish-books-applaud.md new file mode 100644 index 0000000000..4d77b26373 --- /dev/null +++ b/.changeset/selfish-books-applaud.md @@ -0,0 +1,38 @@ +--- +'@reown/appkit-adapter-ethers5': patch +'@reown/appkit-adapter-solana': patch +'@reown/appkit-scaffold-ui': patch +'@apps/laboratory': patch +'@reown/appkit': patch +'@reown/appkit-common': patch +'@reown/appkit-core': patch +'@apps/demo': patch +'@apps/gallery': patch +'@examples/html-ethers': patch +'@examples/html-ethers5': patch +'@examples/html-wagmi': patch +'@examples/next-ethers': patch +'@examples/next-wagmi': patch +'@examples/react-ethers': patch +'@examples/react-ethers5': patch +'@examples/react-solana': patch +'@examples/react-wagmi': patch +'@examples/vue-ethers5': patch +'@examples/vue-solana': patch +'@examples/vue-wagmi': patch +'@reown/appkit-adapter-ethers': patch +'@reown/appkit-adapter-polkadot': patch +'@reown/appkit-adapter-wagmi': patch +'@reown/appkit-utils': patch +'@reown/appkit-cdn': patch +'@reown/appkit-ethers': patch +'@reown/appkit-ethers5': patch +'@reown/appkit-polyfills': patch +'@reown/appkit-siwe': patch +'@reown/appkit-solana': patch +'@reown/appkit-ui': patch +'@reown/appkit-wagmi': patch +'@reown/appkit-wallet': patch +--- + +Fixed deep link not working after connecting a wallet diff --git a/packages/adapters/ethers5/src/client.ts b/packages/adapters/ethers5/src/client.ts index 2c69e9b2f0..67a55f9300 100644 --- a/packages/adapters/ethers5/src/client.ts +++ b/packages/adapters/ethers5/src/client.ts @@ -829,7 +829,6 @@ export class Ethers5Adapter { return } - this.appKit?.setIsConnected(true, this.chainNamespace) this.appKit?.setPreferredAccountType( preferredAccountType as W3mFrameTypes.AccountType, this.chainNamespace diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index cc5c525ed2..b5f77d2135 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -110,10 +110,6 @@ export class AppKit { return NetworkController.switchActiveNetwork(caipNetwork) } - public getIsConnected() { - return AccountController.state.isConnected - } - public getWalletProvider() { return ChainController.state.activeChain ? ProviderUtil.state.providers[ChainController.state.activeChain] @@ -218,15 +214,11 @@ export class AppKit { ]?.replace } - public setIsConnected: (typeof AccountController)['setIsConnected'] = (isConnected, chain) => { - AccountController.setIsConnected(isConnected, chain) - } - public setStatus: (typeof AccountController)['setStatus'] = (status, chain) => { AccountController.setStatus(status, chain) } - public getIsConnectedState = () => AccountController.state.isConnected + public getIsConnectedState = () => Boolean(ChainController.state.activeCaipAddress) public setAllAccounts: (typeof AccountController)['setAllAccounts'] = (addresses, chain) => { AccountController.setAllAccounts(addresses, chain) diff --git a/packages/appkit/src/tests/appkit.test.ts b/packages/appkit/src/tests/appkit.test.ts index 64e9df1edc..bf560b4384 100644 --- a/packages/appkit/src/tests/appkit.test.ts +++ b/packages/appkit/src/tests/appkit.test.ts @@ -177,21 +177,11 @@ describe('Base', () => { expect(appKit.isTransactionShouldReplaceView()).toBe(true) }) - it('should set is connected', () => { - appKit.setIsConnected(true, 'eip155') - expect(AccountController.setIsConnected).toHaveBeenCalledWith(true, 'eip155') - }) - it('should set status', () => { appKit.setStatus('connected', 'eip155') expect(AccountController.setStatus).toHaveBeenCalledWith('connected', 'eip155') }) - it('should get is connected state', () => { - vi.mocked(AccountController).state = { isConnected: true } as any - expect(appKit.getIsConnectedState()).toBe(true) - }) - it('should set all accounts', () => { const addresses = ['0x123', '0x456'] as unknown as AccountType[] appKit.setAllAccounts(addresses, 'eip155') @@ -239,6 +229,7 @@ describe('Base', () => { it('should set CAIP address', () => { appKit.setCaipAddress('eip155:1:0x123', 'eip155') + expect(appKit.getIsConnectedState()).toBe(true) expect(AccountController.setCaipAddress).toHaveBeenCalledWith('eip155:1:0x123', 'eip155') }) diff --git a/packages/appkit/src/universal-adapter/client.ts b/packages/appkit/src/universal-adapter/client.ts index 8080dbd89e..f8ab685664 100644 --- a/packages/appkit/src/universal-adapter/client.ts +++ b/packages/appkit/src/universal-adapter/client.ts @@ -526,9 +526,10 @@ export class UniversalAdapterClient { this.appKit?.setPreferredAccountType(preferredAccountType, chainNamespace) this.appKit?.setCaipAddress(address, chainNamespace) this.syncConnectedWalletInfo() - this.syncAccounts() await Promise.all([this.appKit?.setApprovedCaipNetworksData(chainNamespace)]) } + + this.syncAccounts() }) } else { this.appKit?.resetWcConnection() diff --git a/packages/common/src/utils/SafeLocalStorage.ts b/packages/common/src/utils/SafeLocalStorage.ts index 03957246fe..19456c7524 100644 --- a/packages/common/src/utils/SafeLocalStorage.ts +++ b/packages/common/src/utils/SafeLocalStorage.ts @@ -8,7 +8,11 @@ export type SafeLocalStorageItems = { '@appkit/connected_social': string '@appkit/connected_social_username': string '@appkit/recent_wallets': string - '@appkit/deeplink_choice': string + /* + * DO NOT CHANGE: @walletconnect/universal-provider requires us to set this specific key + * This value is a stringified version of { href: stiring; name: string } + */ + WALLETCONNECT_DEEPLINK_CHOICE: string } export const SafeLocalStorageKeys = { @@ -21,7 +25,7 @@ export const SafeLocalStorageKeys = { CONNECTED_SOCIAL: '@appkit/connected_social', CONNECTED_SOCIAL_USERNAME: '@appkit/connected_social_username', RECENT_WALLETS: '@appkit/recent_wallets', - DEEPLINK_CHOICE: '@appkit/deeplink_choice' + DEEPLINK_CHOICE: 'WALLETCONNECT_DEEPLINK_CHOICE' } as const export const SafeLocalStorage = { diff --git a/packages/core/src/controllers/AccountController.ts b/packages/core/src/controllers/AccountController.ts index 4385ba89de..485564760c 100644 --- a/packages/core/src/controllers/AccountController.ts +++ b/packages/core/src/controllers/AccountController.ts @@ -19,7 +19,6 @@ import type UniversalProvider from '@walletconnect/universal-provider' // -- Types --------------------------------------------- // export interface AccountControllerState { - isConnected: boolean currentTab: number caipAddress?: CaipAddress address?: string @@ -45,7 +44,6 @@ export interface AccountControllerState { // -- State --------------------------------------------- // const state = proxy({ - isConnected: false, currentTab: 0, tokenBalance: [], smartAccountDeployed: false, @@ -97,21 +95,10 @@ export const AccountController = { ) }, - setIsConnected( - isConnected: AccountControllerState['isConnected'], - chain: ChainNamespace | undefined - ) { - ChainController.setAccountProp('isConnected', isConnected, chain) - }, - setStatus(status: AccountControllerState['status'], chain: ChainNamespace | undefined) { ChainController.setAccountProp('status', status, chain) }, - getChainIsConnected(chain: ChainNamespace | undefined) { - return ChainController.getAccountProp('isConnected', chain) - }, - getCaipAddress(chain: ChainNamespace | undefined) { return ChainController.getAccountProp('caipAddress', chain) }, diff --git a/packages/core/src/controllers/ChainController.ts b/packages/core/src/controllers/ChainController.ts index 58e85c87f3..1bea9b54bd 100644 --- a/packages/core/src/controllers/ChainController.ts +++ b/packages/core/src/controllers/ChainController.ts @@ -39,7 +39,6 @@ type ChainsInitializerAdapter = Pick< // -- Constants ----------------------------------------- // const accountState: AccountControllerState = { - isConnected: false, currentTab: 0, tokenBalance: [], smartAccountDeployed: false, @@ -435,7 +434,6 @@ export const ChainController = { this.setChainAccountData( chainToWrite, ref({ - isConnected: false, smartAccountDeployed: false, currentTab: 0, caipAddress: undefined, diff --git a/packages/core/src/utils/StorageUtil.ts b/packages/core/src/utils/StorageUtil.ts index 7ba4d4e657..1c26bc154c 100644 --- a/packages/core/src/utils/StorageUtil.ts +++ b/packages/core/src/utils/StorageUtil.ts @@ -5,9 +5,9 @@ import { ChainController } from '../controllers/ChainController.js' // -- Utility ----------------------------------------------------------------- export const StorageUtil = { - setWalletConnectDeepLink({ href }: { href: string; name: string }) { + setWalletConnectDeepLink({ name, href }: { href: string; name: string }) { try { - SafeLocalStorage.setItem(SafeLocalStorageKeys.DEEPLINK_CHOICE, href) + SafeLocalStorage.setItem(SafeLocalStorageKeys.DEEPLINK_CHOICE, JSON.stringify({ href, name })) } catch { console.info('Unable to set WalletConnect deep link') } @@ -17,7 +17,7 @@ export const StorageUtil = { try { const deepLink = SafeLocalStorage.getItem(SafeLocalStorageKeys.DEEPLINK_CHOICE) if (deepLink) { - return deepLink + return JSON.parse(deepLink) } } catch { console.info('Unable to get WalletConnect deep link') diff --git a/packages/core/tests/controllers/AccountController.test.ts b/packages/core/tests/controllers/AccountController.test.ts index 33418d26ad..20729f95c7 100644 --- a/packages/core/tests/controllers/AccountController.test.ts +++ b/packages/core/tests/controllers/AccountController.test.ts @@ -24,7 +24,6 @@ beforeAll(() => { describe('AccountController', () => { it('should have valid default state', () => { expect(AccountController.state).toEqual({ - isConnected: false, smartAccountDeployed: false, currentTab: 0, tokenBalance: [], @@ -33,11 +32,6 @@ describe('AccountController', () => { }) }) - it('should update state correctly on setIsConnected()', () => { - AccountController.setIsConnected(true, chain) - expect(AccountController.state.isConnected).toEqual(true) - }) - it('should update state correctly on setCaipAddress()', () => { AccountController.setCaipAddress(caipAddress, chain) expect(AccountController.state.caipAddress).toEqual(caipAddress) @@ -81,7 +75,6 @@ describe('AccountController', () => { it('should update state correctly on resetAccount()', () => { AccountController.resetAccount(chain) expect(AccountController.state).toEqual({ - isConnected: false, smartAccountDeployed: false, currentTab: 0, caipAddress: undefined, diff --git a/packages/core/tests/controllers/ChainController.test.ts b/packages/core/tests/controllers/ChainController.test.ts index 2db41c8d8f..f74ce5d6b1 100644 --- a/packages/core/tests/controllers/ChainController.test.ts +++ b/packages/core/tests/controllers/ChainController.test.ts @@ -61,7 +61,6 @@ describe('ChainController', () => { it('should reset account as expected', () => { ChainController.resetAccount(ChainController.state.activeChain) - expect(ChainController.getAccountProp('isConnected')).toEqual(false) expect(ChainController.getAccountProp('smartAccountDeployed')).toEqual(false) expect(ChainController.getAccountProp('currentTab')).toEqual(0) expect(ChainController.getAccountProp('caipAddress')).toEqual(undefined) diff --git a/packages/core/tests/utils/StorageUtil.test.ts b/packages/core/tests/utils/StorageUtil.test.ts index 30dd3de7f6..9686f0de4c 100644 --- a/packages/core/tests/utils/StorageUtil.test.ts +++ b/packages/core/tests/utils/StorageUtil.test.ts @@ -46,7 +46,7 @@ describe('StorageUtil', () => { const deepLink = { href: 'https://example.com', name: 'Example Wallet' } StorageUtil.setWalletConnectDeepLink(deepLink) const savedDL = SafeLocalStorage.getItem(SafeLocalStorageKeys.DEEPLINK_CHOICE) - expect(savedDL).toBe(deepLink.href) + expect(savedDL).toBe(JSON.stringify({ href: deepLink.href, name: deepLink.name })) }) it('should handle errors when setting deep link', () => { @@ -63,8 +63,14 @@ describe('StorageUtil', () => { describe('getWalletConnectDeepLink', () => { it('should get WalletConnect deep link from localStorage', () => { const deepLink = { href: 'https://example.com', name: 'Example Wallet' } - SafeLocalStorage.setItem('@appkit/deeplink_choice', deepLink.href) - expect(StorageUtil.getWalletConnectDeepLink()).toEqual(deepLink.href) + SafeLocalStorage.setItem( + 'WALLETCONNECT_DEEPLINK_CHOICE', + JSON.stringify({ href: deepLink.href, name: deepLink.name }) + ) + expect(StorageUtil.getWalletConnectDeepLink()).toEqual({ + href: deepLink.href, + name: deepLink.name + }) }) it('should return undefined if deep link is not set', () => { @@ -84,9 +90,12 @@ describe('StorageUtil', () => { describe('deleteWalletConnectDeepLink', () => { it('should delete WalletConnect deep link from localStorage', () => { - SafeLocalStorage.setItem('@appkit/deeplink_choice', 'https://example.com') + SafeLocalStorage.setItem( + 'WALLETCONNECT_DEEPLINK_CHOICE', + JSON.stringify({ href: 'https://example.com', name: 'Example' }) + ) StorageUtil.deleteWalletConnectDeepLink() - expect(SafeLocalStorage.getItem('@appkit/deeplink_choice')).toBeUndefined() + expect(SafeLocalStorage.getItem('WALLETCONNECT_DEEPLINK_CHOICE')).toBeUndefined() }) it('should handle errors when deleting deep link', () => { diff --git a/packages/scaffold-ui/src/views/w3m-select-addresses-view/index.ts b/packages/scaffold-ui/src/views/w3m-select-addresses-view/index.ts index e275c053d2..05daf5f4fa 100644 --- a/packages/scaffold-ui/src/views/w3m-select-addresses-view/index.ts +++ b/packages/scaffold-ui/src/views/w3m-select-addresses-view/index.ts @@ -135,8 +135,8 @@ export class W3mSelectAddressesView extends LitElement { } private async onCancel() { - const { isConnected } = AccountController.state - if (isConnected) { + const { activeCaipAddress } = ChainController.state + if (activeCaipAddress) { await ConnectionController.disconnect() ModalController.close() } else {