Skip to content

Commit

Permalink
chore: disconnect if syncExistingConnection fails
Browse files Browse the repository at this point in the history
  • Loading branch information
magiziz committed Dec 18, 2024
1 parent e482392 commit 258c818
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
1 change: 1 addition & 0 deletions packages/adapters/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export class EthersAdapter extends AdapterBlueprint {
const { id, chainId } = params

const connector = this.connectors.find(c => c.id === id)

const selectedProvider = connector?.provider as Provider

if (!selectedProvider) {
Expand Down
1 change: 1 addition & 0 deletions packages/adapters/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class WagmiAdapter extends AdapterBlueprint {
params: AdapterBlueprint.GetAccountsParams
): Promise<AdapterBlueprint.GetAccountsResult> {
const connector = this.getWagmiConnector(params.id)

if (!connector) {
return { accounts: [] }
}
Expand Down
77 changes: 44 additions & 33 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1673,48 +1673,59 @@ export class AppKit {
}

private async syncExistingConnection() {
const connectorId = StorageUtil.getConnectedConnectorId()
const activeNamespace = StorageUtil.getActiveNamespace()

if (connectorId === ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT && activeNamespace) {
this.syncWalletConnectAccount()
} else if (connectorId && connectorId !== ConstantsUtil.CONNECTOR_ID.AUTH && activeNamespace) {
this.setStatus('connecting', activeNamespace as ChainNamespace)
const adapter = this.getAdapter(activeNamespace as ChainNamespace)
const res = await adapter?.syncConnection({
id: connectorId,
chainId: this.getCaipNetwork()?.id,
namespace: activeNamespace as ChainNamespace,
rpcUrl: this.getCaipNetwork()?.rpcUrls?.default?.http?.[0] as string
})
try {
const connectorId = StorageUtil.getConnectedConnectorId()
const activeNamespace = StorageUtil.getActiveNamespace()

if (res) {
const accounts = await adapter?.getAccounts({
if (connectorId === ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT && activeNamespace) {
this.syncWalletConnectAccount()
} else if (
connectorId &&
connectorId !== ConstantsUtil.CONNECTOR_ID.AUTH &&
activeNamespace
) {
this.setStatus('connecting', activeNamespace as ChainNamespace)
const adapter = this.getAdapter(activeNamespace as ChainNamespace)
const res = await adapter?.syncConnection({
id: connectorId,
chainId: this.getCaipNetwork()?.id,
namespace: activeNamespace as ChainNamespace,
id: connectorId
rpcUrl: this.getCaipNetwork()?.rpcUrls?.default?.http?.[0] as string
})

if (!accounts || accounts.accounts.length === 0) {
await this.disconnect()
if (res) {
const accounts = await adapter?.getAccounts({
namespace: activeNamespace as ChainNamespace,
id: connectorId
})

return
}
if (!accounts || accounts.accounts.length === 0) {
throw new Error('No accounts found')
}

this.syncProvider({ ...res, chainNamespace: activeNamespace as ChainNamespace })
await this.syncAccount({ ...res, chainNamespace: activeNamespace as ChainNamespace })
this.setAllAccounts(accounts.accounts, activeNamespace as ChainNamespace)
this.setStatus('connected', activeNamespace as ChainNamespace)
} else {
this.setStatus('disconnected', activeNamespace as ChainNamespace)
}
this.syncProvider({ ...res, chainNamespace: activeNamespace as ChainNamespace })
await this.syncAccount({ ...res, chainNamespace: activeNamespace as ChainNamespace })
this.setAllAccounts(accounts.accounts, activeNamespace as ChainNamespace)
this.setStatus('connected', activeNamespace as ChainNamespace)
} else {
this.setStatus('disconnected', activeNamespace as ChainNamespace)
}

if (!this.caipNetworks?.some(network => network.id === res?.chainId)) {
if (res?.chainId) {
this.setUnsupportedNetwork(res.chainId)
if (!this.caipNetworks?.some(network => network.id === res?.chainId)) {
if (res?.chainId) {
this.setUnsupportedNetwork(res.chainId)
}
}
} else if (connectorId !== ConstantsUtil.CONNECTOR_ID.AUTH) {
this.setStatus('disconnected', ChainController.state.activeChain as ChainNamespace)
}
} else if (connectorId !== ConstantsUtil.CONNECTOR_ID.AUTH) {
this.setStatus('disconnected', ChainController.state.activeChain as ChainNamespace)
} catch (err) {
// eslint-disable-next-line no-console
console.log("AppKit:syncExistingConnection - couldn't sync existing connection", err)
this.disconnect().finally(() => {
StorageUtil.deleteConnectedConnectorId()
this.setStatus('disconnected', ChainController.state.activeChain as ChainNamespace)
})
}
}

Expand Down

0 comments on commit 258c818

Please sign in to comment.