@@ -74,15 +74,18 @@ extension web3.BrowserFunctions {
74
74
75
75
76
76
public func sendTransaction( _ transactionJSON: [ String : Any ] , password: String = " web3swift " ) -> [ String : Any ] ? {
77
- guard let transaction = EthereumTransaction . fromJSON ( transactionJSON) else { return nil }
78
- guard let options = TransactionOptions . fromJSON ( transactionJSON) else { return nil }
79
- var transactionOptions = TransactionOptions ( )
80
- transactionOptions. from = options. from
81
- transactionOptions. to = options. to
82
- transactionOptions. value = options. value != nil ? options. value! : BigUInt ( 0 )
83
- transactionOptions. gasLimit = options. gasLimit != nil ? options. gasLimit! : . automatic
84
- transactionOptions. gasPrice = options. gasPrice != nil ? options. gasPrice! : . automatic
85
- return self . sendTransaction ( transaction, transactionOptions: transactionOptions, password: password)
77
+ do {
78
+ let jsonData : Data = try JSONSerialization . data ( withJSONObject: transactionJSON, options: [ ] )
79
+ let transaction : EthereumTransaction = try JSONDecoder ( ) . decode ( EthereumTransaction . self, from: jsonData)
80
+ let options : TransactionOptions = try JSONDecoder ( ) . decode ( TransactionOptions . self, from: jsonData)
81
+ var transactionOptions = TransactionOptions ( )
82
+ transactionOptions. from = options. from
83
+ transactionOptions. to = options. to
84
+ transactionOptions. value = options. value != nil ? options. value! : BigUInt ( 0 )
85
+ transactionOptions. gasLimit = options. gasLimit != nil ? options. gasLimit! : . automatic
86
+ transactionOptions. gasPrice = options. gasPrice != nil ? options. gasPrice! : . automatic
87
+ return self . sendTransaction ( transaction, transactionOptions: transactionOptions, password: password)
88
+ } catch { return nil }
86
89
}
87
90
88
91
public func sendTransaction( _ transaction: EthereumTransaction , transactionOptions: TransactionOptions , password: String = " web3swift " ) -> [ String : Any ] ? {
@@ -95,15 +98,18 @@ extension web3.BrowserFunctions {
95
98
}
96
99
97
100
public func estimateGas( _ transactionJSON: [ String : Any ] ) -> BigUInt ? {
98
- guard let transaction = EthereumTransaction . fromJSON ( transactionJSON) else { return nil }
99
- guard let options = TransactionOptions . fromJSON ( transactionJSON) else { return nil }
100
- var transactionOptions = TransactionOptions ( )
101
- transactionOptions. from = options. from
102
- transactionOptions. to = options. to
103
- transactionOptions. value = options. value != nil ? options. value! : BigUInt ( 0 )
104
- transactionOptions. gasLimit = . automatic
105
- transactionOptions. gasPrice = options. gasPrice != nil ? options. gasPrice! : . automatic
106
- return self . estimateGas ( transaction, transactionOptions: transactionOptions)
101
+ do {
102
+ let jsonData : Data = try JSONSerialization . data ( withJSONObject: transactionJSON, options: [ ] )
103
+ let transaction : EthereumTransaction = try JSONDecoder ( ) . decode ( EthereumTransaction . self, from: jsonData)
104
+ let options : TransactionOptions = try JSONDecoder ( ) . decode ( TransactionOptions . self, from: jsonData)
105
+ var transactionOptions = TransactionOptions ( )
106
+ transactionOptions. from = options. from
107
+ transactionOptions. to = options. to
108
+ transactionOptions. value = options. value != nil ? options. value! : BigUInt ( 0 )
109
+ transactionOptions. gasLimit = . automatic
110
+ transactionOptions. gasPrice = options. gasPrice != nil ? options. gasPrice! : . automatic
111
+ return self . estimateGas ( transaction, transactionOptions: transactionOptions)
112
+ } catch { return nil }
107
113
}
108
114
109
115
public func estimateGas( _ transaction: EthereumTransaction , transactionOptions: TransactionOptions ) -> BigUInt ? {
@@ -116,9 +122,10 @@ extension web3.BrowserFunctions {
116
122
}
117
123
118
124
public func prepareTxForApproval( _ transactionJSON: [ String : Any ] ) -> ( transaction: EthereumTransaction ? , options: TransactionOptions ? ) {
119
- guard let transaction = EthereumTransaction . fromJSON ( transactionJSON) else { return ( nil , nil ) }
120
- guard let options = TransactionOptions . fromJSON ( transactionJSON) else { return ( nil , nil ) }
121
125
do {
126
+ let jsonData : Data = try JSONSerialization . data ( withJSONObject: transactionJSON, options: [ ] )
127
+ let transaction : EthereumTransaction = try JSONDecoder ( ) . decode ( EthereumTransaction . self, from: jsonData)
128
+ let options : TransactionOptions = try JSONDecoder ( ) . decode ( TransactionOptions . self, from: jsonData)
122
129
return try self . prepareTxForApproval ( transaction, options: options)
123
130
} catch {
124
131
return ( nil , nil )
@@ -144,20 +151,23 @@ extension web3.BrowserFunctions {
144
151
}
145
152
146
153
public func signTransaction( _ transactionJSON: [ String : Any ] , password: String = " web3swift " ) -> String ? {
147
- guard let transaction = EthereumTransaction . fromJSON ( transactionJSON) else { return nil }
148
- guard let options = TransactionOptions . fromJSON ( transactionJSON) else { return nil }
149
- var transactionOptions = TransactionOptions ( )
150
- transactionOptions. from = options. from
151
- transactionOptions. to = options. to
152
- transactionOptions. value = options. value != nil ? options. value! : BigUInt ( 0 )
153
- transactionOptions. gasLimit = options. gasLimit != nil ? options. gasLimit! : . automatic
154
- transactionOptions. gasPrice = options. gasPrice != nil ? options. gasPrice! : . automatic
155
- if let nonceString = transactionJSON [ " nonce " ] as? String , let nonce = BigUInt ( nonceString. stripHexPrefix ( ) , radix: 16 ) {
156
- transactionOptions. nonce = . manual( nonce)
157
- } else {
158
- transactionOptions. nonce = . pending
159
- }
160
- return self . signTransaction ( transaction, transactionOptions: transactionOptions, password: password)
154
+ do {
155
+ let jsonData : Data = try JSONSerialization . data ( withJSONObject: transactionJSON, options: [ ] )
156
+ let transaction : EthereumTransaction = try JSONDecoder ( ) . decode ( EthereumTransaction . self, from: jsonData)
157
+ let options : TransactionOptions = try JSONDecoder ( ) . decode ( TransactionOptions . self, from: jsonData)
158
+ var transactionOptions = TransactionOptions ( )
159
+ transactionOptions. from = options. from
160
+ transactionOptions. to = options. to
161
+ transactionOptions. value = options. value != nil ? options. value! : BigUInt ( 0 )
162
+ transactionOptions. gasLimit = options. gasLimit != nil ? options. gasLimit! : . automatic
163
+ transactionOptions. gasPrice = options. gasPrice != nil ? options. gasPrice! : . automatic
164
+ if let nonceString = transactionJSON [ " nonce " ] as? String , let nonce = BigUInt ( nonceString. stripHexPrefix ( ) , radix: 16 ) {
165
+ transactionOptions. nonce = . manual( nonce)
166
+ } else {
167
+ transactionOptions. nonce = . pending
168
+ }
169
+ return self . signTransaction ( transaction, transactionOptions: transactionOptions, password: password)
170
+ } catch { return nil }
161
171
}
162
172
163
173
public func signTransaction( _ trans: EthereumTransaction , transactionOptions: TransactionOptions , password: String = " web3swift " ) -> String ? {
0 commit comments