Skip to content

Commit

Permalink
Fix brave/brave-ios#8115, brave/brave-ios#8103: Ensure selected accou…
Browse files Browse the repository at this point in the history
…nt 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.
  • Loading branch information
StephenHeaps committed Sep 21, 2023
1 parent 7a7754b commit b0abac5
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Sources/BraveWallet/Crypto/Stores/NetworkStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b0abac5

Please sign in to comment.