From e3d993dd4b6b4e46842f1fc0637faaeb5aa10a6e Mon Sep 17 00:00:00 2001 From: ERussel Date: Wed, 15 Jan 2025 18:08:08 +0100 Subject: [PATCH] refactoring --- .../Common/Configs/ApplicationConfigs.swift | 2 +- .../Predicate/NSPredicate+Filter.swift | 6 +- .../BalanceRemoteSubscriptionService.swift | 224 +++++++++++------- 3 files changed, 143 insertions(+), 89 deletions(-) diff --git a/novawallet/Common/Configs/ApplicationConfigs.swift b/novawallet/Common/Configs/ApplicationConfigs.swift index 69362349d..6f04082b3 100644 --- a/novawallet/Common/Configs/ApplicationConfigs.swift +++ b/novawallet/Common/Configs/ApplicationConfigs.swift @@ -141,7 +141,7 @@ extension ApplicationConfig: ApplicationConfigProtocol { #if F_RELEASE URL(string: "https://raw.githubusercontent.com/novasamatech/nova-utils/master/chains/v21/chains.json")! #else - URL(string: "https://raw.githubusercontent.com/novasamatech/nova-utils/master/chains/v21/chains_dev.json")! + URL(string: "https://raw.githubusercontent.com/novasamatech/nova-utils/refs/heads/test/muse_testnet/chains/v21/chains_dev.json")! #endif } diff --git a/novawallet/Common/Extension/Foundation/Predicate/NSPredicate+Filter.swift b/novawallet/Common/Extension/Foundation/Predicate/NSPredicate+Filter.swift index 3f3247e3a..92653890b 100644 --- a/novawallet/Common/Extension/Foundation/Predicate/NSPredicate+Filter.swift +++ b/novawallet/Common/Extension/Foundation/Predicate/NSPredicate+Filter.swift @@ -229,7 +229,11 @@ extension NSPredicate { chainAssetId.assetId ) - let storagePredicate = NSPredicate(format: "%K == %@", storage) + let storagePredicate = NSPredicate( + format: "%K == %@", + #keyPath(CDAssetLock.storage), + storage + ) return NSCompoundPredicate(andPredicateWithSubpredicates: [ accountPredicate, chainIdPredicate, assetIdPredicate, storagePredicate diff --git a/novawallet/Common/Services/RemoteSubscription/Substrate/BalanceRemoteSubscriptionService.swift b/novawallet/Common/Services/RemoteSubscription/Substrate/BalanceRemoteSubscriptionService.swift index 37fb62328..22330db11 100644 --- a/novawallet/Common/Services/RemoteSubscription/Substrate/BalanceRemoteSubscriptionService.swift +++ b/novawallet/Common/Services/RemoteSubscription/Substrate/BalanceRemoteSubscriptionService.swift @@ -65,14 +65,75 @@ final class BalanceRemoteSubscriptionService: RemoteSubscriptionService { ) } - private func prepareNativeAssetSubscriptionRequests( + func prepareSubscriptionRequests( from accountId: AccountId, chainAsset: ChainAsset, transactionSubscription: TransactionSubscribing? - ) throws -> [SubscriptionSettings] { - let storageKeyFactory = LocalStorageKeyFactory() - let chainId = chainAsset.chain.chainId + ) -> [SubscriptionSettings] { + do { + if let assetRawType = chainAsset.asset.type { + guard let customAssetType = AssetType(rawValue: assetRawType) else { + return [] + } + + switch customAssetType { + case .statemine: + return try prepareAssetsPalletSubscriptionRequests( + from: accountId, + chainAsset: chainAsset, + transactionSubscription: transactionSubscription + ) + case .orml: + return try prepareOrmlSubscriptionRequests( + from: accountId, + chainAsset: chainAsset, + transactionSubscription: transactionSubscription + ) + case .evmAsset, .evmNative, .equilibrium: + logger.error("Unsupported asset type: \(customAssetType)") + return [] + } + } else { + return try prepareNativeAssetSubscriptionRequests( + from: accountId, + chainAsset: chainAsset, + transactionSubscription: transactionSubscription + ) + } + } catch { + logger.error("Can't create request: \(error)") + return [] + } + } + func prepareSubscriptionRequests( + from accountId: AccountId, + chain: ChainModel, + onlyFor assetIds: Set?, + transactionSubscription: TransactionSubscribing? + ) -> [SubscriptionSettings] { + let chainAssets = if let assetIds { + chain.chainAssets().filter { assetIds.contains($0.asset.assetId) } + } else { + chain.chainAssets() + } + + return chainAssets.flatMap { chainAsset in + prepareSubscriptionRequests( + from: accountId, + chainAsset: chainAsset, + transactionSubscription: transactionSubscription + ) + } + } +} + +private extension BalanceRemoteSubscriptionService { + func getAccountInfoRequest( + from accountId: AccountId, + chainId: ChainModel.Id, + storageKeyFactory: LocalStorageKeyFactoryProtocol + ) throws -> MapSubscriptionRequest { let accountStoragePath = SystemPallet.accountPath let accountLocalKey = try storageKeyFactory.createFromStoragePath( accountStoragePath, @@ -80,6 +141,16 @@ final class BalanceRemoteSubscriptionService: RemoteSubscriptionService { chainId: chainId ) + return MapSubscriptionRequest(storagePath: accountStoragePath, localKey: accountLocalKey) { + BytesCodable(wrappedValue: accountId) + } + } + + func getLocksRequest( + from accountId: AccountId, + chainId: ChainModel.Id, + storageKeyFactory: LocalStorageKeyFactoryProtocol + ) throws -> MapSubscriptionRequest { let locksStoragePath = StorageCodingPath.balanceLocks let locksLocalKey = try storageKeyFactory.createFromStoragePath( locksStoragePath, @@ -87,6 +158,17 @@ final class BalanceRemoteSubscriptionService: RemoteSubscriptionService { chainId: chainId ) + return MapSubscriptionRequest( + storagePath: locksStoragePath, + localKey: locksLocalKey + ) { BytesCodable(wrappedValue: accountId) } + } + + func getHoldsRequest( + from accountId: AccountId, + chainId: ChainModel.Id, + storageKeyFactory: LocalStorageKeyFactoryProtocol + ) throws -> MapSubscriptionRequest { let holdsStoragePath = BalancesPallet.holdsPath let holdsLocalKey = try storageKeyFactory.createFromStoragePath( holdsStoragePath, @@ -94,6 +176,17 @@ final class BalanceRemoteSubscriptionService: RemoteSubscriptionService { chainId: chainId ) + return MapSubscriptionRequest( + storagePath: holdsStoragePath, + localKey: holdsLocalKey + ) { BytesCodable(wrappedValue: accountId) } + } + + func getFreezesRequest( + from accountId: AccountId, + chainId: ChainModel.Id, + storageKeyFactory: LocalStorageKeyFactoryProtocol + ) throws -> MapSubscriptionRequest { let freezesStoragePath = BalancesPallet.freezesPath let freezesLocalKey = try storageKeyFactory.createFromStoragePath( freezesStoragePath, @@ -101,33 +194,52 @@ final class BalanceRemoteSubscriptionService: RemoteSubscriptionService { chainId: chainId ) - let accountRequest = MapSubscriptionRequest(storagePath: accountStoragePath, localKey: accountLocalKey) { - BytesCodable(wrappedValue: accountId) - } - - let locksRequest = MapSubscriptionRequest( - storagePath: locksStoragePath, - localKey: locksLocalKey - ) { BytesCodable(wrappedValue: accountId) } - - let holdsRequest = MapSubscriptionRequest( - storagePath: holdsStoragePath, - localKey: holdsLocalKey - ) { BytesCodable(wrappedValue: accountId) } - - let freezesRequest = MapSubscriptionRequest( + return MapSubscriptionRequest( storagePath: freezesStoragePath, localKey: freezesLocalKey ) { BytesCodable(wrappedValue: accountId) } + } + + func prepareNativeAssetSubscriptionRequests( + from accountId: AccountId, + chainAsset: ChainAsset, + transactionSubscription: TransactionSubscribing? + ) throws -> [SubscriptionSettings] { + let storageKeyFactory = LocalStorageKeyFactory() + let chainId = chainAsset.chain.chainId + + let accountRequest = try getAccountInfoRequest( + from: accountId, + chainId: chainId, + storageKeyFactory: storageKeyFactory + ) + + let locksRequest = try getLocksRequest( + from: accountId, + chainId: chainId, + storageKeyFactory: storageKeyFactory + ) + + let holdsRequest = try getHoldsRequest( + from: accountId, + chainId: chainId, + storageKeyFactory: storageKeyFactory + ) + + let freezesRequest = try getFreezesRequest( + from: accountId, + chainId: chainId, + storageKeyFactory: storageKeyFactory + ) let handlerFactory = subscriptionHandlingFactory.createNative( for: accountId, chainAssetId: chainAsset.chainAssetId, params: .init( - accountLocalStorageKey: accountLocalKey, - locksLocalStorageKey: locksLocalKey, - holdsLocalStorageKey: holdsLocalKey, - freezesLocalStorageKey: freezesLocalKey + accountLocalStorageKey: accountRequest.localKey, + locksLocalStorageKey: locksRequest.localKey, + holdsLocalStorageKey: holdsRequest.localKey, + freezesLocalStorageKey: freezesRequest.localKey ), transactionSubscription: transactionSubscription ) @@ -140,7 +252,7 @@ final class BalanceRemoteSubscriptionService: RemoteSubscriptionService { ] } - private func prepareAssetsPalletSubscriptionRequests( + func prepareAssetsPalletSubscriptionRequests( from accountId: AccountId, chainAsset: ChainAsset, transactionSubscription: TransactionSubscribing? @@ -196,7 +308,7 @@ final class BalanceRemoteSubscriptionService: RemoteSubscriptionService { ] } - private func prepareOrmlSubscriptionRequests( + func prepareOrmlSubscriptionRequests( from accountId: AccountId, chainAsset: ChainAsset, transactionSubscription: TransactionSubscribing? @@ -249,66 +361,4 @@ final class BalanceRemoteSubscriptionService: RemoteSubscriptionService { SubscriptionSettings(request: locksRequest, handlingFactory: handlerFactory) ] } - - func prepareSubscriptionRequests( - from accountId: AccountId, - chainAsset: ChainAsset, - transactionSubscription: TransactionSubscribing? - ) -> [SubscriptionSettings] { - do { - if let assetRawType = chainAsset.asset.type { - guard let customAssetType = AssetType(rawValue: assetRawType) else { - return [] - } - - switch customAssetType { - case .statemine: - return try prepareAssetsPalletSubscriptionRequests( - from: accountId, - chainAsset: chainAsset, - transactionSubscription: transactionSubscription - ) - case .orml: - return try prepareOrmlSubscriptionRequests( - from: accountId, - chainAsset: chainAsset, - transactionSubscription: transactionSubscription - ) - case .evmAsset, .evmNative, .equilibrium: - logger.error("Unsupported asset type: \(customAssetType)") - return [] - } - } else { - return try prepareNativeAssetSubscriptionRequests( - from: accountId, - chainAsset: chainAsset, - transactionSubscription: transactionSubscription - ) - } - } catch { - logger.error("Can't create request: \(error)") - return [] - } - } - - func prepareSubscriptionRequests( - from accountId: AccountId, - chain: ChainModel, - onlyFor assetIds: Set?, - transactionSubscription: TransactionSubscribing? - ) -> [SubscriptionSettings] { - let chainAssets = if let assetIds { - chain.chainAssets().filter { assetIds.contains($0.asset.assetId) } - } else { - chain.chainAssets() - } - - return chainAssets.flatMap { chainAsset in - prepareSubscriptionRequests( - from: accountId, - chainAsset: chainAsset, - transactionSubscription: transactionSubscription - ) - } - } }