Skip to content

Commit 482589d

Browse files
committed
Changed the parsing in TransactionInBlock to properly use the Decodable protocol instead of switching over to JSON parsing
Removed all the old (now completely dead) JSON based parsing code. No point in keeping the JSON code as it duplicates the Decodable code, and makes code maintenance more difficult
1 parent cbaea38 commit 482589d

File tree

4 files changed

+8
-178
lines changed

4 files changed

+8
-178
lines changed

Sources/web3swift/HookedFunctions/Web3+BrowserFunctions.swift

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,7 @@ extension web3.BrowserFunctions {
7171
guard let publicKey = SECP256K1.recoverPublicKey(hash: hash, signature: signatureData) else {return nil}
7272
return Web3.Utils.publicToAddressString(publicKey)
7373
}
74-
75-
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)
86-
}
87-
74+
8875
public func sendTransaction(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") -> [String:Any]? {
8976
do {
9077
let result = try self.web3.eth.sendTransaction(transaction, transactionOptions: transactionOptions, password: password)
@@ -93,19 +80,7 @@ extension web3.BrowserFunctions {
9380
return nil
9481
}
9582
}
96-
97-
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)
107-
}
108-
83+
10984
public func estimateGas(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions) -> BigUInt? {
11085
do {
11186
let result = try self.web3.eth.estimateGas(transaction, transactionOptions: transactionOptions)
@@ -114,16 +89,6 @@ extension web3.BrowserFunctions {
11489
return nil
11590
}
11691
}
117-
118-
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-
do {
122-
return try self.prepareTxForApproval(transaction, options: options)
123-
} catch {
124-
return (nil, nil)
125-
}
126-
}
12792

12893
public func prepareTxForApproval(_ trans: EthereumTransaction, options opts: TransactionOptions) throws -> (transaction: EthereumTransaction?, options: TransactionOptions?) {
12994
do {
@@ -142,24 +107,7 @@ extension web3.BrowserFunctions {
142107
return (nil, nil)
143108
}
144109
}
145-
146-
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)
161-
}
162-
110+
163111
public func signTransaction(_ trans: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") -> String? {
164112
do {
165113
var transaction = trans

Sources/web3swift/Transaction/EthereumTransaction.swift

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

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
@@ -198,25 +198,6 @@ public struct TransactionDetails: Decodable {
198198
let transaction = try EthereumTransaction(from: decoder)
199199
self.transaction = transaction
200200
}
201-
202-
public init? (_ json: [String: AnyObject]) {
203-
let bh = json["blockHash"] as? String
204-
if (bh != nil) {
205-
guard let blockHash = Data.fromHex(bh!) else {return nil}
206-
self.blockHash = blockHash
207-
}
208-
let bn = json["blockNumber"] as? String
209-
let ti = json["transactionIndex"] as? String
210-
211-
guard let transaction = EthereumTransaction.fromJSON(json) else {return nil}
212-
self.transaction = transaction
213-
if bn != nil {
214-
blockNumber = BigUInt(bn!.stripHexPrefix(), radix: 16)
215-
}
216-
if ti != nil {
217-
transactionIndex = BigUInt(ti!.stripHexPrefix(), radix: 16)
218-
}
219-
}
220201
}
221202

222203
public struct TransactionReceipt: Decodable {
@@ -418,27 +399,12 @@ public enum TransactionInBlock:Decodable {
418399
if let string = try? value.decode(String.self) {
419400
guard let d = Data.fromHex(string) else {throw Web3Error.dataError}
420401
self = .hash(d)
421-
} else if let dict = try? value.decode([String:String].self) {
422-
// guard let t = try? EthereumTransaction(from: decoder) else {throw Web3Error.dataError}
423-
guard let t = EthereumTransaction.fromJSON(dict) else {throw Web3Error.dataError}
424-
self = .transaction(t)
402+
} else if let transaction = try? value.decode(EthereumTransaction.self) {
403+
self = .transaction(transaction)
425404
} else {
426405
self = .null
427406
}
428407
}
429-
430-
431-
public init?(_ data: AnyObject) {
432-
if let string = data as? String {
433-
guard let d = Data.fromHex(string) else {return nil}
434-
self = .hash(d)
435-
} else if let dict = data as? [String:AnyObject] {
436-
guard let t = EthereumTransaction.fromJSON(dict) else {return nil}
437-
self = .transaction(t)
438-
} else {
439-
return nil
440-
}
441-
}
442408
}
443409

444410
public struct Block:Decodable {

0 commit comments

Comments
 (0)