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