From ec255f4afd5df6a6daca89a9530da5e9446e24d0 Mon Sep 17 00:00:00 2001 From: Aaron Cook Date: Thu, 19 Oct 2023 08:51:49 +0200 Subject: [PATCH] fix: multipl session disconnects throwing (#2655) --- .../walletconnect/WalletConnectWallet.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/services/walletconnect/WalletConnectWallet.ts b/src/services/walletconnect/WalletConnectWallet.ts index 23c2c1f5aa..3a27e2d763 100644 --- a/src/services/walletconnect/WalletConnectWallet.ts +++ b/src/services/walletconnect/WalletConnectWallet.ts @@ -141,16 +141,16 @@ class WalletConnectWallet { const newEip155ChainId = getEip155ChainId(chainId) const newEip155Account = `${newEip155ChainId}:${safeAddress}` - const hasNewChainId = !currentEip155ChainIds.includes(newEip155ChainId) - const hasNewAccount = !currentEip155Accounts.includes(newEip155Account) + const isUnsupportedChain = !currentEip155ChainIds.includes(newEip155ChainId) + const isNewSessionSafe = !currentEip155Accounts.includes(newEip155Account) // Switching to unsupported chain - if (hasNewChainId) { + if (isUnsupportedChain) { return this.disconnectSession(session) } - // Add new account to the session namespace - if (hasNewAccount) { + // Add new Safe to the session namespace + if (isNewSessionSafe) { const namespaces: SessionTypes.Namespaces = { [EIP155]: { ...session.namespaces[EIP155], @@ -165,7 +165,7 @@ class WalletConnectWallet { }) } - // Switch to the new account + // Switch to the new Safe await this.accountsChanged(session.topic, chainId, safeAddress) // Switch to the new chain @@ -173,7 +173,11 @@ class WalletConnectWallet { } public async updateSessions(chainId: string, safeAddress: string) { - await Promise.all(this.getActiveSessions().map((session) => this.updateSession(session, chainId, safeAddress))) + // If updating sessions disconnects multiple due to an unsupported chain, + // we need to wait for the previous session to disconnect before the next + for await (const session of this.getActiveSessions()) { + await this.updateSession(session, chainId, safeAddress) + } } public async rejectSession(proposal: Web3WalletTypes.SessionProposal) {