Skip to content

Commit

Permalink
Merge pull request #515 from pharms-eth/remove-Promises
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslavyaroslav committed Apr 27, 2022
2 parents 8b5e77e + d37ad4d commit 3126c04
Show file tree
Hide file tree
Showing 89 changed files with 2,087 additions and 3,016 deletions.
1 change: 0 additions & 1 deletion Cartfile
Expand Up @@ -2,4 +2,3 @@ github "attaswift/BigInt" ~> 5.3.0
github "attaswift/SipHash" ~> 1.2.2
github "daltoniam/Starscream" ~> 4.0.4
github "krzyzanowskim/CryptoSwift" ~> 1.4.2
github "mxcl/PromiseKit" ~> 6.16.2
3 changes: 1 addition & 2 deletions Cartfile.resolved
@@ -1,5 +1,4 @@
github "attaswift/BigInt" "v5.3.0"
github "attaswift/SipHash" "v1.2.2"
github "daltoniam/Starscream" "4.0.4"
github "krzyzanowskim/CryptoSwift" "1.4.2"
github "mxcl/PromiseKit" "6.16.2"
github "krzyzanowskim/CryptoSwift" "1.5.0"
2 changes: 1 addition & 1 deletion Documentation/web3swift 2.0 Migration Guide.md
Expand Up @@ -274,7 +274,7 @@ let contract = web3.contract(Web3.Utils.erc20ABI, at: contractAddress, abiVersio
let userAddress = EthereumAddress("<address>")!
guard let readTX = contract?.read("balanceOf", parameters: [addressOfUser] as [AnyObject]) else {return}
readTX.transactionOptions.from = EthereumAddress("<address>")!
let tokenBalance = try readTX.callPromise().wait()
let tokenBalance = try readTX.callPromise()
guard let balance = tokenBalance["0"] as? BigUInt else {return}
```

Expand Down
9 changes: 0 additions & 9 deletions Package.resolved 100755 → 100644
Expand Up @@ -19,15 +19,6 @@
"version": "1.4.3"
}
},
{
"package": "PromiseKit",
"repositoryURL": "https://github.com/mxcl/PromiseKit.git",
"state": {
"branch": null,
"revision": "3fd8c77ded8a4bbee548e3bd6c987ffe8c1e3574",
"version": "6.17.0"
}
},
{
"package": "Starscream",
"repositoryURL": "https://github.com/daltoniam/Starscream.git",
Expand Down
7 changes: 3 additions & 4 deletions Package.swift
@@ -1,4 +1,4 @@
// swift-tools-version: 5.4
// swift-tools-version: 5.5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -14,23 +14,22 @@ let excludeFiles: String = []
let package = Package(
name: "Web3swift",
platforms: [
.macOS(.v10_12), .iOS(.v11)
.macOS(.v10_15), .iOS(.v13)
],
products: [
.library(name: "web3swift", targets: ["web3swift"])
],

dependencies: [
.package(url: "https://github.com/attaswift/BigInt.git", from: "5.3.0"),
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.16.2"),
.package(url: "https://github.com/daltoniam/Starscream.git", from: "4.0.4"),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .exact("1.4.3"))
],
targets: [
.target(name: "secp256k1"),
.target(
name: "web3swift",
dependencies: ["BigInt", "secp256k1", "PromiseKit", "Starscream", "CryptoSwift"],
dependencies: ["BigInt", "secp256k1", "Starscream", "CryptoSwift"],
exclude: excludeFiles,
resources: [
.copy("./Browser/browser.js"),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 24 additions & 18 deletions Sources/web3swift/Browser/BrowserViewController.swift
Expand Up @@ -63,13 +63,17 @@ open class BrowserViewController: UIViewController {
}, for: "getRPCurl")

self.webView.bridge.register({ (parameters, completion) in
let allAccounts = web3.browserFunctions.getAccounts()
completion(.success(["accounts": allAccounts as Any]))
Task {
let allAccounts = await web3.browserFunctions.getAccounts()
completion(.success(["accounts": allAccounts as Any]))
}
}, for: "eth_getAccounts")

self.webView.bridge.register({ (parameters, completion) in
let coinbase = web3.browserFunctions.getCoinbase()
completion(.success(["coinbase": coinbase as Any]))
Task {
let coinbase = await web3.browserFunctions.getCoinbase()
completion(.success(["coinbase": coinbase as Any]))
}
}, for: "eth_coinbase")

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

self.webView.bridge.register({ (parameters, completion) in
if parameters == nil {
completion(.failure(Bridge.JSError(code: 0, description: "No parameters provided")))
return
}
let transaction = parameters!["transaction"] as? [String: Any]
if transaction == nil {
completion(.failure(Bridge.JSError(code: 0, description: "Not enough parameters provided")))
return
}
let result = web3.browserFunctions.signTransaction(transaction!)
if result == nil {
completion(.failure(Bridge.JSError(code: 0, description: "Data is invalid")))
return
Task {
if parameters == nil {
completion(.failure(Bridge.JSError(code: 0, description: "No parameters provided")))
return
}
let transaction = parameters!["transaction"] as? [String: Any]
if transaction == nil {
completion(.failure(Bridge.JSError(code: 0, description: "Not enough parameters provided")))
return
}
let result = await web3.browserFunctions.signTransaction(transaction!)
if result == nil {
completion(.failure(Bridge.JSError(code: 0, description: "Data is invalid")))
return
}
completion(.success(["signedTransaction": result as Any]))
}
completion(.success(["signedTransaction": result as Any]))
}, for: "eth_signTransaction")
}
}
Expand Down
47 changes: 24 additions & 23 deletions Sources/web3swift/HookedFunctions/Web3+BrowserFunctions.swift
Expand Up @@ -9,17 +9,17 @@ import BigInt

extension web3.BrowserFunctions {

public func getAccounts() -> [String]? {
public func getAccounts() async -> [String]? {
do {
let accounts = try self.web3.eth.getAccounts()
let accounts = try await self.web3.eth.getAccounts()
return accounts.compactMap({$0.address})
} catch {
return [String]()
}
}

public func getCoinbase() -> String? {
guard let addresses = self.getAccounts() else {return nil}
public func getCoinbase() async -> String? {
guard let addresses = await self.getAccounts() else {return nil}
guard addresses.count > 0 else {return nil}
return addresses[0]
}
Expand Down Expand Up @@ -69,7 +69,7 @@ extension web3.BrowserFunctions {
return Web3.Utils.publicToAddressString(publicKey)
}

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

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

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

public func estimateGas(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions) -> BigUInt? {
public func estimateGas(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions) async -> BigUInt? {
do {
let result = try self.web3.eth.estimateGas(transaction, transactionOptions: transactionOptions)
let result = try await self.web3.eth.estimateGas(transaction, transactionOptions: transactionOptions)
return result
} catch {
return nil
}
}

public func prepareTxForApproval(_ transactionJSON: [String: Any]) -> (transaction: EthereumTransaction?, options: TransactionOptions?) {
public func prepareTxForApproval(_ transactionJSON: [String: Any]) async -> (transaction: EthereumTransaction?, options: TransactionOptions?) {
do {
let jsonData: Data = try JSONSerialization.data(withJSONObject: transactionJSON, options: [])
let transaction: EthereumTransaction = try JSONDecoder().decode(EthereumTransaction.self, from: jsonData)
let options: TransactionOptions = try JSONDecoder().decode(TransactionOptions.self, from: jsonData)
return try self.prepareTxForApproval(transaction, options: options)
return try await self.prepareTxForApproval(transaction, options: options)
} catch {
return (nil, nil)
}
}

public func prepareTxForApproval(_ trans: EthereumTransaction, options opts: TransactionOptions) throws -> (transaction: EthereumTransaction?, options: TransactionOptions?) {
public func prepareTxForApproval(_ trans: EthereumTransaction, options opts: TransactionOptions) async throws -> (transaction: EthereumTransaction?, options: TransactionOptions?) {
do {
var transaction = trans
var options = opts
guard let _ = options.from else {return (nil, nil)}
let gasPrice = try self.web3.eth.getGasPrice()
let gasPrice = try await self.web3.eth.getGasPrice()
transaction.parameters.gasPrice = gasPrice
options.gasPrice = .manual(gasPrice)
guard let gasEstimate = self.estimateGas(transaction, transactionOptions: options) else {return (nil, nil)}
guard let gasEstimate = await self.estimateGas(transaction, transactionOptions: options) else {return (nil, nil)}
transaction.parameters.gasLimit = gasEstimate

options.gasLimit = .limited(gasEstimate)
print(transaction)
return (transaction, options)
Expand All @@ -146,7 +147,7 @@ extension web3.BrowserFunctions {
}
}

public func signTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") -> String? {
public func signTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") async -> String? {
do {
let jsonData: Data = try JSONSerialization.data(withJSONObject: transactionJSON, options: [])
let transaction: EthereumTransaction = try JSONDecoder().decode(EthereumTransaction.self, from: jsonData)
Expand All @@ -162,11 +163,11 @@ extension web3.BrowserFunctions {
} else {
transactionOptions.nonce = .pending
}
return self.signTransaction(transaction, transactionOptions: transactionOptions, password: password)
return await self.signTransaction(transaction, transactionOptions: transactionOptions, password: password)
} catch { return nil }
}

public func signTransaction(_ trans: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") -> String? {
public func signTransaction(_ trans: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") async -> String? {
do {
var transaction = trans
guard let from = transactionOptions.from else {return nil}
Expand All @@ -178,23 +179,23 @@ extension web3.BrowserFunctions {
case .manual(let gasPrice):
transaction.parameters.gasPrice = gasPrice
default:
let gasPrice = try self.web3.eth.getGasPrice()
let gasPrice = try await self.web3.eth.getGasPrice()
transaction.parameters.gasPrice = gasPrice
}

switch gasLimitPolicy {
case .manual(let gasLimit):
transaction.parameters.gasLimit = gasLimit
default:
let gasLimit = try self.web3.eth.estimateGas(transaction, transactionOptions: transactionOptions)
let gasLimit = try await self.web3.eth.estimateGas(transaction, transactionOptions: transactionOptions)
transaction.parameters.gasLimit = gasLimit
}

switch noncePolicy {
case .manual(let nonce):
transaction.nonce = nonce
default:
let nonce = try self.web3.eth.getTransactionCount(address: from, onBlock: "pending")
let nonce = try await self.web3.eth.getTransactionCount(address: from, onBlock: "pending")
transaction.nonce = nonce
}

Expand Down

0 comments on commit 3126c04

Please sign in to comment.