Skip to content

Commit 3126c04

Browse files
Merge pull request #515 from pharms-eth/remove-Promises
2 parents 8b5e77e + d37ad4d commit 3126c04

File tree

89 files changed

+2087
-3016
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+2087
-3016
lines changed

Cartfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ github "attaswift/BigInt" ~> 5.3.0
22
github "attaswift/SipHash" ~> 1.2.2
33
github "daltoniam/Starscream" ~> 4.0.4
44
github "krzyzanowskim/CryptoSwift" ~> 1.4.2
5-
github "mxcl/PromiseKit" ~> 6.16.2

Cartfile.resolved

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
github "attaswift/BigInt" "v5.3.0"
22
github "attaswift/SipHash" "v1.2.2"
33
github "daltoniam/Starscream" "4.0.4"
4-
github "krzyzanowskim/CryptoSwift" "1.4.2"
5-
github "mxcl/PromiseKit" "6.16.2"
4+
github "krzyzanowskim/CryptoSwift" "1.5.0"

Documentation/web3swift 2.0 Migration Guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ let contract = web3.contract(Web3.Utils.erc20ABI, at: contractAddress, abiVersio
274274
let userAddress = EthereumAddress("<address>")!
275275
guard let readTX = contract?.read("balanceOf", parameters: [addressOfUser] as [AnyObject]) else {return}
276276
readTX.transactionOptions.from = EthereumAddress("<address>")!
277-
let tokenBalance = try readTX.callPromise().wait()
277+
let tokenBalance = try readTX.callPromise()
278278
guard let balance = tokenBalance["0"] as? BigUInt else {return}
279279
```
280280

Package.resolved

100755100644
Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.4
1+
// swift-tools-version: 5.5.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
@@ -14,23 +14,22 @@ let excludeFiles: String = []
1414
let package = Package(
1515
name: "Web3swift",
1616
platforms: [
17-
.macOS(.v10_12), .iOS(.v11)
17+
.macOS(.v10_15), .iOS(.v13)
1818
],
1919
products: [
2020
.library(name: "web3swift", targets: ["web3swift"])
2121
],
2222

2323
dependencies: [
2424
.package(url: "https://github.com/attaswift/BigInt.git", from: "5.3.0"),
25-
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.16.2"),
2625
.package(url: "https://github.com/daltoniam/Starscream.git", from: "4.0.4"),
2726
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .exact("1.4.3"))
2827
],
2928
targets: [
3029
.target(name: "secp256k1"),
3130
.target(
3231
name: "web3swift",
33-
dependencies: ["BigInt", "secp256k1", "PromiseKit", "Starscream", "CryptoSwift"],
32+
dependencies: ["BigInt", "secp256k1", "Starscream", "CryptoSwift"],
3433
exclude: excludeFiles,
3534
resources: [
3635
.copy("./Browser/browser.js"),

Sources/Linting.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/web3swift/Browser/BrowserViewController.swift

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,17 @@ open class BrowserViewController: UIViewController {
6363
}, for: "getRPCurl")
6464

6565
self.webView.bridge.register({ (parameters, completion) in
66-
let allAccounts = web3.browserFunctions.getAccounts()
67-
completion(.success(["accounts": allAccounts as Any]))
66+
Task {
67+
let allAccounts = await web3.browserFunctions.getAccounts()
68+
completion(.success(["accounts": allAccounts as Any]))
69+
}
6870
}, for: "eth_getAccounts")
6971

7072
self.webView.bridge.register({ (parameters, completion) in
71-
let coinbase = web3.browserFunctions.getCoinbase()
72-
completion(.success(["coinbase": coinbase as Any]))
73+
Task {
74+
let coinbase = await web3.browserFunctions.getCoinbase()
75+
completion(.success(["coinbase": coinbase as Any]))
76+
}
7377
}, for: "eth_coinbase")
7478

7579
self.webView.bridge.register({ (parameters, completion) in
@@ -97,21 +101,23 @@ open class BrowserViewController: UIViewController {
97101
}, for: "eth_sign")
98102

99103
self.webView.bridge.register({ (parameters, completion) in
100-
if parameters == nil {
101-
completion(.failure(Bridge.JSError(code: 0, description: "No parameters provided")))
102-
return
103-
}
104-
let transaction = parameters!["transaction"] as? [String: Any]
105-
if transaction == nil {
106-
completion(.failure(Bridge.JSError(code: 0, description: "Not enough parameters provided")))
107-
return
108-
}
109-
let result = web3.browserFunctions.signTransaction(transaction!)
110-
if result == nil {
111-
completion(.failure(Bridge.JSError(code: 0, description: "Data is invalid")))
112-
return
104+
Task {
105+
if parameters == nil {
106+
completion(.failure(Bridge.JSError(code: 0, description: "No parameters provided")))
107+
return
108+
}
109+
let transaction = parameters!["transaction"] as? [String: Any]
110+
if transaction == nil {
111+
completion(.failure(Bridge.JSError(code: 0, description: "Not enough parameters provided")))
112+
return
113+
}
114+
let result = await web3.browserFunctions.signTransaction(transaction!)
115+
if result == nil {
116+
completion(.failure(Bridge.JSError(code: 0, description: "Data is invalid")))
117+
return
118+
}
119+
completion(.success(["signedTransaction": result as Any]))
113120
}
114-
completion(.success(["signedTransaction": result as Any]))
115121
}, for: "eth_signTransaction")
116122
}
117123
}

Sources/web3swift/HookedFunctions/Web3+BrowserFunctions.swift

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

1010
extension web3.BrowserFunctions {
1111

12-
public func getAccounts() -> [String]? {
12+
public func getAccounts() async -> [String]? {
1313
do {
14-
let accounts = try self.web3.eth.getAccounts()
14+
let accounts = try await self.web3.eth.getAccounts()
1515
return accounts.compactMap({$0.address})
1616
} catch {
1717
return [String]()
1818
}
1919
}
2020

21-
public func getCoinbase() -> String? {
22-
guard let addresses = self.getAccounts() else {return nil}
21+
public func getCoinbase() async -> String? {
22+
guard let addresses = await self.getAccounts() else {return nil}
2323
guard addresses.count > 0 else {return nil}
2424
return addresses[0]
2525
}
@@ -69,7 +69,7 @@ extension web3.BrowserFunctions {
6969
return Web3.Utils.publicToAddressString(publicKey)
7070
}
7171

72-
public func sendTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") -> [String: Any]? {
72+
public func sendTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") async -> [String: Any]? {
7373
do {
7474
let jsonData: Data = try JSONSerialization.data(withJSONObject: transactionJSON, options: [])
7575
let transaction: EthereumTransaction = try JSONDecoder().decode(EthereumTransaction.self, from: jsonData)
@@ -80,20 +80,20 @@ extension web3.BrowserFunctions {
8080
transactionOptions.value = options.value ?? 0
8181
transactionOptions.gasLimit = options.gasLimit ?? .automatic
8282
transactionOptions.gasPrice = options.gasPrice ?? .automatic
83-
return self.sendTransaction(transaction, transactionOptions: transactionOptions, password: password)
83+
return await self.sendTransaction(transaction, transactionOptions: transactionOptions, password: password)
8484
} catch { return nil }
8585
}
8686

87-
public func sendTransaction(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") -> [String: Any]? {
87+
public func sendTransaction(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") async -> [String: Any]? {
8888
do {
89-
let result = try self.web3.eth.sendTransaction(transaction, transactionOptions: transactionOptions, password: password)
89+
let result = try await self.web3.eth.sendTransaction(transaction, transactionOptions: transactionOptions, password: password)
9090
return ["txhash": result.hash]
9191
} catch {
9292
return nil
9393
}
9494
}
9595

96-
public func estimateGas(_ transactionJSON: [String: Any]) -> BigUInt? {
96+
public func estimateGas(_ transactionJSON: [String: Any]) async -> BigUInt? {
9797
do {
9898
let jsonData: Data = try JSONSerialization.data(withJSONObject: transactionJSON, options: [])
9999
let transaction: EthereumTransaction = try JSONDecoder().decode(EthereumTransaction.self, from: jsonData)
@@ -104,40 +104,41 @@ extension web3.BrowserFunctions {
104104
transactionOptions.value = options.value ?? 0
105105
transactionOptions.gasLimit = .automatic
106106
transactionOptions.gasPrice = options.gasPrice ?? .automatic
107-
return self.estimateGas(transaction, transactionOptions: transactionOptions)
107+
return await self.estimateGas(transaction, transactionOptions: transactionOptions)
108108
} catch { return nil }
109109
}
110110

111-
public func estimateGas(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions) -> BigUInt? {
111+
public func estimateGas(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions) async -> BigUInt? {
112112
do {
113-
let result = try self.web3.eth.estimateGas(transaction, transactionOptions: transactionOptions)
113+
let result = try await self.web3.eth.estimateGas(transaction, transactionOptions: transactionOptions)
114114
return result
115115
} catch {
116116
return nil
117117
}
118118
}
119119

120-
public func prepareTxForApproval(_ transactionJSON: [String: Any]) -> (transaction: EthereumTransaction?, options: TransactionOptions?) {
120+
public func prepareTxForApproval(_ transactionJSON: [String: Any]) async -> (transaction: EthereumTransaction?, options: TransactionOptions?) {
121121
do {
122122
let jsonData: Data = try JSONSerialization.data(withJSONObject: transactionJSON, options: [])
123123
let transaction: EthereumTransaction = try JSONDecoder().decode(EthereumTransaction.self, from: jsonData)
124124
let options: TransactionOptions = try JSONDecoder().decode(TransactionOptions.self, from: jsonData)
125-
return try self.prepareTxForApproval(transaction, options: options)
125+
return try await self.prepareTxForApproval(transaction, options: options)
126126
} catch {
127127
return (nil, nil)
128128
}
129129
}
130130

131-
public func prepareTxForApproval(_ trans: EthereumTransaction, options opts: TransactionOptions) throws -> (transaction: EthereumTransaction?, options: TransactionOptions?) {
131+
public func prepareTxForApproval(_ trans: EthereumTransaction, options opts: TransactionOptions) async throws -> (transaction: EthereumTransaction?, options: TransactionOptions?) {
132132
do {
133133
var transaction = trans
134134
var options = opts
135135
guard let _ = options.from else {return (nil, nil)}
136-
let gasPrice = try self.web3.eth.getGasPrice()
136+
let gasPrice = try await self.web3.eth.getGasPrice()
137137
transaction.parameters.gasPrice = gasPrice
138138
options.gasPrice = .manual(gasPrice)
139-
guard let gasEstimate = self.estimateGas(transaction, transactionOptions: options) else {return (nil, nil)}
139+
guard let gasEstimate = await self.estimateGas(transaction, transactionOptions: options) else {return (nil, nil)}
140140
transaction.parameters.gasLimit = gasEstimate
141+
141142
options.gasLimit = .limited(gasEstimate)
142143
print(transaction)
143144
return (transaction, options)
@@ -146,7 +147,7 @@ extension web3.BrowserFunctions {
146147
}
147148
}
148149

149-
public func signTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") -> String? {
150+
public func signTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") async -> String? {
150151
do {
151152
let jsonData: Data = try JSONSerialization.data(withJSONObject: transactionJSON, options: [])
152153
let transaction: EthereumTransaction = try JSONDecoder().decode(EthereumTransaction.self, from: jsonData)
@@ -162,11 +163,11 @@ extension web3.BrowserFunctions {
162163
} else {
163164
transactionOptions.nonce = .pending
164165
}
165-
return self.signTransaction(transaction, transactionOptions: transactionOptions, password: password)
166+
return await self.signTransaction(transaction, transactionOptions: transactionOptions, password: password)
166167
} catch { return nil }
167168
}
168169

169-
public func signTransaction(_ trans: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") -> String? {
170+
public func signTransaction(_ trans: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") async -> String? {
170171
do {
171172
var transaction = trans
172173
guard let from = transactionOptions.from else {return nil}
@@ -178,23 +179,23 @@ extension web3.BrowserFunctions {
178179
case .manual(let gasPrice):
179180
transaction.parameters.gasPrice = gasPrice
180181
default:
181-
let gasPrice = try self.web3.eth.getGasPrice()
182+
let gasPrice = try await self.web3.eth.getGasPrice()
182183
transaction.parameters.gasPrice = gasPrice
183184
}
184185

185186
switch gasLimitPolicy {
186187
case .manual(let gasLimit):
187188
transaction.parameters.gasLimit = gasLimit
188189
default:
189-
let gasLimit = try self.web3.eth.estimateGas(transaction, transactionOptions: transactionOptions)
190+
let gasLimit = try await self.web3.eth.estimateGas(transaction, transactionOptions: transactionOptions)
190191
transaction.parameters.gasLimit = gasLimit
191192
}
192193

193194
switch noncePolicy {
194195
case .manual(let nonce):
195196
transaction.nonce = nonce
196197
default:
197-
let nonce = try self.web3.eth.getTransactionCount(address: from, onBlock: "pending")
198+
let nonce = try await self.web3.eth.getTransactionCount(address: from, onBlock: "pending")
198199
transaction.nonce = nonce
199200
}
200201

0 commit comments

Comments
 (0)