@@ -100,6 +100,128 @@ extension web3.web3contract {
100
100
}
101
101
}
102
102
103
+ extension web3 . web3contract . EventParser {
104
+ public func parseTransactionPromise( _ transaction: EthereumTransaction ) -> Promise < [ EventParserResultProtocol ] > {
105
+ let queue = self . web3. requestDispatcher. queue
106
+ do {
107
+ guard let hash = transaction. hash else {
108
+ throw Web3Error . processingError ( " Failed to get transaction hash " ) }
109
+ return self . parseTransactionByHashPromise ( hash)
110
+ } catch {
111
+ let returnPromise = Promise< [ EventParserResultProtocol] > . pending( )
112
+ queue. async {
113
+ returnPromise. resolver. reject ( error)
114
+ }
115
+ return returnPromise. promise
116
+ }
117
+ }
118
+
119
+ public func parseTransactionByHashPromise( _ hash: Data ) -> Promise < [ EventParserResultProtocol ] > {
120
+ let queue = self . web3. requestDispatcher. queue
121
+ return self . web3. eth. getTransactionReceiptPromise ( hash) . map ( on: queue) { receipt throws -> [ EventParserResultProtocol ] in
122
+ guard let results = parseReceiptForLogs ( receipt: receipt, contract: self . contract, eventName: self . eventName, filter: self . filter) else {
123
+ throw Web3Error . processingError ( " Failed to parse receipt for events " )
124
+ }
125
+ return results
126
+ }
127
+ }
128
+
129
+ public func parseBlockByNumberPromise( _ blockNumber: UInt64 ) -> Promise < [ EventParserResultProtocol ] > {
130
+ let queue = self . web3. requestDispatcher. queue
131
+ do {
132
+ if self . filter != nil && ( self . filter? . fromBlock != nil || self . filter? . toBlock != nil ) {
133
+ throw Web3Error . inputError ( " Can not mix parsing specific block and using block range filter " )
134
+ }
135
+ return self . web3. eth. getBlockByNumberPromise ( blockNumber) . then ( on: queue) { res in
136
+ return self . parseBlockPromise ( res)
137
+ }
138
+ } catch {
139
+ let returnPromise = Promise< [ EventParserResultProtocol] > . pending( )
140
+ queue. async {
141
+ returnPromise. resolver. reject ( error)
142
+ }
143
+ return returnPromise. promise
144
+ }
145
+ }
146
+
147
+ public func parseBlockPromise( _ block: Block ) -> Promise < [ EventParserResultProtocol ] > {
148
+ let queue = self . web3. requestDispatcher. queue
149
+ do {
150
+ guard let bloom = block. logsBloom else {
151
+ throw Web3Error . processingError ( " Block doesn't have a bloom filter log " )
152
+ }
153
+ if self . contract. address != nil {
154
+ let addressPresent = block. logsBloom? . test ( topic: self . contract. address!. addressData)
155
+ if ( addressPresent != true ) {
156
+ let returnPromise = Promise< [ EventParserResultProtocol] > . pending( )
157
+ queue. async {
158
+ returnPromise. resolver. fulfill ( [ EventParserResultProtocol] ( ) )
159
+ }
160
+ return returnPromise. promise
161
+ }
162
+ }
163
+ guard let eventOfSuchTypeIsPresent = self . contract. testBloomForEventPrecence ( eventName: self . eventName, bloom: bloom) else {
164
+ throw Web3Error . processingError ( " Error processing bloom for events " )
165
+ }
166
+ if ( !eventOfSuchTypeIsPresent) {
167
+ let returnPromise = Promise< [ EventParserResultProtocol] > . pending( )
168
+ queue. async {
169
+ returnPromise. resolver. fulfill ( [ EventParserResultProtocol] ( ) )
170
+ }
171
+ return returnPromise. promise
172
+ }
173
+ return Promise { seal in
174
+
175
+ var pendingEvents : [ Promise < [ EventParserResultProtocol ] > ] = [ Promise < [ EventParserResultProtocol ] > ] ( )
176
+ for transaction in block. transactions {
177
+ switch transaction {
178
+ case . null:
179
+ seal. reject ( Web3Error . processingError ( " No information about transactions in block " ) )
180
+ return
181
+ case . transaction( let tx) :
182
+ guard let hash = tx. hash else {
183
+ seal. reject ( Web3Error . processingError ( " Failed to get transaction hash " ) )
184
+ return
185
+ }
186
+ let subresultPromise = self . parseTransactionByHashPromise ( hash)
187
+ pendingEvents. append ( subresultPromise)
188
+ case . hash( let hash) :
189
+ let subresultPromise = self . parseTransactionByHashPromise ( hash)
190
+ pendingEvents. append ( subresultPromise)
191
+ }
192
+ }
193
+ when ( resolved: pendingEvents) . done ( on: queue) { ( results: [ PromiseResult < [ EventParserResultProtocol ] > ] ) throws in
194
+ var allResults = [ EventParserResultProtocol] ( )
195
+ for res in results {
196
+ guard case . fulfilled( let subresult) = res else {
197
+ throw Web3Error . processingError ( " Failed to parse event for one transaction in block " )
198
+ }
199
+ allResults. append ( contentsOf: subresult)
200
+ }
201
+ seal. fulfill ( allResults)
202
+ } . catch ( on: queue) { err in
203
+ seal. reject ( err)
204
+ }
205
+ }
206
+ } catch {
207
+ // let returnPromise = Promise<[EventParserResultProtocol]>.pending()
208
+ // queue.async {
209
+ // returnPromise.resolver.fulfill([EventParserResultProtocol]())
210
+ // }
211
+ // return returnPromise.promise
212
+ let returnPromise = Promise< [ EventParserResultProtocol] > . pending( )
213
+ queue. async {
214
+ returnPromise. resolver. reject ( error)
215
+ }
216
+ return returnPromise. promise
217
+ }
218
+ }
219
+
220
+ }
221
+
222
+
223
+
224
+
103
225
extension web3 . web3contract {
104
226
public func getIndexedEvents( eventName: String ? , filter: EventFilter ) -> Result < [ EventParserResultProtocol ] , Web3Error > {
105
227
guard let rawContract = self . contract as? ContractV2 else { return Result . failure ( Web3Error . nodeError ( " ABIv1 is not supported for this method " ) ) }
@@ -150,69 +272,6 @@ extension web3.web3contract {
150
272
}
151
273
}
152
274
153
- extension web3 . web3contract {
154
- public func getIndexedEventsPromise( eventName: String ? , filter: EventFilter , joinWithReceipts: Bool = false ) -> Promise < [ EventParserResultProtocol ] > {
155
- let queue = self . web3. requestDispatcher. queue
156
- do {
157
- guard let rawContract = self . contract as? ContractV2 else {
158
- throw Web3Error . nodeError ( " ABIv1 is not supported for this method " )
159
- }
160
- guard let preEncoding = encodeTopicToGetLogs ( contract: rawContract, eventName: eventName, filter: filter) else {
161
- throw Web3Error . processingError ( " Failed to encode topic for request " )
162
- }
163
- // var event: ABIv2.Element.Event? = nil
164
- if eventName != nil {
165
- guard let _ = rawContract. events [ eventName!] else {
166
- throw Web3Error . processingError ( " No such event in a contract " )
167
- }
168
- // event = ev
169
- }
170
- let request = JSONRPCRequestFabric . prepareRequest ( . getLogs, parameters: [ preEncoding] )
171
- let fetchLogsPromise = self . web3. dispatch ( request) . map ( on: queue) { response throws -> [ EventParserResult ] in
172
- guard let value: [ EventLog ] = response. getValue ( ) else {
173
- throw Web3Error . nodeError ( " Empty or malformed response " )
174
- }
175
- let allLogs = value
176
- let decodedLogs = allLogs. compactMap ( { ( log) -> EventParserResult ? in
177
- let ( n, d) = self . contract. parseEvent ( log)
178
- guard let evName = n, let evData = d else { return nil }
179
- var res = EventParserResult ( eventName: evName, transactionReceipt: nil , contractAddress: log. address, decodedResult: evData)
180
- res. transactionHash = log. transactionHash
181
- return res
182
- } ) . filter { ( res: EventParserResult ? ) -> Bool in
183
- if eventName != nil {
184
- if res != nil && res? . eventName == eventName && res? . transactionHash != nil {
185
- return true
186
- }
187
- } else {
188
- if res != nil && res? . transactionHash != nil {
189
- return true
190
- }
191
- }
192
- return false
193
- }
194
- return decodedLogs
195
- }
196
- if ( !joinWithReceipts) {
197
- return fetchLogsPromise. mapValues ( on: queue) { res -> EventParserResultProtocol in
198
- return res as EventParserResultProtocol
199
- }
200
- }
201
- return fetchLogsPromise. thenMap ( on: queue) { singleEvent in
202
- return self . web3. eth. getTransactionReceiptPromise ( singleEvent. transactionHash!) . map ( on: queue) { receipt in
203
- var joinedEvent = singleEvent
204
- joinedEvent. transactionReceipt = receipt
205
- return joinedEvent as EventParserResultProtocol
206
- }
207
- }
208
- } catch {
209
- let returnPromise = Promise< [ EventParserResultProtocol] > . pending( )
210
- queue. async {
211
- returnPromise. resolver. reject ( error)
212
- }
213
- return returnPromise. promise
214
- }
215
- }
216
- }
275
+
217
276
218
277
0 commit comments