From b0f6da47d1a9da2ac49803ba3d911652a671abf4 Mon Sep 17 00:00:00 2001 From: StephenHeaps <5314553+StephenHeaps@users.noreply.github.com> Date: Mon, 23 Oct 2023 11:05:16 -0400 Subject: [PATCH] Fix #7945: NFTs displayed in Portfolio when grouped by accounts (#8300) Make sure NFTs filtered out when grouping by accounts; resolves NFTs displaying in account groups & possible graph issues (NFT dependent) --- .../Crypto/Stores/PortfolioStore.swift | 14 ++++++++--- .../PortfolioStoreTests.swift | 25 ++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Sources/BraveWallet/Crypto/Stores/PortfolioStore.swift b/Sources/BraveWallet/Crypto/Stores/PortfolioStore.swift index f01e687c903..dc880581eec 100644 --- a/Sources/BraveWallet/Crypto/Stores/PortfolioStore.swift +++ b/Sources/BraveWallet/Crypto/Stores/PortfolioStore.swift @@ -407,7 +407,16 @@ public class PortfolioStore: ObservableObject, WalletObserverStore { let filters = self.filters let selectedAccounts = filters.accounts.filter(\.isSelected).map(\.model) let selectedNetworks = filters.networks.filter(\.isSelected).map(\.model) - let allVisibleUserAssets = assetManager.getAllUserAssetsInNetworkAssetsByVisibility(networks: selectedNetworks, visible: true) + let allVisibleUserAssets: [NetworkAssets] = assetManager.getAllUserAssetsInNetworkAssetsByVisibility( + networks: selectedNetworks, + visible: true + ).map { networkAssets in // filter out NFTs from Portfolio + NetworkAssets( + network: networkAssets.network, + tokens: networkAssets.tokens.filter { !($0.isNft || $0.isErc721) }, + sortOrder: networkAssets.sortOrder + ) + } // update assets on display immediately with empty values. Issue #5567 self.assetGroups = buildAssetGroupViewModels( groupBy: filters.groupBy, @@ -597,7 +606,7 @@ public class PortfolioStore: ObservableObject, WalletObserverStore { switch groupType { case .none: return allVisibleUserAssets.flatMap { networkAssets in - networkAssets.tokens.filter { (!$0.isErc721 && !$0.isNft) }.map { token in + networkAssets.tokens.map { token in AssetViewModel( groupType: groupType, token: token, @@ -629,7 +638,6 @@ public class PortfolioStore: ObservableObject, WalletObserverStore { return [] } return networkAssets.tokens - .filter { (!$0.isErc721 && !$0.isNft) } .map { token in AssetViewModel( groupType: groupType, diff --git a/Tests/BraveWalletTests/PortfolioStoreTests.swift b/Tests/BraveWalletTests/PortfolioStoreTests.swift index 648665219d6..c03ff702636 100644 --- a/Tests/BraveWalletTests/PortfolioStoreTests.swift +++ b/Tests/BraveWalletTests/PortfolioStoreTests.swift @@ -142,7 +142,8 @@ import Preferences let mockEthUserAssets: [BraveWallet.BlockchainToken] = [ .previewToken.copy(asVisibleAsset: true), .previewDaiToken, // Verify non-visible assets not displayed #6386 - .mockUSDCToken.copy(asVisibleAsset: true) + .mockUSDCToken.copy(asVisibleAsset: true), + .mockERC721NFTToken.copy(asVisibleAsset: true) // Verify NFTs not used in Portfolio #7945 ] let ethBalanceWei = formatter.weiString( from: mockETHBalanceAccount1, @@ -232,6 +233,10 @@ import Preferences completion(usdcAccount2BalanceWei, .success, "") } } + rpcService._erc721TokenBalance = { _, _, _, _, completion in + // should not be fetching NFT balance in Portfolio + completion("", .internalError, "Error Message") + } rpcService._solanaBalance = { accountAddress, chainId, completion in // sol balance if accountAddress == self.solAccount1.address { @@ -634,6 +639,12 @@ import Preferences XCTAssertEqual(group.assets[safe: 4]?.quantity, String(format: "%.04f", 0)) // SOL (value = $0, SOL networks hidden) XCTAssertNil(group.assets[safe: 5]) + + // Verify NFTs not used in Portfolio #7945 + let noAssetsAreNFTs = lastUpdatedAssetGroups.flatMap(\.assets).allSatisfy({ + !($0.token.isNft || $0.token.isErc721) + }) + XCTAssertTrue(noAssetsAreNFTs) }.store(in: &cancellables) store.saveFilters(.init( groupBy: store.filters.groupBy, @@ -750,6 +761,12 @@ import Preferences XCTAssertEqual(filAccount2Group.assets[safe: 0]?.token.symbol, BraveWallet.BlockchainToken.mockFilToken.symbol) XCTAssertEqual(filAccount2Group.assets[safe: 0]?.quantity, String(format: "%.04f", 0)) + + // Verify NFTs not used in Portfolio #7945 + let noAssetsAreNFTs = lastUpdatedAssetGroups.flatMap(\.assets).allSatisfy({ + !($0.token.isNft || $0.token.isErc721) + }) + XCTAssertTrue(noAssetsAreNFTs) } .store(in: &cancellables) store.saveFilters(.init( @@ -914,6 +931,12 @@ import Preferences XCTAssertEqual(ethGoerliGroup.assets[safe: 0]?.token.symbol, BraveWallet.BlockchainToken.previewToken.symbol) XCTAssertEqual(ethGoerliGroup.assets[safe: 0]?.quantity, String(format: "%.04f", 0)) + + // Verify NFTs not used in Portfolio #7945 + let noAssetsAreNFTs = lastUpdatedAssetGroups.flatMap(\.assets).allSatisfy({ + !($0.token.isNft || $0.token.isErc721) + }) + XCTAssertTrue(noAssetsAreNFTs) } .store(in: &cancellables) store.saveFilters(.init(