Skip to content

Commit

Permalink
fix deeplink and metadata issue
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvoskamp committed Nov 14, 2024
1 parent e5d5f33 commit 2803e8a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
52 changes: 47 additions & 5 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,12 @@ export class AppKit {
}
) {
this.caipNetworks = this.extendCaipNetworks(options)

this.defaultCaipNetwork = this.extendDefaultCaipNetwork(options)
await this.initControllers(options)
this.createAuthProvider()
await this.createUniversalProvider()
this.createClients()
ChainController.initialize(options.adapters ?? [])
this.defaultCaipNetwork = this.extendDefaultCaipNetwork(options)
this.initControllers(options)
this.chainAdapters = await this.createAdapters(
options.adapters as unknown as AdapterBlueprint[]
)
Expand Down Expand Up @@ -805,11 +804,11 @@ export class AppKit {
})

if (res) {
await this.syncAccount({
this.syncProvider({
...res,
chainNamespace: chain || (ChainController.state.activeChain as ChainNamespace)
})
this.syncProvider({
await this.syncAccount({
...res,
chainNamespace: chain || (ChainController.state.activeChain as ChainNamespace)
})
Expand Down Expand Up @@ -1388,6 +1387,7 @@ export class AppKit {
this.setCaipNetwork(this.caipNetworks?.find(n => n.chainNamespace === chainNamespace))
}

this.syncConnectedWalletInfo(chainNamespace)
const adapter = this.getAdapter(chainNamespace)

const balance = await adapter?.getBalance({
Expand All @@ -1403,6 +1403,48 @@ export class AppKit {
}
}

private syncConnectedWalletInfo(chainNamespace: ChainNamespace) {
const currentActiveWallet = SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR)
const providerType = ProviderUtil.state.providerIds[chainNamespace]

if (
providerType === UtilConstantsUtil.CONNECTOR_TYPE_ANNOUNCED ||
providerType === UtilConstantsUtil.CONNECTOR_TYPE_INJECTED
) {
if (currentActiveWallet) {
const connector = this.getConnectors().find(c => c.id === currentActiveWallet)

if (connector?.info) {
this.setConnectedWalletInfo({ ...connector.info }, chainNamespace)
}
}
} else if (providerType === UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT) {
const provider = ProviderUtil.getProvider(chainNamespace)

if (provider?.session) {
this.setConnectedWalletInfo(
{
...provider.session.peer.metadata,
name: provider.session.peer.metadata.name,
icon: provider.session.peer.metadata.icons?.[0]
},
chainNamespace
)
}
} else if (providerType === UtilConstantsUtil.COINBASE_CONNECTOR_ID) {
const connector = this.getConnectors().find(
c => c.id === UtilConstantsUtil.COINBASE_CONNECTOR_ID
)

this.setConnectedWalletInfo(
{ name: 'Coinbase Wallet', icon: this.getConnectorImage(connector) },
chainNamespace
)
} else if (currentActiveWallet) {
this.setConnectedWalletInfo({ name: currentActiveWallet }, chainNamespace)
}
}

private async syncIdentity({
address,
chainId,
Expand Down
40 changes: 39 additions & 1 deletion packages/appkit/src/tests/appkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import {
ChainController,
type Connector
} from '@reown/appkit-core'
import type { CaipNetwork } from '@reown/appkit-common'
import {
SafeLocalStorage,
SafeLocalStorageKeys,
type CaipNetwork,
type SafeLocalStorageItems
} from '@reown/appkit-common'
import { mockOptions } from './mocks/Options'

// Mock all controllers and UniversalAdapterClient
Expand Down Expand Up @@ -465,5 +470,38 @@ describe('Base', () => {

expect(ChainController.switchActiveNetwork).toHaveBeenCalledTimes(1)
})

it('should set connected wallet info when syncing account', async () => {
// Mock the connector data
const mockConnector = {
id: 'test-wallet'
} as Connector

vi.mocked(ConnectorController.getConnectors).mockReturnValue([mockConnector])

const mockAccountData = {
address: '0x123',
chainId: '1',
chainNamespace: 'eip155' as const
}

vi.spyOn(SafeLocalStorage, 'getItem').mockImplementation(
(key: keyof SafeLocalStorageItems) => {
if (key === SafeLocalStorageKeys.CONNECTED_CONNECTOR) {
return mockConnector.id
}
return undefined
}
)

await appKit['syncAccount'](mockAccountData)

expect(AccountController.setConnectedWalletInfo).toHaveBeenCalledWith(
expect.objectContaining({
name: mockConnector.id
}),
'eip155'
)
})
})
})

0 comments on commit 2803e8a

Please sign in to comment.