@@ -9,17 +9,17 @@ import BigInt
9
9
10
10
extension web3 . BrowserFunctions {
11
11
12
- public func getAccounts( ) -> [ String ] ? {
12
+ public func getAccounts( ) async -> [ String ] ? {
13
13
do {
14
- let accounts = try self . web3. eth. getAccounts ( )
14
+ let accounts = try await self . web3. eth. getAccounts ( )
15
15
return accounts. compactMap ( { $0. address} )
16
16
} catch {
17
17
return [ String] ( )
18
18
}
19
19
}
20
20
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 }
23
23
guard addresses. count > 0 else { return nil }
24
24
return addresses [ 0 ]
25
25
}
@@ -69,7 +69,7 @@ extension web3.BrowserFunctions {
69
69
return Web3 . Utils. publicToAddressString ( publicKey)
70
70
}
71
71
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 ] ? {
73
73
do {
74
74
let jsonData : Data = try JSONSerialization . data ( withJSONObject: transactionJSON, options: [ ] )
75
75
let transaction : EthereumTransaction = try JSONDecoder ( ) . decode ( EthereumTransaction . self, from: jsonData)
@@ -80,20 +80,20 @@ extension web3.BrowserFunctions {
80
80
transactionOptions. value = options. value ?? 0
81
81
transactionOptions. gasLimit = options. gasLimit ?? . automatic
82
82
transactionOptions. gasPrice = options. gasPrice ?? . automatic
83
- return self . sendTransaction ( transaction, transactionOptions: transactionOptions, password: password)
83
+ return await self . sendTransaction ( transaction, transactionOptions: transactionOptions, password: password)
84
84
} catch { return nil }
85
85
}
86
86
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 ] ? {
88
88
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)
90
90
return [ " txhash " : result. hash]
91
91
} catch {
92
92
return nil
93
93
}
94
94
}
95
95
96
- public func estimateGas( _ transactionJSON: [ String : Any ] ) -> BigUInt ? {
96
+ public func estimateGas( _ transactionJSON: [ String : Any ] ) async -> BigUInt ? {
97
97
do {
98
98
let jsonData : Data = try JSONSerialization . data ( withJSONObject: transactionJSON, options: [ ] )
99
99
let transaction : EthereumTransaction = try JSONDecoder ( ) . decode ( EthereumTransaction . self, from: jsonData)
@@ -104,40 +104,41 @@ extension web3.BrowserFunctions {
104
104
transactionOptions. value = options. value ?? 0
105
105
transactionOptions. gasLimit = . automatic
106
106
transactionOptions. gasPrice = options. gasPrice ?? . automatic
107
- return self . estimateGas ( transaction, transactionOptions: transactionOptions)
107
+ return await self . estimateGas ( transaction, transactionOptions: transactionOptions)
108
108
} catch { return nil }
109
109
}
110
110
111
- public func estimateGas( _ transaction: EthereumTransaction , transactionOptions: TransactionOptions ) -> BigUInt ? {
111
+ public func estimateGas( _ transaction: EthereumTransaction , transactionOptions: TransactionOptions ) async -> BigUInt ? {
112
112
do {
113
- let result = try self . web3. eth. estimateGas ( transaction, transactionOptions: transactionOptions)
113
+ let result = try await self . web3. eth. estimateGas ( transaction, transactionOptions: transactionOptions)
114
114
return result
115
115
} catch {
116
116
return nil
117
117
}
118
118
}
119
119
120
- public func prepareTxForApproval( _ transactionJSON: [ String : Any ] ) -> ( transaction: EthereumTransaction ? , options: TransactionOptions ? ) {
120
+ public func prepareTxForApproval( _ transactionJSON: [ String : Any ] ) async -> ( transaction: EthereumTransaction ? , options: TransactionOptions ? ) {
121
121
do {
122
122
let jsonData : Data = try JSONSerialization . data ( withJSONObject: transactionJSON, options: [ ] )
123
123
let transaction : EthereumTransaction = try JSONDecoder ( ) . decode ( EthereumTransaction . self, from: jsonData)
124
124
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)
126
126
} catch {
127
127
return ( nil , nil )
128
128
}
129
129
}
130
130
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 ? ) {
132
132
do {
133
133
var transaction = trans
134
134
var options = opts
135
135
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 ( )
137
137
transaction. parameters. gasPrice = gasPrice
138
138
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 ) }
140
140
transaction. parameters. gasLimit = gasEstimate
141
+
141
142
options. gasLimit = . limited( gasEstimate)
142
143
print ( transaction)
143
144
return ( transaction, options)
@@ -146,7 +147,7 @@ extension web3.BrowserFunctions {
146
147
}
147
148
}
148
149
149
- public func signTransaction( _ transactionJSON: [ String : Any ] , password: String = " web3swift " ) -> String ? {
150
+ public func signTransaction( _ transactionJSON: [ String : Any ] , password: String = " web3swift " ) async -> String ? {
150
151
do {
151
152
let jsonData : Data = try JSONSerialization . data ( withJSONObject: transactionJSON, options: [ ] )
152
153
let transaction : EthereumTransaction = try JSONDecoder ( ) . decode ( EthereumTransaction . self, from: jsonData)
@@ -162,11 +163,11 @@ extension web3.BrowserFunctions {
162
163
} else {
163
164
transactionOptions. nonce = . pending
164
165
}
165
- return self . signTransaction ( transaction, transactionOptions: transactionOptions, password: password)
166
+ return await self . signTransaction ( transaction, transactionOptions: transactionOptions, password: password)
166
167
} catch { return nil }
167
168
}
168
169
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 ? {
170
171
do {
171
172
var transaction = trans
172
173
guard let from = transactionOptions. from else { return nil }
@@ -178,23 +179,23 @@ extension web3.BrowserFunctions {
178
179
case . manual( let gasPrice) :
179
180
transaction. parameters. gasPrice = gasPrice
180
181
default :
181
- let gasPrice = try self . web3. eth. getGasPrice ( )
182
+ let gasPrice = try await self . web3. eth. getGasPrice ( )
182
183
transaction. parameters. gasPrice = gasPrice
183
184
}
184
185
185
186
switch gasLimitPolicy {
186
187
case . manual( let gasLimit) :
187
188
transaction. parameters. gasLimit = gasLimit
188
189
default :
189
- let gasLimit = try self . web3. eth. estimateGas ( transaction, transactionOptions: transactionOptions)
190
+ let gasLimit = try await self . web3. eth. estimateGas ( transaction, transactionOptions: transactionOptions)
190
191
transaction. parameters. gasLimit = gasLimit
191
192
}
192
193
193
194
switch noncePolicy {
194
195
case . manual( let nonce) :
195
196
transaction. nonce = nonce
196
197
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 " )
198
199
transaction. nonce = nonce
199
200
}
200
201
0 commit comments