Skip to content

Commit

Permalink
fix: deeplinks not working (#2882)
Browse files Browse the repository at this point in the history
Co-authored-by: tomiir <[email protected]>
  • Loading branch information
magiziz and tomiir authored Sep 23, 2024
1 parent b35af73 commit aaa22cf
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 56 deletions.
38 changes: 38 additions & 0 deletions .changeset/selfish-books-applaud.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion packages/adapters/ethers5/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,6 @@ export class Ethers5Adapter {
return
}

this.appKit?.setIsConnected(true, this.chainNamespace)
this.appKit?.setPreferredAccountType(
preferredAccountType as W3mFrameTypes.AccountType,
this.chainNamespace
Expand Down
10 changes: 1 addition & 9 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 1 addition & 10 deletions packages/appkit/src/tests/appkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
})

Expand Down
3 changes: 2 additions & 1 deletion packages/appkit/src/universal-adapter/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 6 additions & 2 deletions packages/common/src/utils/SafeLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down
13 changes: 0 additions & 13 deletions packages/core/src/controllers/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type UniversalProvider from '@walletconnect/universal-provider'

// -- Types --------------------------------------------- //
export interface AccountControllerState {
isConnected: boolean
currentTab: number
caipAddress?: CaipAddress
address?: string
Expand All @@ -45,7 +44,6 @@ export interface AccountControllerState {

// -- State --------------------------------------------- //
const state = proxy<AccountControllerState>({
isConnected: false,
currentTab: 0,
tokenBalance: [],
smartAccountDeployed: false,
Expand Down Expand Up @@ -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)
},
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/controllers/ChainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type ChainsInitializerAdapter = Pick<

// -- Constants ----------------------------------------- //
const accountState: AccountControllerState = {
isConnected: false,
currentTab: 0,
tokenBalance: [],
smartAccountDeployed: false,
Expand Down Expand Up @@ -435,7 +434,6 @@ export const ChainController = {
this.setChainAccountData(
chainToWrite,
ref({
isConnected: false,
smartAccountDeployed: false,
currentTab: 0,
caipAddress: undefined,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/utils/StorageUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -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')
Expand Down
7 changes: 0 additions & 7 deletions packages/core/tests/controllers/AccountController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ beforeAll(() => {
describe('AccountController', () => {
it('should have valid default state', () => {
expect(AccountController.state).toEqual({
isConnected: false,
smartAccountDeployed: false,
currentTab: 0,
tokenBalance: [],
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion packages/core/tests/controllers/ChainController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 14 additions & 5 deletions packages/core/tests/utils/StorageUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit aaa22cf

Please sign in to comment.