Skip to content

Commit cc721d0

Browse files
authored
Merge pull request #359 from Syn-McJ/feat/block-fee-info
Add fee info to EthereumBlockInfo
2 parents 9087f09 + 6ae5dd8 commit cc721d0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

web3swift/src/Client/Models/EthereumBlockInfo.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
//
55

66
import Foundation
7+
import BigInt
78

89
public struct EthereumBlockInfo: Equatable {
910
public var number: EthereumBlock
1011
public var timestamp: Date
1112
public var transactions: [String]
13+
public var gasLimit: BigUInt
14+
public var gasUsed: BigUInt
15+
public var baseFeePerGas: BigUInt?
1216
}
1317

1418
extension EthereumBlockInfo: Codable {
1519
enum CodingKeys: CodingKey {
1620
case number
1721
case timestamp
1822
case transactions
23+
case gasLimit
24+
case gasUsed
25+
case baseFeePerGas
1926
}
2027

2128
public init(from decoder: Decoder) throws {
@@ -33,10 +40,25 @@ extension EthereumBlockInfo: Codable {
3340
guard let transactions = try? container.decode([String].self, forKey: .transactions) else {
3441
throw JSONRPCError.decodingError
3542
}
43+
44+
guard let gasLimit = try? container.decode(String.self, forKey: .gasLimit) else {
45+
throw JSONRPCError.decodingError
46+
}
47+
48+
guard let gasUsed = try? container.decode(String.self, forKey: .gasUsed) else {
49+
throw JSONRPCError.decodingError
50+
}
51+
52+
let baseFeePerGas = try? container.decode(String.self, forKey: .baseFeePerGas)
3653

3754
self.number = number
3855
self.timestamp = Date(timeIntervalSince1970: timestamp)
3956
self.transactions = transactions
57+
self.gasLimit = BigUInt(hex: gasLimit) ?? BigUInt(0)
58+
self.gasUsed = BigUInt(hex: gasUsed) ?? BigUInt(0)
59+
if let baseFee = baseFeePerGas {
60+
self.baseFeePerGas = BigUInt(hex: baseFee)
61+
}
4062
}
4163

4264
public func encode(to encoder: Encoder) throws {
@@ -45,5 +67,10 @@ extension EthereumBlockInfo: Codable {
4567
try container.encode(number, forKey: .number)
4668
try container.encode(Int(timestamp.timeIntervalSince1970).web3.hexString, forKey: .timestamp)
4769
try container.encode(transactions, forKey: .transactions)
70+
try container.encode(gasLimit.web3.hexString, forKey: .gasLimit)
71+
try container.encode(gasUsed.web3.hexString, forKey: .gasUsed)
72+
if let baseFee = baseFeePerGas {
73+
try container.encode(baseFee.web3.hexString, forKey: .baseFeePerGas)
74+
}
4875
}
4976
}

0 commit comments

Comments
 (0)