@@ -37,7 +37,7 @@ extension web3.web3contract {
37
37
38
38
*/
39
39
public func parseBlockByNumber( _ blockNumber: UInt64 ) throws -> [ EventParserResultProtocol ] {
40
- let result = try self . parseBlockByNumberPromise ( blockNumber) . wait ( )
40
+ let result = try self . parseBlockByNumberPromise ( blockNumber)
41
41
return result
42
42
}
43
43
@@ -54,7 +54,7 @@ extension web3.web3contract {
54
54
55
55
*/
56
56
public func parseBlock( _ block: Block ) throws -> [ EventParserResultProtocol ] {
57
- let result = try self . parseBlockPromise ( block) . wait ( )
57
+ let result = try self . parseBlockPromise ( block)
58
58
return result
59
59
}
60
60
@@ -71,7 +71,7 @@ extension web3.web3contract {
71
71
72
72
*/
73
73
public func parseTransactionByHash( _ hash: Data ) throws -> [ EventParserResultProtocol ] {
74
- let result = try self . parseTransactionByHashPromise ( hash) . wait ( )
74
+ let result = try self . parseTransactionByHashPromise ( hash)
75
75
return result
76
76
}
77
77
@@ -88,82 +88,61 @@ extension web3.web3contract {
88
88
89
89
*/
90
90
public func parseTransaction( _ transaction: EthereumTransaction ) throws -> [ EventParserResultProtocol ] {
91
- let result = try self . parseTransactionPromise ( transaction) . wait ( )
91
+ let result = try self . parseTransactionPromise ( transaction)
92
92
return result
93
93
}
94
94
}
95
95
}
96
96
97
97
extension web3 . web3contract . EventParser {
98
- public func parseTransactionPromise( _ transaction: EthereumTransaction ) -> Promise < [ EventParserResultProtocol ] > {
99
- let queue = self . web3. requestDispatcher. queue
100
- do {
101
- guard let hash = transaction. hash else {
102
- throw Web3Error . processingError ( desc: " Failed to get transaction hash " ) }
103
- return self . parseTransactionByHashPromise ( hash)
104
- } catch {
105
- let returnPromise = Promise< [ EventParserResultProtocol] > . pending( )
106
- queue. async {
107
- returnPromise. resolver. reject ( error)
108
- }
109
- return returnPromise. promise
98
+ public func parseTransactionPromise( _ transaction: EthereumTransaction ) throws -> [ EventParserResultProtocol ] {
99
+ guard let hash = transaction. hash else {
100
+ throw Web3Error . processingError ( desc: " Failed to get transaction hash " )
110
101
}
102
+ return try self . parseTransactionByHashPromise ( hash)
111
103
}
112
104
113
- public func parseTransactionByHashPromise( _ hash: Data ) -> Promise < [ EventParserResultProtocol ] > {
114
- let queue = self . web3. requestDispatcher. queue
115
- return self . web3. eth. getTransactionReceiptPromise ( hash) . map ( on: queue) { receipt throws -> [ EventParserResultProtocol ] in
116
- guard let results = parseReceiptForLogs ( receipt: receipt, contract: self . contract, eventName: self . eventName, filter: self . filter) else {
117
- throw Web3Error . processingError ( desc: " Failed to parse receipt for events " )
118
- }
119
- return results
105
+ public func parseTransactionByHashPromise( _ hash: Data ) throws -> [ EventParserResultProtocol ] {
106
+ let receipt = self . web3. eth. getTransactionReceiptPromise ( hash)
107
+
108
+ guard let results = parseReceiptForLogs ( receipt: receipt, contract: contract, eventName: eventName, filter: filter) else {
109
+ throw Web3Error . processingError ( desc: " Failed to parse receipt for events " )
120
110
}
111
+ return results
112
+
121
113
}
122
114
123
- public func parseBlockByNumberPromise( _ blockNumber: UInt64 ) -> Promise < [ EventParserResultProtocol ] > {
124
- let queue = self . web3. requestDispatcher. queue
125
- do {
126
- if self . filter != nil && ( self . filter? . fromBlock != nil || self . filter? . toBlock != nil ) {
127
- throw Web3Error . inputError ( desc: " Can not mix parsing specific block and using block range filter " )
128
- }
129
- return self . web3. eth. getBlockByNumberPromise ( blockNumber) . then ( on: queue) { res in
130
- return self . parseBlockPromise ( res)
131
- }
132
- } catch {
133
- let returnPromise = Promise< [ EventParserResultProtocol] > . pending( )
134
- queue. async {
135
- returnPromise. resolver. reject ( error)
136
- }
137
- return returnPromise. promise
115
+ public func parseBlockByNumberPromise( _ blockNumber: UInt64 ) async throws -> [ EventParserResultProtocol ] {
116
+
117
+ guard filter == nil || filter? . fromBlock == nil && filter? . toBlock == nil else {
118
+ throw Web3Error . inputError ( desc: " Can not mix parsing specific block and using block range filter " )
138
119
}
120
+
121
+ let res = try await self . web3. eth. getBlockByNumberPromise ( blockNumber)
122
+
123
+ return try self . parseBlockPromise ( res)
139
124
}
140
125
141
- public func parseBlockPromise( _ block: Block ) -> Promise < [ EventParserResultProtocol ] > {
142
- let queue = self . web3. requestDispatcher. queue
143
- do {
144
- guard let bloom = block. logsBloom else {
145
- throw Web3Error . processingError ( desc: " Block doesn't have a bloom filter log " )
146
- }
147
- if self . contract. address != nil {
148
- let addressPresent = block. logsBloom? . test ( topic: self . contract. address!. addressData)
149
- if ( addressPresent != true ) {
150
- let returnPromise = Promise< [ EventParserResultProtocol] > . pending( )
151
- queue. async {
152
- returnPromise. resolver. fulfill ( [ EventParserResultProtocol] ( ) )
153
- }
154
- return returnPromise. promise
155
- }
156
- }
157
- guard let eventOfSuchTypeIsPresent = self . contract. testBloomForEventPrecence ( eventName: self . eventName, bloom: bloom) else {
158
- throw Web3Error . processingError ( desc: " Error processing bloom for events " )
159
- }
160
- if ( !eventOfSuchTypeIsPresent) {
161
- let returnPromise = Promise< [ EventParserResultProtocol] > . pending( )
162
- queue. async {
163
- returnPromise. resolver. fulfill ( [ EventParserResultProtocol] ( ) )
164
- }
165
- return returnPromise. promise
126
+ public func parseBlockPromise( _ block: Block ) throws -> [ EventParserResultProtocol ] {
127
+
128
+ guard let bloom = block. logsBloom else {
129
+ throw Web3Error . processingError ( desc: " Block doesn't have a bloom filter log " )
130
+ }
131
+
132
+ if let contractAddress = contract. address {
133
+ if !( block. logsBloom? . test ( topic: contractAddress. addressData) ?? true ) {
134
+ return [ EventParserResultProtocol] ( )
166
135
}
136
+ }
137
+
138
+ guard let eventOfSuchTypeIsPresent = self . contract. testBloomForEventPrecence ( eventName: self . eventName, bloom: bloom) else {
139
+ throw Web3Error . processingError ( desc: " Error processing bloom for events " )
140
+ }
141
+ if ( !eventOfSuchTypeIsPresent) {
142
+ return [ EventParserResultProtocol] ( )
143
+ }
144
+
145
+
167
146
return Promise { seal in
168
147
169
148
var pendingEvents : [ Promise < [ EventParserResultProtocol ] > ] = [ Promise < [ EventParserResultProtocol ] > ] ( )
@@ -184,6 +163,7 @@ extension web3.web3contract.EventParser {
184
163
pendingEvents. append ( subresultPromise)
185
164
}
186
165
}
166
+
187
167
when ( resolved: pendingEvents) . done ( on: queue) { ( results: [ PromiseResult < [ EventParserResultProtocol ] > ] ) throws in
188
168
var allResults = [ EventParserResultProtocol] ( )
189
169
for res in results {
@@ -193,22 +173,12 @@ extension web3.web3contract.EventParser {
193
173
allResults. append ( contentsOf: subresult)
194
174
}
195
175
seal. fulfill ( allResults)
196
- } . catch ( on: queue) { err in
176
+ }
177
+ . catch ( on: queue) { err in
197
178
seal. reject ( err)
198
179
}
199
180
}
200
- } catch {
201
- // let returnPromise = Promise<[EventParserResultProtocol]>.pending()
202
- // queue.async {
203
- // returnPromise.resolver.fulfill([EventParserResultProtocol]())
204
- // }
205
- // return returnPromise.promise
206
- let returnPromise = Promise< [ EventParserResultProtocol] > . pending( )
207
- queue. async {
208
- returnPromise. resolver. reject ( error)
209
- }
210
- return returnPromise. promise
211
- }
181
+
212
182
}
213
183
214
184
}
0 commit comments