@@ -11,19 +11,16 @@ import Result
11
11
import BigInt
12
12
13
13
final class ContractCallOperation : Web3Operation {
14
- var intermediate : TransactionIntermediate ?
15
14
var method : String ?
16
15
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 )
20
18
self . method = intermediate. method
21
19
}
22
20
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 " ) {
24
22
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 )
27
24
self . method = method
28
25
}
29
26
@@ -34,17 +31,20 @@ final class ContractCallOperation: Web3Operation {
34
31
guard let _ = self . next else { return processError ( Web3Error . inputError ( " Invalid input supplied " ) ) }
35
32
guard inputData != nil else { return processError ( Web3Error . inputError ( " Invalid input supplied " ) ) }
36
33
guard let input = inputData! as? [ AnyObject ] else { return processError ( Web3Error . inputError ( " Invalid input supplied " ) ) }
34
+
37
35
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 " ) ) }
39
37
let options = input [ 1 ] as? Web3Options
40
38
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
48
48
switch res {
49
49
case . success( let result) :
50
50
guard let resultData = result as? Data else {
@@ -57,9 +57,6 @@ final class ContractCallOperation: Web3Operation {
57
57
guard let method = self . method else {
58
58
return self . processError ( Web3Error . dataError)
59
59
}
60
- guard let intermediate = self . intermediate else {
61
- return self . processError ( Web3Error . dataError)
62
- }
63
60
guard let decodedData = intermediate. contract. decodeReturnData ( method, data: resultData) else
64
61
{
65
62
return self . processError ( Web3Error . dataError)
@@ -69,8 +66,49 @@ final class ContractCallOperation: Web3Operation {
69
66
return self . processError ( error)
70
67
}
71
68
}
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)
73
76
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
+
74
112
}
75
113
}
76
114
@@ -140,7 +178,7 @@ final class ContractSendOperation: Web3Operation {
140
178
} else {
141
179
if ( mergedOptions. gasLimit! < gasEstimate) {
142
180
if ( options? . gasLimit != nil && options!. gasLimit != nil && options!. gasLimit! >= gasEstimate) {
143
- mergedOptions. gasLimit = gasEstimate
181
+ mergedOptions. gasLimit = options! . gasLimit!
144
182
} else {
145
183
return self . processError ( Web3Error . inputError ( " Estimated gas is larger than the gas limit " ) )
146
184
}
0 commit comments