Skip to content

Commit 79bb291

Browse files
Add 2 dimension array decodeHex method.
- Rename decodeHex method to swift name convention. - Add FeeHistory struct initializer.
1 parent ba1a60d commit 79bb291

File tree

3 files changed

+70
-56
lines changed

3 files changed

+70
-56
lines changed

Sources/web3swift/Convenience/Decodable+Extensions.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ extension KeyedDecodingContainer {
103103
/// - Parameter key: The key that the decoded value is associated with.
104104
/// - Returns: A decoded value of type `BigUInt`
105105
/// - throws: `Web3Error.dataError` if value associated with key are unable to be initialized as `DecodableFromHex`.
106-
public func decodeHex<T: DecodableFromHex>(to type: T.Type, key: KeyedDecodingContainer<K>.Key) throws -> T {
107-
let string = try self.decode(String.self, forKey: key)
106+
public func decodeHex<T: DecodableFromHex>(_ type: T.Type, forKey: KeyedDecodingContainer<K>.Key) throws -> T {
107+
let string = try self.decode(String.self, forKey: forKey)
108108
guard let number = T(fromHex: string) else { throw Web3Error.dataError }
109109
return number
110110
}
@@ -117,8 +117,22 @@ extension KeyedDecodingContainer {
117117
/// - Parameter key: The key that the decoded value is associated with.
118118
/// - Returns: A decoded value of type `BigUInt`
119119
/// - throws: `Web3Error.dataError` if value associated with key are unable to be initialized as `[DecodableFromHex]`.
120-
public func decodeHex<T: DecodableFromHex>(to type: Array<T>.Type, key: KeyedDecodingContainer<K>.Key) throws -> Array<T> {
121-
var container = try self.nestedUnkeyedContainer(forKey: key)
120+
public func decodeHex<T: DecodableFromHex>(_ type: Array<T>.Type, forKey: KeyedDecodingContainer<K>.Key) throws -> Array<T> {
121+
var container = try self.nestedUnkeyedContainer(forKey: forKey)
122+
guard let array = try? container.decode(type) else { throw Web3Error.dataError }
123+
return array
124+
}
125+
126+
/// Decodes a value of the given key from Hex to `[DecodableFromHex]`
127+
///
128+
/// Currently this method supports only `Data.Type`, `BigUInt.Type`, `Date.Type`
129+
///
130+
/// - Parameter type: Array of a generic type `T` wich conforms to `DecodableFromHex` protocol
131+
/// - Parameter key: The key that the decoded value is associated with.
132+
/// - Returns: A decoded value of type `BigUInt`
133+
/// - throws: `Web3Error.dataError` if value associated with key are unable to be initialized as `[DecodableFromHex]`.
134+
public func decodeHex<T: DecodableFromHex>(_ type: Array<Array<T>>.Type, forKey: KeyedDecodingContainer<K>.Key) throws -> Array<Array<T>> {
135+
var container = try self.nestedUnkeyedContainer(forKey: forKey)
122136
guard let array = try? container.decode(type) else { throw Web3Error.dataError }
123137
return array
124138
}

Sources/web3swift/Web3/Web3+GasOracle.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ extension web3.Eth.Oracle.FeeHistory: Decodable {
217217
public init(from decoder: Decoder) throws {
218218
let values = try decoder.container(keyedBy: CodingKeys.self)
219219

220-
self.baseFeePerGas = try values.decodeHex(to: [BigUInt].self, key: .baseFeePerGas)
221-
self.gasUsedRatio = try values.decodeHex(to: [Double].self, key: .gasUsedRatio)
222-
self.oldestBlock = try values.decodeHex(to: BigUInt.self, key: .oldestBlock)
223-
self.reward = try values.decodeHex(to: [[BigUInt]].self, key: .reward)
220+
self.baseFeePerGas = try values.decodeHex([BigUInt].self, forKey: .baseFeePerGas)
221+
self.gasUsedRatio = try values.decode([Double].self, forKey: .gasUsedRatio)
222+
self.oldestBlock = try values.decodeHex(BigUInt.self, forKey: .oldestBlock)
223+
self.reward = try values.decodeHex([[BigUInt]].self, forKey: .reward)
224224
}
225225
}
226226

Sources/web3swift/Web3/Web3+Structures.swift

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ extension TransactionOptions: Decodable {
2121
public init(from decoder: Decoder) throws {
2222
let container = try decoder.container(keyedBy: CodingKeys.self)
2323

24-
if let gasLimit = try? container.decodeHex(to: BigUInt.self, key: .gas) {
24+
if let gasLimit = try? container.decodeHex(BigUInt.self, forKey: .gas) {
2525
self.gasLimit = .manual(gasLimit)
2626
} else {
2727
self.gasLimit = .automatic
2828
}
2929

30-
if let gasPrice = try? container.decodeHex(to: BigUInt.self, key: .gasPrice) {
30+
if let gasPrice = try? container.decodeHex(BigUInt.self, forKey: .gasPrice) {
3131
self.gasPrice = .manual(gasPrice)
3232
} else {
3333
self.gasPrice = .automatic
@@ -51,15 +51,15 @@ extension TransactionOptions: Decodable {
5151
// }
5252
self.from = from
5353

54-
self.value = try container.decodeHex(to: BigUInt.self, key: .value)
54+
self.value = try container.decodeHex(BigUInt.self, forKey: .value)
5555

56-
if let nonce = try? container.decodeHex(to: BigUInt.self, key: .nonce) {
56+
if let nonce = try? container.decodeHex(BigUInt.self, forKey: .nonce) {
5757
self.nonce = .manual(nonce)
5858
} else {
5959
self.nonce = .pending
6060
}
6161

62-
if let callOnBlock = try? container.decodeHex(to: BigUInt.self, key: .callOnBlock) {
62+
if let callOnBlock = try? container.decodeHex(BigUInt.self, forKey: .callOnBlock) {
6363
self.callOnBlock = .exactBlockNumber(callOnBlock)
6464
} else {
6565
self.callOnBlock = .pending
@@ -85,22 +85,22 @@ extension EthereumTransaction: Decodable {
8585
let container = try decoder.container(keyedBy: CodingKeys.self)
8686

8787
// test to see if it is a EIP-1559 wrapper
88-
if let envelope = try? container.decodeHex(to: BigUInt.self, key: .type) {
88+
if let envelope = try? container.decodeHex(BigUInt.self, forKey: .type) {
8989
// if present and non-sero we are a new wrapper we can't decode
9090
if envelope != BigInt(0) { throw Web3Error.dataError }
9191
}
9292

93-
if let data = try? container.decodeHex(to: Data.self, key: .data) {
93+
if let data = try? container.decodeHex(Data.self, forKey: .data) {
9494
self.data = data
9595
} else {
96-
guard let data = try? container.decodeHex(to: Data.self, key: .input) else { throw Web3Error.dataError }
96+
guard let data = try? container.decodeHex(Data.self, forKey: .input) else { throw Web3Error.dataError }
9797
self.data = data
9898
}
9999

100-
nonce = try container.decodeHex(to: BigUInt.self, key: .nonce)
101-
v = try container.decodeHex(to: BigUInt.self, key: .v)
102-
r = try container.decodeHex(to: BigUInt.self, key: .r)
103-
s = try container.decodeHex(to: BigUInt.self, key: .s)
100+
nonce = try container.decodeHex(BigUInt.self, forKey: .nonce)
101+
v = try container.decodeHex(BigUInt.self, forKey: .v)
102+
r = try container.decodeHex(BigUInt.self, forKey: .r)
103+
s = try container.decodeHex(BigUInt.self, forKey: .s)
104104

105105
guard let to = options.to,
106106
let gasLimit = options.gasLimit,
@@ -144,9 +144,9 @@ public struct TransactionDetails: Decodable {
144144

145145
public init(from decoder: Decoder) throws {
146146
let container = try decoder.container(keyedBy: CodingKeys.self)
147-
self.blockNumber = try? container.decodeHex(to: BigUInt.self, key: .blockNumber)
148-
self.blockHash = try? container.decodeHex(to: Data.self, key: .blockHash)
149-
self.transactionIndex = try? container.decodeHex(to: BigUInt.self, key: .blockNumber)
147+
self.blockNumber = try? container.decodeHex(BigUInt.self, forKey: .blockNumber)
148+
self.blockHash = try? container.decodeHex(Data.self, forKey: .blockHash)
149+
self.transactionIndex = try? container.decodeHex(BigUInt.self, forKey: .blockNumber)
150150
self.transaction = try EthereumTransaction(from: decoder)
151151
}
152152
}
@@ -192,21 +192,21 @@ extension TransactionReceipt {
192192
public init(from decoder: Decoder) throws {
193193
let container = try decoder.container(keyedBy: CodingKeys.self)
194194

195-
self.blockNumber = try container.decodeHex(to: BigUInt.self, key: .blockNumber)
195+
self.blockNumber = try container.decodeHex(BigUInt.self, forKey: .blockNumber)
196196

197-
self.blockHash = try container.decodeHex(to: Data.self, key: .blockHash)
197+
self.blockHash = try container.decodeHex(Data.self, forKey: .blockHash)
198198

199-
self.transactionIndex = try container.decodeHex(to: BigUInt.self, key: .transactionIndex)
199+
self.transactionIndex = try container.decodeHex(BigUInt.self, forKey: .transactionIndex)
200200

201-
self.transactionHash = try container.decodeHex(to: Data.self, key: .transactionHash)
201+
self.transactionHash = try container.decodeHex(Data.self, forKey: .transactionHash)
202202

203203
self.contractAddress = try? container.decodeIfPresent(EthereumAddress.self, forKey: .contractAddress)
204204

205-
self.cumulativeGasUsed = try container.decodeHex(to: BigUInt.self, key: .cumulativeGasUsed)
205+
self.cumulativeGasUsed = try container.decodeHex(BigUInt.self, forKey: .cumulativeGasUsed)
206206

207-
self.gasUsed = try container.decodeHex(to: BigUInt.self, key: .gasUsed)
207+
self.gasUsed = try container.decodeHex(BigUInt.self, forKey: .gasUsed)
208208

209-
let status = try? container.decodeHex(to: BigUInt.self, key: .status)
209+
let status = try? container.decodeHex(BigUInt.self, forKey: .status)
210210
switch status {
211211
case nil: self.status = .notYetProcessed
212212
case 1: self.status = .ok
@@ -276,19 +276,19 @@ public struct EventLog : Decodable {
276276
let address = try container.decode(EthereumAddress.self, forKey: .address)
277277
self.address = address
278278

279-
self.blockNumber = try container.decodeHex(to: BigUInt.self, key: .blockNumber)
279+
self.blockNumber = try container.decodeHex(BigUInt.self, forKey: .blockNumber)
280280

281-
self.blockHash = try container.decodeHex(to: Data.self, key: .blockHash)
281+
self.blockHash = try container.decodeHex(Data.self, forKey: .blockHash)
282282

283-
self.transactionIndex = try container.decodeHex(to: BigUInt.self, key: .transactionIndex)
283+
self.transactionIndex = try container.decodeHex(BigUInt.self, forKey: .transactionIndex)
284284

285-
self.transactionHash = try container.decodeHex(to: Data.self, key: .transactionHash)
285+
self.transactionHash = try container.decodeHex(Data.self, forKey: .transactionHash)
286286

287-
self.data = try container.decodeHex(to: Data.self, key: .data)
287+
self.data = try container.decodeHex(Data.self, forKey: .data)
288288

289-
self.logIndex = try container.decodeHex(to: BigUInt.self, key: .logIndex)
289+
self.logIndex = try container.decodeHex(BigUInt.self, forKey: .logIndex)
290290

291-
let removed = try? container.decodeHex(to: BigUInt.self, key: .removed)
291+
let removed = try? container.decodeHex(BigUInt.self, forKey: .removed)
292292
self.removed = removed == 1 ? true : false
293293

294294
let topicsStrings = try container.decode([String].self, forKey: .topics)
@@ -377,35 +377,35 @@ extension Block {
377377
public init(from decoder: Decoder) throws {
378378
let container = try decoder.container(keyedBy: CodingKeys.self)
379379

380-
self.number = try container.decodeHex(to: BigUInt.self, key: .number)
381-
self.hash = try container.decodeHex(to: Data.self, key: .hash)
382-
self.parentHash = try container.decodeHex(to: Data.self, key: .parentHash)
383-
self.nonce = try? container.decodeHex(to: Data.self, key: .nonce)
384-
self.sha3Uncles = try container.decodeHex(to: Data.self, key: .sha3Uncles)
380+
self.number = try container.decodeHex(BigUInt.self, forKey: .number)
381+
self.hash = try container.decodeHex(Data.self, forKey: .hash)
382+
self.parentHash = try container.decodeHex(Data.self, forKey: .parentHash)
383+
self.nonce = try? container.decodeHex(Data.self, forKey: .nonce)
384+
self.sha3Uncles = try container.decodeHex(Data.self, forKey: .sha3Uncles)
385385

386-
if let logsBloomData = try? container.decodeHex(to: Data.self, key: .logsBloom) {
386+
if let logsBloomData = try? container.decodeHex(Data.self, forKey: .logsBloom) {
387387
self.logsBloom = EthereumBloomFilter(logsBloomData)
388388
}
389389

390-
self.transactionsRoot = try container.decodeHex(to: Data.self, key: .transactionsRoot)
391-
self.stateRoot = try container.decodeHex(to: Data.self, key: .stateRoot)
392-
self.receiptsRoot = try container.decodeHex(to: Data.self, key: .receiptsRoot)
390+
self.transactionsRoot = try container.decodeHex(Data.self, forKey: .transactionsRoot)
391+
self.stateRoot = try container.decodeHex(Data.self, forKey: .stateRoot)
392+
self.receiptsRoot = try container.decodeHex(Data.self, forKey: .receiptsRoot)
393393

394394
if let minerAddress = try? container.decode(String.self, forKey: .miner) {
395395
self.miner = EthereumAddress(minerAddress)
396396
}
397397

398-
self.difficulty = try container.decodeHex(to: BigUInt.self, key: .difficulty)
399-
self.totalDifficulty = try container.decodeHex(to: BigUInt.self, key: .totalDifficulty)
400-
self.extraData = try container.decodeHex(to: Data.self, key: .extraData)
401-
self.size = try container.decodeHex(to: BigUInt.self, key: .size)
402-
self.gasLimit = try container.decodeHex(to: BigUInt.self, key: .gasLimit)
403-
self.gasUsed = try container.decodeHex(to: BigUInt.self, key: .gasUsed)
398+
self.difficulty = try container.decodeHex(BigUInt.self, forKey: .difficulty)
399+
self.totalDifficulty = try container.decodeHex(BigUInt.self, forKey: .totalDifficulty)
400+
self.extraData = try container.decodeHex(Data.self, forKey: .extraData)
401+
self.size = try container.decodeHex(BigUInt.self, forKey: .size)
402+
self.gasLimit = try container.decodeHex(BigUInt.self, forKey: .gasLimit)
403+
self.gasUsed = try container.decodeHex(BigUInt.self, forKey: .gasUsed)
404404

405405
// optional, since pre EIP-1559 block haven't such property.
406-
self.baseFeePerGas = try? container.decodeHex(to: BigUInt.self, key: .baseFeePerGas)
406+
self.baseFeePerGas = try? container.decodeHex(BigUInt.self, forKey: .baseFeePerGas)
407407

408-
self.timestamp = try container.decodeHex(to: Date.self, key: .timestamp)
408+
self.timestamp = try container.decodeHex(Date.self, forKey: .timestamp)
409409

410410
self.transactions = try container.decode([TransactionInBlock].self, forKey: .transactions)
411411

@@ -443,8 +443,8 @@ public struct TxPoolStatus : Decodable {
443443
public extension TxPoolStatus {
444444
init(from decoder: Decoder) throws {
445445
let container = try decoder.container(keyedBy: CodingKeys.self)
446-
self.pending = try container.decodeHex(to: BigUInt.self, key: .pending)
447-
self.queued = try container.decodeHex(to: BigUInt.self, key: .queued)
446+
self.pending = try container.decodeHex(BigUInt.self, forKey: .pending)
447+
self.queued = try container.decodeHex(BigUInt.self, forKey: .queued)
448448
}
449449
}
450450

0 commit comments

Comments
 (0)