@@ -9,17 +9,17 @@ import BigInt
99
1010extension 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