Skip to content

Commit 1008eef

Browse files
committed
Migrate from Promise to Async
1 parent d712c42 commit 1008eef

File tree

5 files changed

+90
-129
lines changed

5 files changed

+90
-129
lines changed

Sources/web3swift/Promises/Promise+Web3+Eth+GetAccounts.swift

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,23 @@ import BigInt
99

1010

1111
extension web3.Eth {
12-
public func getAccountsPromise() -> Promise<[EthereumAddress]> {
13-
let queue = web3.requestDispatcher.queue
14-
if (self.web3.provider.attachedKeystoreManager != nil) {
15-
let promise = Promise<[EthereumAddress]>.pending()
16-
queue.async {
17-
do {
18-
let allAccounts = try self.web3.wallet.getAccounts()
19-
promise.resolver.fulfill(allAccounts)
20-
} catch {
21-
promise.resolver.reject(error)
22-
}
23-
}
24-
return promise.promise
12+
public func getAccountsPromise() async throws -> [EthereumAddress] {
13+
14+
guard self.web3.provider.attachedKeystoreManager == nil else {
15+
return try self.web3.wallet.getAccounts()
2516
}
17+
18+
2619
let request = JSONRPCRequestFabric.prepareRequest(.getAccounts, parameters: [])
27-
let rp = web3.dispatch(request)
28-
return rp.map(on: queue) { response in
29-
guard let value: [EthereumAddress] = response.getValue() else {
30-
if response.error != nil {
31-
throw Web3Error.nodeError(desc: response.error!.message)
32-
}
33-
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
20+
let response = try await web3.dispatch(request)
21+
22+
guard let value: [EthereumAddress] = response.getValue() else {
23+
if response.error != nil {
24+
throw Web3Error.nodeError(desc: response.error!.message)
3425
}
35-
return value
26+
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
3627
}
28+
return value
29+
3730
}
3831
}

Sources/web3swift/Promises/Promise+Web3+Eth+GetBlockNumber.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import BigInt
99

1010

1111
extension web3.Eth {
12-
public func getBlockNumberPromise() -> Promise<BigUInt> {
12+
public func getBlockNumberPromise() async throws -> BigUInt {
1313
let request = JSONRPCRequestFabric.prepareRequest(.blockNumber, parameters: [])
14-
let rp = web3.dispatch(request)
15-
let queue = web3.requestDispatcher.queue
16-
return rp.map(on: queue) { response in
17-
guard let value: BigUInt = response.getValue() else {
18-
if response.error != nil {
19-
throw Web3Error.nodeError(desc: response.error!.message)
20-
}
21-
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
14+
let response = try await web3.dispatch(request)
15+
16+
guard let value: BigUInt = response.getValue() else {
17+
if response.error != nil {
18+
throw Web3Error.nodeError(desc: response.error!.message)
2219
}
23-
return value
20+
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
2421
}
22+
return value
23+
24+
2525
}
2626
}

Sources/web3swift/Promises/Promise+Web3+Eth+GetGasPrice.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ import BigInt
99

1010

1111
extension web3.Eth {
12-
public func getGasPricePromise() -> Promise<BigUInt> {
12+
public func getGasPricePromise() async throws -> BigUInt {
1313
let request = JSONRPCRequestFabric.prepareRequest(.gasPrice, parameters: [])
14-
let rp = web3.dispatch(request)
15-
let queue = web3.requestDispatcher.queue
16-
return rp.map(on: queue) { response in
17-
guard let value: BigUInt = response.getValue() else {
18-
if response.error != nil {
19-
throw Web3Error.nodeError(desc: response.error!.message)
20-
}
21-
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
14+
let response = try await web3.dispatch(request)
15+
16+
guard let value: BigUInt = response.getValue() else {
17+
if response.error != nil {
18+
throw Web3Error.nodeError(desc: response.error!.message)
2219
}
23-
return value
20+
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
2421
}
22+
return value
23+
2524
}
2625
}

Sources/web3swift/Promises/Promise+Web3+Eth+GetTransactionReceipt.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@ import BigInt
99

1010

1111
extension web3.Eth {
12-
public func getTransactionReceiptPromise(_ txhash: Data) -> Promise<TransactionReceipt> {
12+
public func getTransactionReceiptPromise(_ txhash: Data) async throws -> TransactionReceipt {
1313
let hashString = txhash.toHexString().addHexPrefix()
14-
return self.getTransactionReceiptPromise(hashString)
14+
return try await self.getTransactionReceiptPromise(hashString)
1515
}
1616

17-
public func getTransactionReceiptPromise(_ txhash: String) -> Promise<TransactionReceipt> {
17+
public func getTransactionReceiptPromise(_ txhash: String) async throws -> TransactionReceipt {
1818
let request = JSONRPCRequestFabric.prepareRequest(.getTransactionReceipt, parameters: [txhash])
19-
let rp = web3.dispatch(request)
20-
let queue = web3.requestDispatcher.queue
21-
return rp.map(on: queue) { response in
22-
guard let value: TransactionReceipt = response.getValue() else {
23-
if response.error != nil {
24-
throw Web3Error.nodeError(desc: response.error!.message)
25-
}
26-
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
19+
let response = try await web3.dispatch(request)
20+
21+
guard let value: TransactionReceipt = response.getValue() else {
22+
if response.error != nil {
23+
throw Web3Error.nodeError(desc: response.error!.message)
2724
}
28-
return value
25+
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
2926
}
27+
return value
28+
3029
}
3130
}

Sources/web3swift/Web3/Web3+EventParser.swift

Lines changed: 46 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extension web3.web3contract {
3737

3838
*/
3939
public func parseBlockByNumber(_ blockNumber: UInt64) throws -> [EventParserResultProtocol] {
40-
let result = try self.parseBlockByNumberPromise(blockNumber).wait()
40+
let result = try self.parseBlockByNumberPromise(blockNumber)
4141
return result
4242
}
4343

@@ -54,7 +54,7 @@ extension web3.web3contract {
5454

5555
*/
5656
public func parseBlock(_ block: Block) throws -> [EventParserResultProtocol] {
57-
let result = try self.parseBlockPromise(block).wait()
57+
let result = try self.parseBlockPromise(block)
5858
return result
5959
}
6060

@@ -71,7 +71,7 @@ extension web3.web3contract {
7171

7272
*/
7373
public func parseTransactionByHash(_ hash: Data) throws -> [EventParserResultProtocol] {
74-
let result = try self.parseTransactionByHashPromise(hash).wait()
74+
let result = try self.parseTransactionByHashPromise(hash)
7575
return result
7676
}
7777

@@ -88,82 +88,61 @@ extension web3.web3contract {
8888

8989
*/
9090
public func parseTransaction(_ transaction: EthereumTransaction) throws -> [EventParserResultProtocol] {
91-
let result = try self.parseTransactionPromise(transaction).wait()
91+
let result = try self.parseTransactionPromise(transaction)
9292
return result
9393
}
9494
}
9595
}
9696

9797
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")
110101
}
102+
return try self.parseTransactionByHashPromise(hash)
111103
}
112104

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")
120110
}
111+
return results
112+
121113
}
122114

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")
138119
}
120+
121+
let res = try await self.web3.eth.getBlockByNumberPromise(blockNumber)
122+
123+
return try self.parseBlockPromise(res)
139124
}
140125

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]()
166135
}
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+
167146
return Promise {seal in
168147

169148
var pendingEvents: [Promise<[EventParserResultProtocol]>] = [Promise<[EventParserResultProtocol]>]()
@@ -184,6 +163,7 @@ extension web3.web3contract.EventParser {
184163
pendingEvents.append(subresultPromise)
185164
}
186165
}
166+
187167
when(resolved: pendingEvents).done(on: queue){ (results: [PromiseResult<[EventParserResultProtocol]>]) throws in
188168
var allResults = [EventParserResultProtocol]()
189169
for res in results {
@@ -193,22 +173,12 @@ extension web3.web3contract.EventParser {
193173
allResults.append(contentsOf: subresult)
194174
}
195175
seal.fulfill(allResults)
196-
}.catch(on: queue) {err in
176+
}
177+
.catch(on: queue) {err in
197178
seal.reject(err)
198179
}
199180
}
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+
212182
}
213183

214184
}

0 commit comments

Comments
 (0)