Skip to content

Commit

Permalink
fix: sync all available connections
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir committed Dec 11, 2024
1 parent 590b357 commit 49766a7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
62 changes: 38 additions & 24 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1478,15 +1478,19 @@ export class AppKit {
type,
provider,
id,
chainNamespace
chainNamespace,
active = true
}: Pick<AdapterBlueprint.ConnectResult, 'type' | 'provider' | 'id'> & {
chainNamespace: ChainNamespace
active?: boolean
}) {
ProviderUtil.setProviderId(chainNamespace, type)
ProviderUtil.setProvider(chainNamespace, provider)

StorageUtil.setConnectedConnector(id as ConnectorType, chainNamespace)
StorageUtil.setConnectedNamespace(chainNamespace as ChainNamespace)
if (active) {
StorageUtil.setActiveNamespace(chainNamespace as ChainNamespace)
}
}

private async syncAccount({
Expand All @@ -1508,8 +1512,9 @@ export class AppKit {

this.setStatus('connected', chainNamespace)

if (chainIdToUse && chainNamespace === activeNamespace) {
if (chainIdToUse && chainNamespace === activeNamespace && chainIdToUse === activeChainId) {
const caipNetwork = this.caipNetworks?.find(n => n.id.toString() === chainIdToUse.toString())
console.log('>> Sync Account', caipNetwork)
const fallBackCaipNetwork = this.caipNetworks?.find(n => n.chainNamespace === chainNamespace)
this.setCaipNetwork(caipNetwork || fallBackCaipNetwork)
this.syncConnectedWalletInfo(chainNamespace)
Expand Down Expand Up @@ -1661,7 +1666,12 @@ export class AppKit {
)
}

private async syncExternalAccount(params: { namespace: ChainNamespace; connectorId: string }) {
private async syncExternalAccount(params: {
namespace: ChainNamespace
connectorId: string
active?: boolean
}) {
console.log('>> Sync External Account', params.namespace)
this.setStatus('connecting', params.namespace)
const adapter = this.getAdapter(params.namespace)
const res = await adapter?.syncConnection({
Expand All @@ -1672,7 +1682,9 @@ export class AppKit {
})

if (res?.address) {
this.syncProvider({ ...res, chainNamespace: params.namespace })
console.log('>> Sync External Account Success', res)
this.syncProvider({ ...res, chainNamespace: params.namespace, active: params.active })

await this.syncAccount({ ...res, chainNamespace: params.namespace })
const accounts = await adapter?.getAccounts({
namespace: params.namespace,
Expand All @@ -1684,7 +1696,7 @@ export class AppKit {
this.setStatus('disconnected', params.namespace)
}

if (!this.caipNetworks?.some(network => network.id === res?.chainId)) {
if (!this.caipNetworks?.some(network => String(network.id) === String(res?.chainId))) {
if (res?.chainId) {
this.setUnsupportedNetwork(res.chainId)
}
Expand All @@ -1693,24 +1705,26 @@ export class AppKit {

private async syncExistingConnection() {

Check failure on line 1706 in packages/appkit/src/client.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Unexpected console statement
const connectedNamespace = StorageUtil.getActiveNamespace()

if (connectedNamespace) {
const connectedConnector = StorageUtil.getConnectedConnector(connectedNamespace)
if (!connectedConnector) {
this.setStatus('disconnected', connectedNamespace as ChainNamespace)

return
}

if (connectedConnector === UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT) {
await this.syncWalletConnectAccount()
} else if (connectedConnector !== UtilConstantsUtil.CONNECTOR_TYPE_W3M_AUTH) {
await this.syncExternalAccount({
namespace: connectedNamespace,
connectorId: connectedConnector
})
}
}
/*
* For all namespaces, check if there is a connected connector
* If there is, sync the connection but only update state if the chainId matches the active chain
*/
await Promise.all(
this.chainNamespaces.map(async chainNamespace => {
const connectedConnector = StorageUtil.getConnectedConnector(chainNamespace)
if (connectedConnector) {
if (connectedConnector === UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT) {
await this.syncWalletConnectAccount()
} else if (connectedConnector !== UtilConstantsUtil.CONNECTOR_TYPE_W3M_AUTH) {
await this.syncExternalAccount({
namespace: chainNamespace,
connectorId: connectedConnector,
active: chainNamespace === connectedNamespace
})
}
}
})
)
}

private getAdapter(namespace: ChainNamespace) {
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/controllers/ChainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export const ChainController = {
StorageUtil.setActiveCaipNetworkId(caipNetwork.caipNetworkId)

const isSupported = this.checkIfSupportedNetwork(caipNetwork.chainNamespace)
console.trace('>> Active caip network set', caipNetwork, isSupported)

if (!isSupported) {
this.showUnsupportedChainUI()
Expand Down Expand Up @@ -419,11 +420,18 @@ export const ChainController = {
const activeCaipNetwork = this.state.activeCaipNetwork
const requestedCaipNetworks = this.getRequestedCaipNetworks(namespace)

console.log('>> Check if supported network', {
activeCaipNetwork,
requestedCaipNetworks
})

if (!requestedCaipNetworks.length) {
return true
}

return requestedCaipNetworks?.some(network => network.id === activeCaipNetwork?.id)
return requestedCaipNetworks?.some(
network => String(network.id) === String(activeCaipNetwork?.id)
)
},

checkIfSupportedChainId(chainId: number | string) {
Expand Down
8 changes: 8 additions & 0 deletions packages/scaffold-ui-new/src/views/w3m-networks-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ export class W3mNetworksView extends LitElement {
const isCurrentNetworkConnected = AccountController.state.caipAddress
const isAuthConnected = StorageUtil.getConnectedConnector(network.chainNamespace) === 'ID_AUTH'

console.log('>> Should switch network', {
namespace: network.chainNamespace,
isDifferentNamespace,
isNewNetworkConnected,
isCurrentNetworkConnected,
isAuthConnected
})

if (
isDifferentNamespace &&
isCurrentNetworkConnected &&
Expand Down
9 changes: 9 additions & 0 deletions packages/scaffold-ui/src/views/w3m-networks-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ export class W3mNetworksView extends LitElement {
const isCurrentNetworkConnected = AccountController.state.caipAddress
const isAuthConnected = StorageUtil.getConnectedConnector(network.chainNamespace) === 'ID_AUTH'

console.log('>> Should switch network', {
namespace: network.chainNamespace,
isDifferentNamespace,
isNewNetworkConnected,
isCurrentNetworkConnected,
isAuthConnected,
chainState: ChainController.state
})

if (
isDifferentNamespace &&
isCurrentNetworkConnected &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class W3mSwitchActiveChainView extends LitElement {

// -- Render -------------------------------------------- //
public override render() {
console.log('>> wat')
const switchedChainNameString = this.switchToChain
? ConstantsUtil.CHAIN_NAME_MAP[this.switchToChain]
: 'supported'
Expand Down

0 comments on commit 49766a7

Please sign in to comment.