From b0abac5e3f800e9193830c62e2429ac648fde33b Mon Sep 17 00:00:00 2001 From: StephenHeaps <5314553+StephenHeaps@users.noreply.github.com> Date: Thu, 21 Sep 2023 14:13:24 -0400 Subject: [PATCH] Fix brave/brave-ios#8115, brave/brave-ios#8103: Ensure selected account after Switch Chain request, after opening Buy from Asset Detail (brave/brave-ios#8116) Ensure selected account in `chainChangedEvent` to handle case where network switches internally in core, for example during a Switch Chain request. --- .../Crypto/Stores/NetworkStore.swift | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Sources/BraveWallet/Crypto/Stores/NetworkStore.swift b/Sources/BraveWallet/Crypto/Stores/NetworkStore.swift index 4cf0b68840e8..a092774961da 100644 --- a/Sources/BraveWallet/Crypto/Stores/NetworkStore.swift +++ b/Sources/BraveWallet/Crypto/Stores/NetworkStore.swift @@ -131,14 +131,9 @@ public class NetworkStore: ObservableObject { } let success = await rpcService.setNetwork(network.chainId, coin: network.coin, origin: isForOrigin ? origin : nil) - if success { - let account = await walletService.ensureSelectedAccount(forChain: network.coin, chainId: network.chainId) - if account == nil { - assertionFailure("Should not have a nil selectedAccount for any network.") - } - if !isForOrigin { // `isSwapSupported` is for the `defaultSelectedChain` - self.isSwapSupported = await swapService.isSwapSupported(network.chainId) - } + if success, + !isForOrigin { // `isSwapSupported` is for the `defaultSelectedChain` + self.isSwapSupported = await swapService.isSwapSupported(network.chainId) } return success ? nil : .unknown } @@ -284,6 +279,13 @@ extension NetworkStore: BraveWalletJsonRpcServiceObserver { } public func chainChangedEvent(_ chainId: String, coin: BraveWallet.CoinType, origin: URLOrigin?) { Task { @MainActor in + // Verify correct account is selected for the new network. + // This could occur from Eth Switch Chain request when Solana account selected. + let accountId = await walletService.ensureSelectedAccount(forChain: coin, chainId: chainId) + if accountId == nil { + assertionFailure("Should not have a nil selectedAccount for any network.") + } + // Sync our local properties with updated values if let origin, origin == self.origin { selectedChainIdForOrigin = chainId } else if origin == nil {