Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ERussel committed Jan 15, 2025
1 parent 0c3c87d commit e3d993d
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 89 deletions.
2 changes: 1 addition & 1 deletion novawallet/Common/Configs/ApplicationConfigs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,69 +65,181 @@ 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<AssetModel.Id>?,
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<BytesCodable> {
let accountStoragePath = SystemPallet.accountPath
let accountLocalKey = try storageKeyFactory.createFromStoragePath(
accountStoragePath,
accountId: accountId,
chainId: chainId
)

return MapSubscriptionRequest(storagePath: accountStoragePath, localKey: accountLocalKey) {
BytesCodable(wrappedValue: accountId)
}
}

func getLocksRequest(
from accountId: AccountId,
chainId: ChainModel.Id,
storageKeyFactory: LocalStorageKeyFactoryProtocol
) throws -> MapSubscriptionRequest<BytesCodable> {
let locksStoragePath = StorageCodingPath.balanceLocks
let locksLocalKey = try storageKeyFactory.createFromStoragePath(
locksStoragePath,
encodableElement: accountId,
chainId: chainId
)

return MapSubscriptionRequest(
storagePath: locksStoragePath,
localKey: locksLocalKey
) { BytesCodable(wrappedValue: accountId) }
}

func getHoldsRequest(
from accountId: AccountId,
chainId: ChainModel.Id,
storageKeyFactory: LocalStorageKeyFactoryProtocol
) throws -> MapSubscriptionRequest<BytesCodable> {
let holdsStoragePath = BalancesPallet.holdsPath
let holdsLocalKey = try storageKeyFactory.createFromStoragePath(
holdsStoragePath,
encodableElement: accountId,
chainId: chainId
)

return MapSubscriptionRequest(
storagePath: holdsStoragePath,
localKey: holdsLocalKey
) { BytesCodable(wrappedValue: accountId) }
}

func getFreezesRequest(
from accountId: AccountId,
chainId: ChainModel.Id,
storageKeyFactory: LocalStorageKeyFactoryProtocol
) throws -> MapSubscriptionRequest<BytesCodable> {
let freezesStoragePath = BalancesPallet.freezesPath
let freezesLocalKey = try storageKeyFactory.createFromStoragePath(
freezesStoragePath,
encodableElement: accountId,
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
)
Expand All @@ -140,7 +252,7 @@ final class BalanceRemoteSubscriptionService: RemoteSubscriptionService {
]
}

private func prepareAssetsPalletSubscriptionRequests(
func prepareAssetsPalletSubscriptionRequests(
from accountId: AccountId,
chainAsset: ChainAsset,
transactionSubscription: TransactionSubscribing?
Expand Down Expand Up @@ -196,7 +308,7 @@ final class BalanceRemoteSubscriptionService: RemoteSubscriptionService {
]
}

private func prepareOrmlSubscriptionRequests(
func prepareOrmlSubscriptionRequests(
from accountId: AccountId,
chainAsset: ChainAsset,
transactionSubscription: TransactionSubscribing?
Expand Down Expand Up @@ -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<AssetModel.Id>?,
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
)
}
}
}

0 comments on commit e3d993d

Please sign in to comment.