Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #7961: Tokens on Testnets still shown in Edit Visible Assets Scre…
Browse files Browse the repository at this point in the history
…en when testnets are filtered out (#7972)
  • Loading branch information
nuo-xu authored Aug 28, 2023
1 parent 329c1a9 commit 96a6ef8
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 16 deletions.
7 changes: 1 addition & 6 deletions Sources/BraveWallet/Crypto/Stores/NFTStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,7 @@ public class NFTStore: ObservableObject {
.filter { account in
WalletConstants.supportedCoinTypes.contains(account.coin)
}
self.allNetworks = await rpcService.allNetworksForSupportedCoins().filter { network in
if !Preferences.Wallet.showTestNetworks.value { // filter out test networks
return !WalletConstants.supportedTestNetworkChainIds.contains(where: { $0 == network.chainId })
}
return true
}
self.allNetworks = await rpcService.allNetworksForSupportedCoins()
let filters = self.filters
let selectedAccounts = filters.accounts.filter(\.isSelected).map(\.model)
let selectedNetworks = filters.networks.filter(\.isSelected).map(\.model)
Expand Down
7 changes: 1 addition & 6 deletions Sources/BraveWallet/Crypto/Stores/PortfolioStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,7 @@ public class PortfolioStore: ObservableObject {
.filter { account in
WalletConstants.supportedCoinTypes.contains(account.coin)
}
self.allNetworks = await rpcService.allNetworksForSupportedCoins().filter { network in
if !Preferences.Wallet.showTestNetworks.value { // filter out test networks
return !WalletConstants.supportedTestNetworkChainIds.contains(where: { $0 == network.chainId })
}
return true
}
self.allNetworks = await rpcService.allNetworksForSupportedCoins()
let filters = self.filters
let selectedAccounts = filters.accounts.filter(\.isSelected).map(\.model)
let selectedNetworks = filters.networks.filter(\.isSelected).map(\.model)
Expand Down
11 changes: 11 additions & 0 deletions Sources/BraveWallet/Crypto/Stores/UserAssetsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Foundation
import BraveCore
import Combine
import Data
import Preferences

public class AssetStore: ObservableObject, Equatable {
@Published var token: BraveWallet.BlockchainToken
Expand Down Expand Up @@ -83,6 +84,8 @@ public class UserAssetsStore: ObservableObject {
self.ipfsApi = ipfsApi
self.assetManager = userAssetManager
self.keyringService.add(self)

Preferences.Wallet.showTestNetworks.observe(from: self)
}

func update() {
Expand Down Expand Up @@ -294,3 +297,11 @@ extension UserAssetsStore: BraveWalletBraveWalletServiceObserver {

public func onResetWallet() { }
}

extension UserAssetsStore: PreferencesObserver {
public func preferencesDidChange(for key: String) {
if key == Preferences.Wallet.showTestNetworks.key {
networkFilters.removeAll()
}
}
}
13 changes: 10 additions & 3 deletions Sources/BraveWallet/Extensions/RpcServiceExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Foundation
import BraveCore
import BigNumber
import os.log
import Preferences

extension BraveWalletJsonRpcService {
/// Obtain the decimal balance of an `BlockchainToken` for a given account
Expand Down Expand Up @@ -257,8 +258,9 @@ extension BraveWalletJsonRpcService {
return balancesForAsset.reduce(0, +)
}

/// Returns an array of all networks for the supported coin types.
@MainActor func allNetworksForSupportedCoins() async -> [BraveWallet.NetworkInfo] {
/// Returns an array of all networks for the supported coin types. Result will exclude test networks if test networks is set to
/// not shown in Wallet Settings
@MainActor func allNetworksForSupportedCoins(respectTestnetPreference: Bool = true) async -> [BraveWallet.NetworkInfo] {
await withTaskGroup(of: [BraveWallet.NetworkInfo].self) { @MainActor [weak self] group -> [BraveWallet.NetworkInfo] in
guard let self = self else { return [] }
for coinType in WalletConstants.supportedCoinTypes {
Expand All @@ -269,7 +271,12 @@ extension BraveWalletJsonRpcService {
}
}
}
let allChains = await group.reduce([BraveWallet.NetworkInfo](), { $0 + $1 })
let allChains = await group.reduce([BraveWallet.NetworkInfo](), { $0 + $1 }).filter { network in
if !Preferences.Wallet.showTestNetworks.value && respectTestnetPreference { // filter out test networks
return !WalletConstants.supportedTestNetworkChainIds.contains(where: { $0 == network.chainId })
}
return true
}
return allChains.sorted { lhs, rhs in
// sort solana chains to the front of the list
lhs.coin == .sol && rhs.coin != .sol
Expand Down
2 changes: 1 addition & 1 deletion Sources/BraveWallet/WalletUserAssetManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public class WalletUserAssetManager: WalletUserAssetManagerType {
if let coin = coin {
networks = await rpcService.allNetworks(coin)
} else {
networks = await rpcService.allNetworksForSupportedCoins()
networks = await rpcService.allNetworksForSupportedCoins(respectTestnetPreference: false)
}
let networkAssets = await walletService.allUserAssets(in: networks)
for networkAsset in networkAssets {
Expand Down
8 changes: 8 additions & 0 deletions Tests/BraveWalletTests/NetworkStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
import XCTest
import Combine
import BraveCore
import Preferences
@testable import BraveWallet

@MainActor class NetworkStoreTests: XCTestCase {

override func setUp() {
Preferences.Wallet.showTestNetworks.value = true
}
override func tearDown() {
Preferences.Wallet.showTestNetworks.reset()
}

private var cancellables: Set<AnyCancellable> = .init()

private func setupServices() -> (BraveWallet.TestKeyringService, BraveWallet.TestJsonRpcService, BraveWallet.TestBraveWalletService, BraveWallet.TestSwapService) {
Expand Down
8 changes: 8 additions & 0 deletions Tests/BraveWalletTests/SelectAccountTokenStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
import Combine
import XCTest
import BraveCore
import Preferences
@testable import BraveWallet

@MainActor class SelectAccountTokenStoreTests: XCTestCase {

override func setUp() {
Preferences.Wallet.showTestNetworks.value = true
}
override func tearDown() {
Preferences.Wallet.showTestNetworks.reset()
}

private var cancellables: Set<AnyCancellable> = .init()

private let allUserAssets: [BraveWallet.BlockchainToken] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
import Combine
import XCTest
import BraveCore
import Preferences
@testable import BraveWallet

@MainActor class TransactionConfirmationStoreTests: XCTestCase {

override func setUp() {
Preferences.Wallet.showTestNetworks.value = true
}
override func tearDown() {
Preferences.Wallet.showTestNetworks.reset()
}

private var cancellables: Set<AnyCancellable> = .init()

private func setupStore(
Expand Down
8 changes: 8 additions & 0 deletions Tests/BraveWalletTests/TransactionsActivityStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
import Combine
import XCTest
import BraveCore
import Preferences
@testable import BraveWallet

class TransactionsActivityStoreTests: XCTestCase {

override func setUp() {
Preferences.Wallet.showTestNetworks.value = true
}
override func tearDown() {
Preferences.Wallet.showTestNetworks.reset()
}

private var cancellables: Set<AnyCancellable> = .init()

let networks: [BraveWallet.CoinType: [BraveWallet.NetworkInfo]] = [
Expand Down

0 comments on commit 96a6ef8

Please sign in to comment.