parent
4b27bca2a1
commit
522fd4be5b
@ -0,0 +1,67 @@ |
||||
// Copyright © 2018 Stormbird PTE. LTD. |
||||
|
||||
import Foundation |
||||
import Combine |
||||
import AlphaWalletAttestation |
||||
import AlphaWalletFoundation |
||||
import AlphaWalletTokenScript |
||||
|
||||
public class FetchTokenScriptFilesImpl: FetchTokenScriptFiles { |
||||
private let assetDefinitionStore: AssetDefinitionStore |
||||
private let tokensDataStore: TokensDataStore |
||||
private let sessionsProvider: SessionsProvider |
||||
private let queue = DispatchQueue(label: "com.FetchAssetDefinitions.UpdateQueue") |
||||
private var cancellable = Set<AnyCancellable>() |
||||
|
||||
public init(assetDefinitionStore: AssetDefinitionStore, |
||||
tokensDataStore: TokensDataStore, |
||||
sessionsProvider: SessionsProvider) { |
||||
|
||||
self.assetDefinitionStore = assetDefinitionStore |
||||
self.tokensDataStore = tokensDataStore |
||||
self.sessionsProvider = sessionsProvider |
||||
} |
||||
|
||||
public func start() { |
||||
fetchForTokens() |
||||
fetchForAttestations() |
||||
} |
||||
|
||||
private func fetchForTokens() { |
||||
sessionsProvider.sessions |
||||
.map { $0.keys } |
||||
.receive(on: queue) |
||||
.flatMap { [tokensDataStore] servers in |
||||
asFuture { |
||||
await tokensDataStore.tokens(for: Array(servers)) |
||||
} |
||||
} |
||||
.map { tokens in |
||||
return tokens.filter { |
||||
switch $0.type { |
||||
case .erc20, .erc721, .erc875, .erc721ForTickets, .erc1155: |
||||
return true |
||||
case .nativeCryptocurrency: |
||||
return false |
||||
} |
||||
}.map { AddressAndOptionalRPCServer(address: $0.contractAddress, server: $0.server) } |
||||
}.sink { [assetDefinitionStore] contractsInDatabase in |
||||
let contractsWithTokenScriptFileFromOfficialRepo = assetDefinitionStore.contractsWithTokenScriptFileFromOfficialRepo.map { AddressAndOptionalRPCServer(address: $0, server: nil) } |
||||
|
||||
let contractsAndServers = Array(Set(contractsInDatabase + contractsWithTokenScriptFileFromOfficialRepo)) |
||||
assetDefinitionStore.fetchXMLs(forContractsAndServers: contractsAndServers) |
||||
}.store(in: &cancellable) |
||||
} |
||||
|
||||
private func fetchForAttestations() { |
||||
let attestations = AttestationsStore.allAttestations() |
||||
for each in attestations { |
||||
if let url = each.scriptUri { |
||||
Task { @MainActor in |
||||
await assetDefinitionStore.fetchXMLForAttestation(withScriptURL: url) |
||||
//TODO: attestations+TokenScript to implement |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
@ -1,48 +1,5 @@ |
||||
// Copyright © 2018 Stormbird PTE. LTD. |
||||
// Copyright © 2023 Stormbird PTE. LTD. |
||||
|
||||
import Foundation |
||||
import Combine |
||||
import AlphaWalletTokenScript |
||||
|
||||
public class FetchTokenScriptFiles { |
||||
private let assetDefinitionStore: AssetDefinitionStore |
||||
private let tokensDataStore: TokensDataStore |
||||
private let sessionsProvider: SessionsProvider |
||||
private let queue = DispatchQueue(label: "com.FetchAssetDefinitions.UpdateQueue") |
||||
private var cancellable = Set<AnyCancellable>() |
||||
|
||||
public init(assetDefinitionStore: AssetDefinitionStore, |
||||
tokensDataStore: TokensDataStore, |
||||
sessionsProvider: SessionsProvider) { |
||||
|
||||
self.assetDefinitionStore = assetDefinitionStore |
||||
self.tokensDataStore = tokensDataStore |
||||
self.sessionsProvider = sessionsProvider |
||||
} |
||||
|
||||
public func start() { |
||||
sessionsProvider.sessions |
||||
.map { $0.keys } |
||||
.receive(on: queue) |
||||
.flatMap { [tokensDataStore] servers in |
||||
asFuture { |
||||
await tokensDataStore.tokens(for: Array(servers)) |
||||
} |
||||
} |
||||
.map { tokens in |
||||
return tokens.filter { |
||||
switch $0.type { |
||||
case .erc20, .erc721, .erc875, .erc721ForTickets, .erc1155: |
||||
return true |
||||
case .nativeCryptocurrency: |
||||
return false |
||||
} |
||||
}.map { AddressAndOptionalRPCServer(address: $0.contractAddress, server: $0.server) } |
||||
}.sink { [assetDefinitionStore] contractsInDatabase in |
||||
let contractsWithTokenScriptFileFromOfficialRepo = assetDefinitionStore.contractsWithTokenScriptFileFromOfficialRepo.map { AddressAndOptionalRPCServer(address: $0, server: nil) } |
||||
|
||||
let contractsAndServers = Array(Set(contractsInDatabase + contractsWithTokenScriptFileFromOfficialRepo)) |
||||
assetDefinitionStore.fetchXMLs(forContractsAndServers: contractsAndServers) |
||||
}.store(in: &cancellable) |
||||
} |
||||
public protocol FetchTokenScriptFiles { |
||||
func start() |
||||
} |
||||
|
@ -0,0 +1,13 @@ |
||||
// Copyright © 2023 Stormbird PTE. LTD. |
||||
|
||||
import Foundation |
||||
|
||||
class TokenScriptForAttestationStore { |
||||
//TODO improve storage when we know more about how the TokenScript store for attestations is used |
||||
private var storage: [URL: String] = [:] |
||||
|
||||
subscript(url: URL) -> String? { |
||||
get { return storage[url] } |
||||
set { storage[url] = newValue } |
||||
} |
||||
} |
Loading…
Reference in new issue