Skip to content

Commit 06b0acf

Browse files
author
Alex Vlasov
committed
cleanups
use async calls with opaque batching for most of the calls
1 parent eee45a7 commit 06b0acf

File tree

5 files changed

+177
-94
lines changed

5 files changed

+177
-94
lines changed

web3swift/Concurrency/Classes/Web3+Concurrency.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class OperationDispatcher {
2424
self.provider = provider
2525
self.queue = queue
2626
self.policy = policy
27-
self.lockQueue = DispatchQueue(label: "batchingQueue")
27+
self.lockQueue = DispatchQueue(label: "batchingQueue", qos: .userInitiated)
2828
}
2929

3030
struct Request {

web3swift/Concurrency/Classes/Web3+TransactionOperations.swift

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@ import Result
1111
import BigInt
1212

1313
final class ContractCallOperation: Web3Operation {
14-
var intermediate: TransactionIntermediate?
1514
var method: String?
1615

17-
convenience init?(_ web3Instance: web3, queue: OperationQueue? = nil, intermediate: TransactionIntermediate, onBlock: String = "latest") {
18-
self.init(web3Instance, queue: queue, inputData: [intermediate.transaction, intermediate.options as Any, onBlock] as AnyObject)
19-
self.intermediate = intermediate
16+
convenience init?(_ web3Instance: web3, queue: OperationQueue? = nil, intermediate: TransactionIntermediate, onBlock: String = "latest", options: Web3Options? = nil) {
17+
self.init(web3Instance, queue: queue, inputData: [intermediate, options as Any, onBlock] as AnyObject)
2018
self.method = intermediate.method
2119
}
2220

23-
convenience init?(_ web3Instance: web3, queue: OperationQueue? = nil, contract: web3.web3contract, method: String = "fallback", parameters: [AnyObject] = [], extraData: Data = Data(), options: Web3Options?, onBlock: String = "latest") {
21+
convenience init?(_ web3Instance: web3, queue: OperationQueue? = nil, contract: web3.web3contract, method: String = "fallback", parameters: [AnyObject] = [], extraData: Data = Data(), options: Web3Options? = nil, onBlock: String = "latest") {
2422
guard let intermediate = contract.method(method, parameters: parameters, extraData: extraData, options: options) else {return nil}
25-
self.init(web3Instance, queue: queue, inputData: [intermediate.transaction, intermediate.options as Any, onBlock] as AnyObject)
26-
self.intermediate = intermediate
23+
self.init(web3Instance, queue: queue, inputData: [intermediate, options as Any, onBlock] as AnyObject)
2724
self.method = method
2825
}
2926

@@ -34,17 +31,20 @@ final class ContractCallOperation: Web3Operation {
3431
guard let _ = self.next else {return processError(Web3Error.inputError("Invalid input supplied"))}
3532
guard inputData != nil else {return processError(Web3Error.inputError("Invalid input supplied"))}
3633
guard let input = inputData! as? [AnyObject] else {return processError(Web3Error.inputError("Invalid input supplied"))}
34+
3735
guard input.count == 3 else {return processError(Web3Error.inputError("Invalid input supplied"))}
38-
guard let transaction = input[0] as? EthereumTransaction else {return processError(Web3Error.inputError("Invalid input supplied"))}
36+
guard let intermediate = input[0] as? TransactionIntermediate else {return processError(Web3Error.inputError("Invalid input supplied"))}
3937
let options = input[1] as? Web3Options
4038
guard let onBlock = input[2] as? String else {return processError(Web3Error.inputError("Invalid input supplied"))}
41-
let mergedOptions = Web3Options.merge(Web3Options.defaultOptions(), with: options)
42-
guard let request = EthereumTransaction.createRequest(method: JSONRPCmethod.call, transaction: transaction, onBlock: onBlock, options: mergedOptions) else {return processError(Web3Error.inputError("Invalid input supplied"))}
43-
let dataOp = DataFetchOperation(self.web3, queue: self.expectedQueue)
44-
dataOp.inputData = request as AnyObject
45-
let parsingOp = DataConversionOperation(self.web3, queue: self.expectedQueue)
46-
dataOp.next = OperationChainingType.operation(parsingOp)
47-
let callback = { (res: Result<AnyObject, Web3Error>) -> () in
39+
guard var mergedOptions = Web3Options.merge(intermediate.options, with: options) else {return processError(Web3Error.inputError("Invalid input supplied"))}
40+
mergedOptions.gasLimit = nil
41+
if (options?.gasLimit != nil) {
42+
mergedOptions.gasLimit = options?.gasLimit
43+
}
44+
guard let _ = mergedOptions.from else {return processError(Web3Error.inputError("Invalid input supplied"))}
45+
let transaction = intermediate.transaction
46+
47+
let parsingCallback = { (res: Result<AnyObject, Web3Error>) -> () in
4848
switch res {
4949
case .success(let result):
5050
guard let resultData = result as? Data else {
@@ -57,9 +57,6 @@ final class ContractCallOperation: Web3Operation {
5757
guard let method = self.method else {
5858
return self.processError(Web3Error.dataError)
5959
}
60-
guard let intermediate = self.intermediate else {
61-
return self.processError(Web3Error.dataError)
62-
}
6360
guard let decodedData = intermediate.contract.decodeReturnData(method, data: resultData) else
6461
{
6562
return self.processError(Web3Error.dataError)
@@ -69,8 +66,49 @@ final class ContractCallOperation: Web3Operation {
6966
return self.processError(error)
7067
}
7168
}
72-
parsingOp.next = OperationChainingType.callback(callback, self.expectedQueue)
69+
70+
guard let request = EthereumTransaction.createRequest(method: JSONRPCmethod.call, transaction: transaction, onBlock: onBlock, options: mergedOptions) else {return self.processError(Web3Error.inputError("Invalid input supplied"))}
71+
let dataOp = DataFetchOperation(self.web3, queue: self.expectedQueue)
72+
dataOp.inputData = request as AnyObject
73+
let parsingOp = DataConversionOperation(self.web3, queue: self.expectedQueue)
74+
dataOp.next = OperationChainingType.operation(parsingOp)
75+
parsingOp.next = OperationChainingType.callback(parsingCallback, self.expectedQueue)
7376
self.expectedQueue.addOperation(dataOp)
77+
78+
// let gasEstimationCallback = { (res: Result<AnyObject, Web3Error>) -> () in
79+
// switch res {
80+
// case .success(let result):
81+
// guard let gasEstimate = result as? BigUInt else {
82+
// return self.processError(Web3Error.dataError)
83+
// }
84+
//// if mergedOptions.gasLimit == nil {
85+
//// mergedOptions.gasLimit = gasEstimate
86+
//// } else {
87+
//// if (mergedOptions.gasLimit! < gasEstimate) {
88+
//// if (options?.gasLimit != nil && options!.gasLimit != nil && options!.gasLimit! >= gasEstimate) {
89+
//// mergedOptions.gasLimit = options!.gasLimit!
90+
//// } else {
91+
//// return self.processError(Web3Error.inputError("Estimated gas is larger than the gas limit"))
92+
//// }
93+
//// }
94+
//// }
95+
// guard let request = EthereumTransaction.createRequest(method: JSONRPCmethod.call, transaction: transaction, onBlock: onBlock, options: mergedOptions) else {return self.processError(Web3Error.inputError("Invalid input supplied"))}
96+
// let dataOp = DataFetchOperation(self.web3, queue: self.expectedQueue)
97+
// dataOp.inputData = request as AnyObject
98+
// let parsingOp = DataConversionOperation(self.web3, queue: self.expectedQueue)
99+
// dataOp.next = OperationChainingType.operation(parsingOp)
100+
// parsingOp.next = OperationChainingType.callback(parsingCallback, self.expectedQueue)
101+
// self.expectedQueue.addOperation(dataOp)
102+
// return
103+
// case .failure(let error):
104+
// return self.processError(error)
105+
// }
106+
// }
107+
//
108+
// guard let gasEstimateOperation = ContractEstimateGasOperation.init(self.web3, queue: self.expectedQueue, intermediate: intermediate, onBlock: onBlock) else {return self.processError(Web3Error.dataError)}
109+
// gasEstimateOperation.next = OperationChainingType.callback(gasEstimationCallback, self.expectedQueue)
110+
// self.expectedQueue.addOperation(gasEstimateOperation)
111+
74112
}
75113
}
76114

@@ -140,7 +178,7 @@ final class ContractSendOperation: Web3Operation {
140178
} else {
141179
if (mergedOptions.gasLimit! < gasEstimate) {
142180
if (options?.gasLimit != nil && options!.gasLimit != nil && options!.gasLimit! >= gasEstimate) {
143-
mergedOptions.gasLimit = gasEstimate
181+
mergedOptions.gasLimit = options!.gasLimit!
144182
} else {
145183
return self.processError(Web3Error.inputError("Estimated gas is larger than the gas limit"))
146184
}

web3swift/KeystoreManager/Classes/EthereumAddress.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public struct EthereumAddress: Equatable {
7979
case .normal:
8080
guard let data = Data.fromHex(addressString) else {return nil}
8181
guard data.count == 20 else {return nil}
82-
if (!ignoreChecksum && data.toHexString().addHexPrefix() != addressString.lowercased()) {
82+
if (!ignoreChecksum && data.toHexString().addHexPrefix() != addressString) {
8383
let checksummedAddress = EthereumAddress.toChecksumAddress(data.toHexString().addHexPrefix())
8484
guard checksummedAddress == addressString else {return nil}
8585
}

web3swift/Web3/Classes/Web3+HttpProvider.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class Web3HttpProvider: Web3Provider {
6161
}
6262
guard let response = self.syncPost(requests) else {return nil}
6363
guard let res = response as? [[String: AnyObject]?] else {return nil}
64+
// print(res)
6465
return res
6566
}
6667

0 commit comments

Comments
 (0)