Merge pull request #4598 from oa-s/#4597

Return directly promise when fetching events EventSourceCoordinatorForActivities #4597
pull/4596/head
Crypto Pank 3 years ago committed by GitHub
commit 76ffcb183f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinator.swift
  2. 16
      AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinatorForActivities.swift
  3. 19
      AlphaWallet/Tokens/Helpers/CallSmartContractFunction.swift

@ -139,8 +139,6 @@ extension EventSourceCoordinator {
extension EventSourceCoordinator.functional {
static func fetchEvents(forTokenId tokenId: TokenId, token: TokenObject, eventOrigin: EventOrigin, wallet: Wallet, eventsDataStore: NonActivityEventsDataStore, queue: DispatchQueue) -> Promise<Void> {
let (promise, seal) = Promise<Void>.pending()
let (filterName, filterValue) = eventOrigin.eventFilter
let filterParam = eventOrigin
.parameters
@ -160,20 +158,16 @@ extension EventSourceCoordinator.functional {
let eventFilter = EventFilter(fromBlock: fromBlock, toBlock: .latest, addresses: addresses, parameterFilters: parameterFilters)
getEventLogs(withServer: token.server, contract: eventOrigin.contract, eventName: eventOrigin.eventName, abiString: eventOrigin.eventAbiString, filter: eventFilter, queue: queue)
.done(on: queue, { result in
return getEventLogs(withServer: token.server, contract: eventOrigin.contract, eventName: eventOrigin.eventName, abiString: eventOrigin.eventAbiString, filter: eventFilter, queue: queue)
.done(on: queue, { result -> Void in
let events = result.compactMap {
Self.convertEventToDatabaseObject($0, filterParam: filterParam, eventOrigin: eventOrigin, contractAddress: token.contractAddress, server: token.server)
}
eventsDataStore.add(events: events)
seal.fulfill(())
}).catch(on: queue, { e in
}).recover(on: queue, { e in
error(value: e, rpcServer: token.server, address: token.contractAddress)
seal.reject(e)
})
return promise
}
static func convertToImplicitAttribute(string: String) -> AssetImplicitAttributes? {

@ -125,8 +125,6 @@ extension EventSourceCoordinatorForActivities {
extension EventSourceCoordinatorForActivities.functional {
static func fetchEvents(tokenContract: AlphaWallet.Address, server: RPCServer, card: TokenScriptCard, eventsDataStore: EventsActivityDataStoreProtocol, queue: DispatchQueue, wallet: Wallet) -> Promise<Void> {
let (promise, seal) = Promise<Void>.pending()
let eventOrigin = card.eventOrigin
let (filterName, filterValue) = eventOrigin.eventFilter
typealias functional = EventSourceCoordinatorForActivities.functional
@ -136,14 +134,12 @@ extension EventSourceCoordinatorForActivities.functional {
if filterParam.allSatisfy({ $0 == nil }) {
//TODO log to console as diagnostic
seal.fulfill(())
return promise
return .init(error: PMKError.cancelled)
}
let oldEvent = eventsDataStore
.getLastMatchingEventSortedByBlockNumber(forContract: eventOrigin.contract, tokenContract: tokenContract, server: server, eventName: eventOrigin.eventName)
//<<<<<<< HEAD
let fromBlock: (EventFilter.Block, UInt64)
if let newestEvent = oldEvent {
let value = UInt64(newestEvent.blockNumber + 1)
@ -156,7 +152,7 @@ extension EventSourceCoordinatorForActivities.functional {
let toBlock = server.makeMaximumToBlockForEvents(fromBlockNumber: fromBlock.1)
let eventFilter = EventFilter(fromBlock: fromBlock.0, toBlock: toBlock, addresses: addresses, parameterFilters: parameterFilters)
getEventLogs(withServer: server, contract: eventOrigin.contract, eventName: eventOrigin.eventName, abiString: eventOrigin.eventAbiString, filter: eventFilter, queue: queue)
return getEventLogs(withServer: server, contract: eventOrigin.contract, eventName: eventOrigin.eventName, abiString: eventOrigin.eventAbiString, filter: eventFilter, queue: queue)
.then(on: queue, { events -> Promise<[EventActivityInstance]> in
let promises = events.compactMap { event -> Promise<EventActivityInstance?> in
guard let blockNumber = event.eventLog?.blockNumber else {
@ -175,15 +171,11 @@ extension EventSourceCoordinatorForActivities.functional {
return when(resolved: promises).map(on: queue, { values -> [EventActivityInstance] in
values.compactMap { $0.optionalValue }.compactMap { $0 }
})
}).done(on: queue, { events in
}).map(on: queue, { events -> Void in
eventsDataStore.add(events: events)
seal.fulfill(())
}).catch(on: queue, { e in
}).recover(on: queue, { e in
error(value: e, rpcServer: server, address: tokenContract)
seal.reject(e)
})
return promise
}
private static func convertEventToDatabaseObject(_ event: EventParserResultProtocol, date: Date, filterParam: [(filter: [EventFilterable], textEquivalent: String)?], eventOrigin: EventOrigin, tokenContract: AlphaWallet.Address, server: RPCServer) -> EventActivityInstance? {

@ -130,18 +130,15 @@ func getEventLogs(
filter: EventFilter,
queue: DispatchQueue
) -> Promise<[EventParserResultProtocol]> {
firstly { () -> Promise<EthereumAddress> in
let contractAddress = EthereumAddress(address: contract)
return .value(contractAddress)
}.then(on: queue) { contractAddress -> Promise<[EventParserResultProtocol]> in
guard let web3 = try? getCachedWeb3(forServer: server, timeout: 60) else {
throw Web3Error(description: "Error creating web3 for: \(server.rpcURL) + \(server.web3Network)")
}
let contractAddress = EthereumAddress(address: contract)
guard let contractInstance = web3swift.web3.web3contract(web3: web3, abiString: abiString, at: contractAddress, options: web3.options) else {
return Promise(error: Web3Error(description: "Error creating web3swift contract instance to call \(eventName)()"))
}
guard let web3 = try? getCachedWeb3(forServer: server, timeout: 60) else {
return Promise(error: Web3Error(description: "Error creating web3 for: \(server.rpcURL) + \(server.web3Network)"))
}
return contractInstance.getIndexedEventsPromise(eventName: eventName, filter: filter)
guard let contractInstance = web3swift.web3.web3contract(web3: web3, abiString: abiString, at: contractAddress, options: web3.options) else {
return Promise(error: Web3Error(description: "Error creating web3swift contract instance to call \(eventName)()"))
}
return contractInstance.getIndexedEventsPromise(eventName: eventName, filter: filter)
}

Loading…
Cancel
Save