Skip to content

Commit e6f526e

Browse files
Merge pull request #490 from mloit/remove-dead-code
2 parents f85ab50 + 1edf2f6 commit e6f526e

File tree

4 files changed

+49
-157
lines changed

4 files changed

+49
-157
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 ?? 0
85+
transactionOptions.gasLimit = options.gasLimit ?? .automatic
86+
transactionOptions.gasPrice = 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 ?? 0
109+
transactionOptions.gasLimit = .automatic
110+
transactionOptions.gasPrice = 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 ?? 0
162+
transactionOptions.gasLimit = options.gasLimit ?? .automatic
163+
transactionOptions.gasPrice = 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? {

Sources/web3swift/Transaction/EthereumTransaction.swift

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -354,51 +354,5 @@ public extension EthereumTransaction {
354354
}
355355
return tx
356356
}
357-
358-
static func fromJSON(_ json: [String: Any]) -> EthereumTransaction? {
359-
guard let options = TransactionOptions.fromJSON(json) else {return nil}
360-
guard let toString = json["to"] as? String else {return nil}
361-
var to: EthereumAddress
362-
if toString == "0x" || toString == "0x0" {
363-
to = EthereumAddress.contractDeploymentAddress()
364-
} else {
365-
guard let ethAddr = EthereumAddress(toString) else {return nil}
366-
to = ethAddr
367-
}
368-
// if (!to.isValid) {
369-
// return nil
370-
// }
371-
var dataString = json["data"] as? String
372-
if (dataString == nil) {
373-
dataString = json["input"] as? String
374-
}
375-
guard dataString != nil, let data = Data.fromHex(dataString!) else {return nil}
376-
var transaction = EthereumTransaction(to: to, data: data, options: options)
377-
if let nonceString = json["nonce"] as? String {
378-
guard let nonce = BigUInt(nonceString.stripHexPrefix(), radix: 16) else {return nil}
379-
transaction.nonce = nonce
380-
}
381-
if let vString = json["v"] as? String {
382-
guard let v = BigUInt(vString.stripHexPrefix(), radix: 16) else {return nil}
383-
transaction.v = v
384-
}
385-
if let rString = json["r"] as? String {
386-
guard let r = BigUInt(rString.stripHexPrefix(), radix: 16) else {return nil}
387-
transaction.r = r
388-
}
389-
if let sString = json["s"] as? String {
390-
guard let s = BigUInt(sString.stripHexPrefix(), radix: 16) else {return nil}
391-
transaction.s = s
392-
}
393-
if let valueString = json["value"] as? String {
394-
guard let value = BigUInt(valueString.stripHexPrefix(), radix: 16) else {return nil}
395-
transaction.value = value
396-
}
397-
let inferedChainID = transaction.inferedChainID
398-
if (transaction.inferedChainID != nil && transaction.v >= BigUInt(37)) {
399-
transaction.chainID = inferedChainID
400-
}
401-
return transaction
402-
}
403-
357+
404358
}

Sources/web3swift/Web3/Web3+Options.swift

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public struct TransactionOptions {
108108
return nil
109109
}
110110
}
111-
111+
112112
public func merge(_ otherOptions: TransactionOptions?) -> TransactionOptions {
113113
guard let other = otherOptions else {return self}
114114
var opts = TransactionOptions()
@@ -121,45 +121,7 @@ public struct TransactionOptions {
121121
opts.callOnBlock = mergeIfNotNil(first: self.callOnBlock, second: other.callOnBlock)
122122
return opts
123123
}
124-
125-
public static func fromJSON(_ json: [String: Any]) -> TransactionOptions? {
126-
var options = TransactionOptions()
127-
if let gas = json["gas"] as? String, let gasBiguint = BigUInt(gas.stripHexPrefix().lowercased(), radix: 16) {
128-
options.gasLimit = .manual(gasBiguint)
129-
} else if let gasLimit = json["gasLimit"] as? String, let gasgasLimitBiguint = BigUInt(gasLimit.stripHexPrefix().lowercased(), radix: 16) {
130-
options.gasLimit = .limited(gasgasLimitBiguint)
131-
} else {
132-
options.gasLimit = .automatic
133-
}
134-
if let gasPrice = json["gasPrice"] as? String, let gasPriceBiguint = BigUInt(gasPrice.stripHexPrefix().lowercased(), radix: 16) {
135-
options.gasPrice = .manual(gasPriceBiguint)
136-
} else {
137-
options.gasPrice = .automatic
138-
}
139-
if let value = json["value"] as? String, let valueBiguint = BigUInt(value.stripHexPrefix().lowercased(), radix: 16) {
140-
options.value = valueBiguint
141-
}
142-
if let toString = json["to"] as? String {
143-
guard let addressTo = EthereumAddress(toString) else {return nil}
144-
options.to = addressTo
145-
}
146-
if let fromString = json["from"] as? String {
147-
guard let addressFrom = EthereumAddress(fromString) else {return nil}
148-
options.from = addressFrom
149-
}
150-
if let nonceString = json["nonce"] as? String, let nonce = BigUInt(nonceString.stripHexPrefix(), radix: 16) {
151-
options.nonce = .manual(nonce)
152-
} else {
153-
options.nonce = .pending
154-
}
155-
if let callOnBlockString = json["callOnBlock"] as? String, let callOnBlock = BigUInt(callOnBlockString.stripHexPrefix(), radix: 16) {
156-
options.callOnBlock = .exactBlockNumber(callOnBlock)
157-
} else {
158-
options.callOnBlock = .pending
159-
}
160-
return options
161-
}
162-
124+
163125
/// Merges two sets of topions by overriding the parameters from the first set by parameters from the second
164126
/// set if those are not nil.
165127
///

Sources/web3swift/Web3/Web3+Structures.swift

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -205,25 +205,6 @@ public struct TransactionDetails: Decodable {
205205
let transaction = try EthereumTransaction(from: decoder)
206206
self.transaction = transaction
207207
}
208-
209-
public init? (_ json: [String: AnyObject]) {
210-
let bh = json["blockHash"] as? String
211-
if (bh != nil) {
212-
guard let blockHash = Data.fromHex(bh!) else {return nil}
213-
self.blockHash = blockHash
214-
}
215-
let bn = json["blockNumber"] as? String
216-
let ti = json["transactionIndex"] as? String
217-
218-
guard let transaction = EthereumTransaction.fromJSON(json) else {return nil}
219-
self.transaction = transaction
220-
if bn != nil {
221-
blockNumber = BigUInt(bn!.stripHexPrefix(), radix: 16)
222-
}
223-
if ti != nil {
224-
transactionIndex = BigUInt(ti!.stripHexPrefix(), radix: 16)
225-
}
226-
}
227208
}
228209

229210
public struct TransactionReceipt: Decodable {
@@ -425,27 +406,12 @@ public enum TransactionInBlock:Decodable {
425406
if let string = try? value.decode(String.self) {
426407
guard let d = Data.fromHex(string) else {throw Web3Error.dataError}
427408
self = .hash(d)
428-
} else if let dict = try? value.decode([String:String].self) {
429-
// guard let t = try? EthereumTransaction(from: decoder) else {throw Web3Error.dataError}
430-
guard let t = EthereumTransaction.fromJSON(dict) else {throw Web3Error.dataError}
431-
self = .transaction(t)
409+
} else if let transaction = try? value.decode(EthereumTransaction.self) {
410+
self = .transaction(transaction)
432411
} else {
433412
self = .null
434413
}
435414
}
436-
437-
438-
public init?(_ data: AnyObject) {
439-
if let string = data as? String {
440-
guard let d = Data.fromHex(string) else {return nil}
441-
self = .hash(d)
442-
} else if let dict = data as? [String:AnyObject] {
443-
guard let t = EthereumTransaction.fromJSON(dict) else {return nil}
444-
self = .transaction(t)
445-
} else {
446-
return nil
447-
}
448-
}
449415
}
450416

451417
public struct Block:Decodable {

0 commit comments

Comments
 (0)