Skip to content

Commit df358d4

Browse files
committed
refactored to work with Decodable stream
1 parent dc0d85b commit df358d4

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

Sources/web3swift/HookedFunctions/Web3+BrowserFunctions.swift

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,18 @@ extension web3.BrowserFunctions {
7474

7575

7676
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 }
8689
}
8790

8891
public func sendTransaction(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") -> [String:Any]? {
@@ -95,15 +98,18 @@ extension web3.BrowserFunctions {
9598
}
9699

97100
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 }
107113
}
108114

109115
public func estimateGas(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions) -> BigUInt? {
@@ -116,9 +122,10 @@ extension web3.BrowserFunctions {
116122
}
117123

118124
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)}
121125
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)
122129
return try self.prepareTxForApproval(transaction, options: options)
123130
} catch {
124131
return (nil, nil)
@@ -144,20 +151,23 @@ extension web3.BrowserFunctions {
144151
}
145152

146153
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 }
161171
}
162172

163173
public func signTransaction(_ trans: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") -> String? {

0 commit comments

Comments
 (0)