This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 440
/
WalletUserAssetManager.swift
249 lines (221 loc) · 10.6 KB
/
WalletUserAssetManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
import Foundation
import Data
import BraveCore
import Preferences
import CoreData
public protocol WalletUserAssetManagerType: AnyObject {
/// Return all visible or all invisible user assets in form of `NetworkAssets`
func getAllUserAssetsInNetworkAssetsByVisibility(networks: [BraveWallet.NetworkInfo], visible: Bool) -> [NetworkAssets]
/// Return all user assets in form of `NetworkAssets`
func getAllUserAssetsInNetworkAssets(networks: [BraveWallet.NetworkInfo], includingUserDeleted: Bool) -> [NetworkAssets]
/// Return all spam or non-spam user assets in form of `NetworkAssets`
func getAllUserNFTs(networks: [BraveWallet.NetworkInfo], isSpam: Bool) -> [NetworkAssets]
/// Return all user marked deleted user assets
func getAllUserDeletedNFTs() -> [WalletUserAsset]
/// Return a `WalletUserAsset` with a given `BraveWallet.BlockchainToken`
func getUserAsset(_ asset: BraveWallet.BlockchainToken) -> WalletUserAsset?
/// Add a `WalletUserAsset` representation of the given
/// `BraveWallet.BlockchainToken` to CoreData
func addUserAsset(_ asset: BraveWallet.BlockchainToken, completion: (() -> Void)?)
/// Remove a `WalletUserAsset` representation of the given
/// `BraveWallet.BlockchainToken` from CoreData
func removeUserAsset(_ asset: BraveWallet.BlockchainToken, completion: (() -> Void)?)
/// Remove an entire `WalletUserAssetGroup` with a given `groupId`
func removeGroup(for groupId: String, completion: (() -> Void)?)
/// Update a `WalletUserAsset`'s `visible`, `isSpam`, and `isDeletedByUser` status
func updateUserAsset(for asset: BraveWallet.BlockchainToken, visible: Bool, isSpam: Bool, isDeletedByUser: Bool, completion: (() -> Void)?)
}
public class WalletUserAssetManager: WalletUserAssetManagerType {
private let rpcService: BraveWalletJsonRpcService
private let walletService: BraveWalletBraveWalletService
public init(
rpcService: BraveWalletJsonRpcService,
walletService: BraveWalletBraveWalletService
) {
self.rpcService = rpcService
self.walletService = walletService
}
/// Return all user's assets stored in CoreData
public func getAllUserAssetsInNetworkAssets(networks: [BraveWallet.NetworkInfo], includingUserDeleted: Bool) -> [NetworkAssets] {
var allUserAssets: [NetworkAssets] = []
for (index, network) in networks.enumerated() {
let groupId = network.walletUserAssetGroupId
if let walletUserAssets = WalletUserAssetGroup.getGroup(groupId: groupId)?.walletUserAssets?.filter({
if !includingUserDeleted {
return $0.isDeletedByUser == false
}
return true
}) {
let networkAsset = NetworkAssets(
network: network,
tokens: walletUserAssets.map(\.blockchainToken),
sortOrder: index
)
allUserAssets.append(networkAsset)
}
}
return allUserAssets.sorted(by: { $0.sortOrder < $1.sortOrder })
}
/// Return either all visible or invisible user's assets (no spam) stored in CoreData based on
/// the given `visible` value
public func getAllUserAssetsInNetworkAssetsByVisibility(networks: [BraveWallet.NetworkInfo], visible: Bool) -> [NetworkAssets] {
var allVisibleUserAssets: [NetworkAssets] = []
for (index, network) in networks.enumerated() {
let groupId = network.walletUserAssetGroupId
if let walletUserAssets = WalletUserAssetGroup.getGroup(groupId: groupId)?.walletUserAssets?.filter({ $0.visible == visible && $0.isSpam == false && $0.isDeletedByUser == false }) {
let networkAsset = NetworkAssets(
network: network,
tokens: walletUserAssets.map(\.blockchainToken),
sortOrder: index
)
allVisibleUserAssets.append(networkAsset)
}
}
return allVisibleUserAssets.sorted(by: { $0.sortOrder < $1.sortOrder })
}
/// Return all user NFTs stored in CoreData with the given `isSpam` status
public func getAllUserNFTs(networks: [BraveWallet.NetworkInfo], isSpam: Bool) -> [NetworkAssets] {
var allUserSpamAssets: [NetworkAssets] = []
for (index, network) in networks.enumerated() {
let groupId = network.walletUserAssetGroupId
if let walletUserAssets = WalletUserAssetGroup.getGroup(groupId: groupId)?.walletUserAssets?.filter({ $0.isSpam == isSpam && ($0.isERC721 || $0.isNFT) && $0.isDeletedByUser == false }) { // Even though users can only spam/unspam NFTs, but we put the NFT filter here to make sure only NFTs are returned
let networkAsset = NetworkAssets(
network: network,
tokens: walletUserAssets.map(\.blockchainToken),
sortOrder: index
)
allUserSpamAssets.append(networkAsset)
}
}
return allUserSpamAssets.sorted(by: { $0.sortOrder < $1.sortOrder })
}
public func getAllUserDeletedNFTs() -> [WalletUserAsset] {
WalletUserAsset.getAllUserDeletedUserAssets() ?? []
}
public func getUserAsset(_ asset: BraveWallet.BlockchainToken) -> WalletUserAsset? {
WalletUserAsset.getUserAsset(asset: asset)
}
public func addUserAsset(_ asset: BraveWallet.BlockchainToken, completion: (() -> Void)?) {
if let existedAsset = WalletUserAsset.getUserAsset(asset: asset) {
if existedAsset.isDeletedByUser { // this asset was added before but user marked as deleted after
WalletUserAsset.updateUserAsset(for: asset, visible: true, isSpam: false, isDeletedByUser: false, completion: completion)
} else { // this asset already exists
completion?()
return
}
} else { // asset does not exist in database
WalletUserAsset.addUserAsset(asset: asset, completion: completion)
}
}
public func removeUserAsset(_ asset: BraveWallet.BlockchainToken, completion: (() -> Void)?) {
WalletUserAsset.removeUserAsset(asset: asset, completion: completion)
}
public func updateUserAsset(
for asset: BraveWallet.BlockchainToken,
visible: Bool,
isSpam: Bool,
isDeletedByUser: Bool,
completion: (() -> Void)?
) {
WalletUserAsset.updateUserAsset(
for: asset,
visible: visible,
isSpam: isSpam,
isDeletedByUser: isDeletedByUser,
completion: completion
)
}
public func removeGroup(for groupId: String, completion: (() -> Void)?) {
WalletUserAssetGroup.removeGroup(groupId, completion: completion)
}
public func migrateUserAssets(completion: (() -> Void)? = nil) {
Task { @MainActor in
if !Preferences.Wallet.migrateCoreToWalletUserAssetCompleted.value {
migrateUserAssets(for: Array(WalletConstants.supportedCoinTypes()), completion: completion)
} else {
let allNetworks = await rpcService.allNetworksForSupportedCoins(respectTestnetPreference: false)
DataController.performOnMainContext { context in
let newCoins = self.allNewCoinsIntroduced(networks: allNetworks, context: context)
if !newCoins.isEmpty {
self.migrateUserAssets(for: newCoins, completion: completion)
} else {
completion?()
}
}
}
}
}
private func allNewCoinsIntroduced(networks: [BraveWallet.NetworkInfo], context: NSManagedObjectContext) -> [BraveWallet.CoinType] {
guard let assetGroupIds = WalletUserAssetGroup.getAllGroups(context: context)?.map({ group in
group.groupId
}) else { return WalletConstants.supportedCoinTypes().elements }
var newCoins: Set<BraveWallet.CoinType> = []
for network in networks where !assetGroupIds.contains("\(network.coin.rawValue).\(network.chainId)") {
newCoins.insert(network.coin)
}
return Array(newCoins)
}
private func migrateUserAssets(for coins: [BraveWallet.CoinType], completion: (() -> Void)?) {
Task { @MainActor in
var fetchedUserAssets: [String: [BraveWallet.BlockchainToken]] = [:]
let networks: [BraveWallet.NetworkInfo] = await rpcService.allNetworks(for: coins, respectTestnetPreference: false)
let networkAssets = await walletService.allUserAssets(in: networks)
for networkAsset in networkAssets {
fetchedUserAssets["\(networkAsset.network.coin.rawValue).\(networkAsset.network.chainId)"] = networkAsset.tokens
}
WalletUserAsset.migrateVisibleAssets(fetchedUserAssets) {
Preferences.Wallet.migrateCoreToWalletUserAssetCompleted.value = true
completion?()
}
}
}
}
#if DEBUG
public class TestableWalletUserAssetManager: WalletUserAssetManagerType {
public var _getAllUserAssetsInNetworkAssetsByVisibility: ((_ networks: [BraveWallet.NetworkInfo], _ visible: Bool) -> [NetworkAssets])?
public var _getAllUserAssetsInNetworkAssets: ((_ networks: [BraveWallet.NetworkInfo], _ includingUserDeleted: Bool) -> [NetworkAssets])?
public var _getAllUserNFTs: ((_ networks: [BraveWallet.NetworkInfo], _ spamStatus: Bool) -> [NetworkAssets])?
public var _getAllUserDeletedNFTs: (() -> [WalletUserAsset])?
public init() {}
public func getAllUserAssetsInNetworkAssets(networks: [BraveWallet.NetworkInfo], includingUserDeleted: Bool) -> [NetworkAssets] {
let defaultAssets: [NetworkAssets] = [
NetworkAssets(network: .mockMainnet, tokens: [.previewToken], sortOrder: 0),
NetworkAssets(network: .mockGoerli, tokens: [.previewToken], sortOrder: 1)
]
let chainIds = networks.map { $0.chainId }
return _getAllUserAssetsInNetworkAssets?(networks, includingUserDeleted) ?? defaultAssets.filter({
chainIds.contains($0.network.chainId)
})
}
public func getAllUserAssetsInNetworkAssetsByVisibility(networks: [BraveWallet.NetworkInfo], visible: Bool) -> [NetworkAssets] {
_getAllUserAssetsInNetworkAssetsByVisibility?(networks, visible) ?? []
}
public func getAllUserNFTs(networks: [BraveWallet.NetworkInfo], isSpam: Bool) -> [NetworkAssets] {
_getAllUserNFTs?(networks, isSpam) ?? []
}
public func getAllUserDeletedNFTs() -> [WalletUserAsset] {
_getAllUserDeletedNFTs?() ?? []
}
public func getUserAsset(_ asset: BraveWallet.BlockchainToken) -> WalletUserAsset? {
return nil
}
public func addUserAsset(_ asset: BraveWallet.BlockchainToken, completion: (() -> Void)?) {
}
public func removeUserAsset(_ asset: BraveWallet.BlockchainToken, completion: (() -> Void)?) {
}
public func removeGroup(for groupId: String, completion: (() -> Void)?) {
}
public func updateUserAsset(
for asset: BraveWallet.BlockchainToken,
visible: Bool,
isSpam: Bool,
isDeletedByUser: Bool,
completion: (() -> Void)?
) {
}
}
#endif