Skip to content

Commit 2695368

Browse files
authored
Merge pull request #114 from BANKEX/develop
Bridge synchronous methods to async onces
2 parents fe44f97 + ac9a797 commit 2695368

25 files changed

+926
-455
lines changed

Example/web3swiftExample/web3swiftExample/ViewController.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,19 @@ class ViewController: UIViewController {
117117
print(err)
118118
}
119119

120-
120+
//Send ERC20 token on Rinkeby
121121
guard case .success(let gasPriceRinkeby) = web3Rinkeby.eth.getGasPrice() else {return}
122122
web3Rinkeby.addKeystoreManager(keystoreManager)
123123
var tokenTransferOptions = Web3Options.defaultOptions()
124124
tokenTransferOptions.gasPrice = gasPriceRinkeby
125125
tokenTransferOptions.from = ks?.addresses?.first!
126126
let testToken = web3Rinkeby.contract(Web3.Utils.erc20ABI, at: EthereumAddress("0xa407dd0cbc9f9d20cdbd557686625e586c85b20a"), abiVersion: 2)!
127127
let intermediateForTokenTransfer = testToken.method("transfer", parameters: [EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B"), BigUInt(1)] as [AnyObject], options: tokenTransferOptions)!
128-
let tokenTransferResult = intermediateForTokenTransfer.send(password: "BANKEXFOUNDATION")
128+
let gasEstimateResult = intermediateForTokenTransfer.estimateGas(options: nil)
129+
guard case .success(let gasEstimate) = gasEstimateResult else {return}
130+
var optionsWithCustomGasLimit = Web3Options()
131+
optionsWithCustomGasLimit.gasLimit = gasEstimate
132+
let tokenTransferResult = intermediateForTokenTransfer.send(password: "BANKEXFOUNDATION", options: optionsWithCustomGasLimit)
129133
switch tokenTransferResult {
130134
case .success(let res):
131135
print("Token transfer successful")

web3swift/ABI/Classes/ABIEncoder.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ extension ABIElement.ParameterType.StaticType {
4848
}
4949
case .address:
5050
if let string = value as? String {
51-
let address = EthereumAddress(string)
52-
guard address.isValid else {return nil}
51+
guard let address = EthereumAddress(string) else {return nil}
5352
let data = address.addressData
5453
return data.setLengthLeft(32)
5554
} else if let address = value as? EthereumAddress {

web3swift/ABIv2/Classes/ABIv2Encoding.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ extension ABIv2Encoder {
200200
}
201201
case .address:
202202
if let string = value as? String {
203-
let address = EthereumAddress(string)
204-
guard address.isValid else {break}
203+
guard let address = EthereumAddress(string) else {return nil}
205204
let data = address.addressData
206205
return data.setLengthLeft(32)
207206
} else if let address = value as? EthereumAddress {

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+ConversionOperations.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ final class AddressArrayConversionOperation: Web3Operation {
153153
}
154154
var toReturn = [EthereumAddress]()
155155
for addrString in resultArray {
156-
let addr = EthereumAddress(addrString)
156+
guard let addr = EthereumAddress(addrString) else {
157+
return processError(Web3Error.dataError)
158+
}
157159
if (addr.isValid) {
158160
toReturn.append(addr)
159161
}

web3swift/Concurrency/Classes/Web3+EthOperations.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ final class GetTransactionCountOperation: Web3Operation {
5656
guard input.count == 2 else {return processError(Web3Error.inputError("Invalid input supplied"))}
5757
guard let address = input[0] as? String else {return processError(Web3Error.inputError("Invalid input supplied"))}
5858
guard let onBlock = input[1] as? String else {return processError(Web3Error.inputError("Invalid input supplied"))}
59-
guard EthereumAddress(address).isValid else {return processError(Web3Error.inputError("Invalid input supplied"))}
59+
guard let _ = EthereumAddress(address) else {return processError(Web3Error.inputError("Invalid input supplied"))}
6060
let request = JSONRPCRequestFabric.prepareRequest(.getTransactionCount, parameters: [address, onBlock])
6161
let dataOp = DataFetchOperation(self.web3, queue: self.expectedQueue)
6262
dataOp.inputData = request as AnyObject
@@ -87,7 +87,7 @@ final class GetBalanceOperation: Web3Operation {
8787
guard input.count == 2 else {return processError(Web3Error.inputError("Invalid input supplied"))}
8888
guard let address = input[0] as? String else {return processError(Web3Error.inputError("Invalid input supplied"))}
8989
guard let onBlock = input[1] as? String else {return processError(Web3Error.inputError("Invalid input supplied"))}
90-
guard EthereumAddress(address).isValid else {return processError(Web3Error.inputError("Invalid input supplied"))}
90+
guard let _ = EthereumAddress(address) else {return processError(Web3Error.inputError("Invalid input supplied"))}
9191
let request = JSONRPCRequestFabric.prepareRequest(.getBalance, parameters: [address, onBlock])
9292
let dataOp = DataFetchOperation(self.web3, queue: self.expectedQueue)
9393
dataOp.inputData = request as AnyObject
@@ -150,7 +150,8 @@ final class EstimateGasOperation: Web3Operation {
150150
guard let transaction = input[0] as? EthereumTransaction else {return processError(Web3Error.inputError("Invalid input supplied"))}
151151
let options = input[1] as? Web3Options
152152
guard let onBlock = input[2] as? String else {return processError(Web3Error.inputError("Invalid input supplied"))}
153-
let mergedOptions = Web3Options.merge(Web3Options.defaultOptions(), with: options)
153+
var mergedOptions = Web3Options.merge(Web3Options.defaultOptions(), with: options)
154+
mergedOptions?.gasLimit = nil
154155
guard let request = EthereumTransaction.createRequest(method: JSONRPCmethod.estimateGas, transaction: transaction, onBlock: onBlock, options: mergedOptions) else {return processError(Web3Error.inputError("Invalid input supplied"))}
155156
let dataOp = DataFetchOperation(self.web3, queue: self.expectedQueue)
156157
dataOp.inputData = request as AnyObject

web3swift/Concurrency/Classes/Web3+NativePrimitivesConversionOperations.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ final class TransactionReceiptConversionOperation: Web3Operation {
2929
}
3030
}
3131

32+
final class TransactionDetailsConversionOperation: Web3Operation {
33+
34+
override func main() {
35+
if (error != nil) {
36+
return self.processError(self.error!)
37+
}
38+
guard let _ = self.next else {return processError(Web3Error.inputError("Invalid input supplied"))}
39+
if inputData == nil || inputData! is NSNull {
40+
return processSuccess(TransactionReceipt.notProcessed(transactionHash: Data()) as AnyObject)
41+
}
42+
guard inputData != nil else {return processError(Web3Error.inputError("Invalid input supplied"))}
43+
guard let input = inputData! as? [String: AnyObject] else {return processError(Web3Error.dataError)}
44+
guard let receipt = TransactionDetails(input) else {
45+
return processError(Web3Error.dataError)
46+
}
47+
return processSuccess(receipt as AnyObject)
48+
}
49+
}
50+
3251
final class BlockConversionOperation: Web3Operation {
3352

3453
override func main() {

web3swift/Concurrency/Classes/Web3+PersonalOperations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class PersonalUnlockAccountOperation: Web3Operation {
3030
guard let address = input[0] as? String else {return processError(Web3Error.inputError("Invalid input supplied"))}
3131
guard let password = input[1] as? String else {return processError(Web3Error.inputError("Invalid input supplied"))}
3232
guard let seconds = input[2] as? UInt64 else {return processError(Web3Error.inputError("Invalid input supplied"))}
33-
guard EthereumAddress(address).isValid else {return processError(Web3Error.inputError("Invalid input supplied"))}
33+
guard let _ = EthereumAddress(address) else {return processError(Web3Error.inputError("Invalid input supplied"))}
3434
let request = JSONRPCRequestFabric.prepareRequest(.unlockAccount, parameters: [address, password, seconds])
3535
let dataOp = DataFetchOperation(self.web3, queue: self.expectedQueue)
3636
dataOp.inputData = request as AnyObject

web3swift/Concurrency/Classes/Web3+TransactionAndBlockDetailsOperations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class GetTransactionDetailsOperation: Web3Operation {
3131
dataOp.inputData = request as AnyObject
3232
let convOp = DictionaryConversionOperation(self.web3, queue: self.expectedQueue)
3333
dataOp.next = OperationChainingType.operation(convOp)
34-
let parsingOp = TransactionReceiptConversionOperation(self.web3, queue: self.expectedQueue)
34+
let parsingOp = TransactionDetailsConversionOperation(self.web3, queue: self.expectedQueue)
3535
convOp.next = OperationChainingType.operation(parsingOp)
3636
parsingOp.next = completion
3737
self.expectedQueue.addOperation(dataOp)

0 commit comments

Comments
 (0)