Skip to content

Commit eaf6189

Browse files
Refactoring
- Rename DecodableFromHex init to make it more explicit. - Rename `slf` to `type` local variable in `getValue<T>() -> T?`. - Fix bug with pre EIP-1559 Block decoding support.
1 parent 40fec2a commit eaf6189

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

Sources/web3swift/Convenience/Decodable+Extensions.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,40 +106,44 @@ extension KeyedDecodingContainer {
106106
/// to be initialized as `BigUInt`.
107107
public func decodeHex<T: DecodableFromHex>(to type: T.Type, key: KeyedDecodingContainer<K>.Key) throws -> T {
108108
let string = try self.decode(String.self, forKey: key)
109-
guard let number = T(from: string) else { throw Web3Error.dataError }
109+
guard let number = T(fromHex: string) else { throw Web3Error.dataError }
110110
return number
111111
}
112112
}
113113

114114
public protocol DecodableFromHex: Decodable {
115-
init?(from hexString: String)
115+
init?(fromHex hexString: String)
116116
}
117117

118118
extension Data: DecodableFromHex {
119-
public init?(from hexString: String) {
119+
public init?(fromHex hexString: String) {
120120
self.init()
121121
guard let tmp = Self.fromHex(hexString) else { return nil }
122122
self = tmp
123123
}
124124
}
125125

126126
extension BigUInt: DecodableFromHex {
127-
public init?(from hexString: String) {
128-
self.init()
129-
guard let tmp = BigUInt(hexString.stripHexPrefix(), radix: 16) else { return nil }
130-
self = tmp
127+
public init?(fromHex hexString: String) {
128+
self.init(hexString.stripHexPrefix(), radix: 16)
131129
}
132130
}
133131

134132
extension Date: DecodableFromHex {
135-
public init?(from hexString: String) {
133+
public init?(fromHex hexString: String) {
136134
self.init()
137135
let stripedHexString = hexString.stripHexPrefix()
138136
guard let timestampInt = UInt64(stripedHexString, radix: 16) else { return nil }
139137
self = Date(timeIntervalSince1970: TimeInterval(timestampInt))
140138
}
141139
}
142140

141+
extension EthereumAddress: DecodableFromHex {
142+
public init?(fromHex hexString: String) {
143+
self.init(hexString, ignoreChecksum: true)
144+
}
145+
}
146+
143147
private extension KeyedDecodingContainer {
144148
func decode(_ type: [String: Any].Type) throws -> [String: Any] {
145149
var dictionary: [String: Any] = [:]

Sources/web3swift/Web3/Web3+JSONRPC.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public struct JSONRPCresponse: Decodable{
109109
[String: Int].self,
110110
[String: [String: [String: [String]]]].self]
111111

112+
// FIXME: Make me a real generic
112113
public init(from decoder: Decoder) throws {
113114
let container = try decoder.container(keyedBy: JSONRPCresponseKeys.self)
114115
let id: Int = try container.decode(Int.self, forKey: .id)
@@ -163,24 +164,26 @@ public struct JSONRPCresponse: Decodable{
163164
self.init(id: id, jsonrpc: jsonrpc, result: result, error: nil)
164165
}
165166

167+
// FIXME: Make me a real generic
166168
/// Get the JSON RCP reponse value by deserializing it into some native <T> class.
167169
///
168170
/// Returns nil if serialization fails
169171
public func getValue<T>() -> T? {
170-
let slf = T.self
171-
if slf == BigUInt.self {
172+
let type = T.self
173+
174+
if type == BigUInt.self {
172175
guard let string = self.result as? String else {return nil}
173176
guard let value = BigUInt(string.stripHexPrefix(), radix: 16) else {return nil}
174177
return value as? T
175-
} else if slf == BigInt.self {
178+
} else if type == BigInt.self {
176179
guard let string = self.result as? String else {return nil}
177180
guard let value = BigInt(string.stripHexPrefix(), radix: 16) else {return nil}
178181
return value as? T
179-
} else if slf == Data.self {
182+
} else if type == Data.self {
180183
guard let string = self.result as? String else {return nil}
181184
guard let value = Data.fromHex(string) else {return nil}
182185
return value as? T
183-
} else if slf == EthereumAddress.self {
186+
} else if type == EthereumAddress.self {
184187
guard let string = self.result as? String else {return nil}
185188
guard let value = EthereumAddress(string, ignoreChecksum: true) else {return nil}
186189
return value as? T
@@ -192,25 +195,25 @@ public struct JSONRPCresponse: Decodable{
192195
// guard let value = self.result as? T else {return nil}
193196
// return value
194197
// }
195-
else if slf == [BigUInt].self {
198+
else if type == [BigUInt].self {
196199
guard let string = self.result as? [String] else {return nil}
197200
let values = string.compactMap { (str) -> BigUInt? in
198201
return BigUInt(str.stripHexPrefix(), radix: 16)
199202
}
200203
return values as? T
201-
} else if slf == [BigInt].self {
204+
} else if type == [BigInt].self {
202205
guard let string = self.result as? [String] else {return nil}
203206
let values = string.compactMap { (str) -> BigInt? in
204207
return BigInt(str.stripHexPrefix(), radix: 16)
205208
}
206209
return values as? T
207-
} else if slf == [Data].self {
210+
} else if type == [Data].self {
208211
guard let string = self.result as? [String] else {return nil}
209212
let values = string.compactMap { (str) -> Data? in
210213
return Data.fromHex(str)
211214
}
212215
return values as? T
213-
} else if slf == [EthereumAddress].self {
216+
} else if type == [EthereumAddress].self {
214217
guard let string = self.result as? [String] else {return nil}
215218
let values = string.compactMap { (str) -> EthereumAddress? in
216219
return EthereumAddress(str, ignoreChecksum: true)

Sources/web3swift/Web3/Web3+Structures.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public struct Block: Decodable {
339339
public var size: BigUInt
340340
public var gasLimit: BigUInt
341341
public var gasUsed: BigUInt
342-
public var baseFeePerGas: BigUInt
342+
public var baseFeePerGas: BigUInt?
343343
public var timestamp: Date
344344
public var transactions: [TransactionInBlock]
345345
public var uncles: [Data]
@@ -401,7 +401,9 @@ extension Block {
401401
self.size = try container.decodeHex(to: BigUInt.self, key: .size)
402402
self.gasLimit = try container.decodeHex(to: BigUInt.self, key: .gasLimit)
403403
self.gasUsed = try container.decodeHex(to: BigUInt.self, key: .gasUsed)
404-
self.baseFeePerGas = try container.decodeHex(to: BigUInt.self, key: .baseFeePerGas)
404+
405+
// optional, since pre EIP-1559 block haven't such property.
406+
self.baseFeePerGas = try? container.decodeHex(to: BigUInt.self, key: .baseFeePerGas)
405407

406408
self.timestamp = try container.decodeHex(to: Date.self, key: .timestamp)
407409

Tests/web3swiftTests/localTests/EIP1559BlockTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ import web3swift
77
class EIP1559BlockTests: XCTestCase {
88
let uselessBlockPart = (
99
number: BigUInt(12_965_000),
10-
hash: Data(from: "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46")!, // "hash":
11-
parentHash: Data(from: "0x2302e1c0b972d00932deb5dab9eb2982f570597d9d42504c05d9c2147eaf9c88")!, // "parentHash":
12-
nonce: Data(from: "0xfb6e1a62d119228b"), // "nonce":
13-
sha3Uncles: Data(from: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")!, // "sha3Uncles":
14-
receiptsRoot: Data(from: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")!, // "receiptsRoot":
15-
logsBloom: EthereumBloomFilter(Data(from: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!), // "logsBloom":
16-
transactionsRoot: Data(from: "0x3a1b03875115b79539e5bd33fb00d8f7b7cd61929d5a3c574f507b8acf415bee")!, // "transactionsRoot":
17-
stateRoot: Data(from: "0xf1133199d44695dfa8fd1bcfe424d82854b5cebef75bddd7e40ea94cda515bcb")!, // "stateRoot":
18-
miner: EthereumAddress( Data(from: "0x8888f1f195afa192cfee860698584c030f4c9db1")!)!, // "miner":
10+
hash: Data(fromHex: "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46")!, // "hash":
11+
parentHash: Data(fromHex: "0x2302e1c0b972d00932deb5dab9eb2982f570597d9d42504c05d9c2147eaf9c88")!, // "parentHash":
12+
nonce: Data(fromHex: "0xfb6e1a62d119228b"), // "nonce":
13+
sha3Uncles: Data(fromHex: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")!, // "sha3Uncles":
14+
receiptsRoot: Data(fromHex: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")!, // "receiptsRoot":
15+
logsBloom: EthereumBloomFilter(Data(fromHex: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!), // "logsBloom":
16+
transactionsRoot: Data(fromHex: "0x3a1b03875115b79539e5bd33fb00d8f7b7cd61929d5a3c574f507b8acf415bee")!, // "transactionsRoot":
17+
stateRoot: Data(fromHex: "0xf1133199d44695dfa8fd1bcfe424d82854b5cebef75bddd7e40ea94cda515bcb")!, // "stateRoot":
18+
miner: EthereumAddress( Data(fromHex: "0x8888f1f195afa192cfee860698584c030f4c9db1")!)!, // "miner":
1919
difficulty: BigUInt(21345678965432), // "difficulty":
2020
totalDifficulty: BigUInt(324567845321), // "totalDifficulty":
2121
size: BigUInt(616), // "size":
22-
extraData: Data(from: "0x")!, // extraData":
22+
extraData: Data(fromHex: "0x")!, // extraData":
2323
gasLimit: BigUInt(3141592), // "gasLimit":
2424
gasUsed: BigUInt(21662), // "gasUsed":
2525
timestamp: Date(), // "timestamp":

0 commit comments

Comments
 (0)