Skip to content

Commit

Permalink
Merge pull request #359 from Syn-McJ/feat/block-fee-info
Browse files Browse the repository at this point in the history
Add fee info to EthereumBlockInfo
  • Loading branch information
DarthMike authored Feb 2, 2024
2 parents 9087f09 + 6ae5dd8 commit cc721d0
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions web3swift/src/Client/Models/EthereumBlockInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@
//

import Foundation
import BigInt

public struct EthereumBlockInfo: Equatable {
public var number: EthereumBlock
public var timestamp: Date
public var transactions: [String]
public var gasLimit: BigUInt
public var gasUsed: BigUInt
public var baseFeePerGas: BigUInt?
}

extension EthereumBlockInfo: Codable {
enum CodingKeys: CodingKey {
case number
case timestamp
case transactions
case gasLimit
case gasUsed
case baseFeePerGas
}

public init(from decoder: Decoder) throws {
Expand All @@ -33,10 +40,25 @@ extension EthereumBlockInfo: Codable {
guard let transactions = try? container.decode([String].self, forKey: .transactions) else {
throw JSONRPCError.decodingError
}

guard let gasLimit = try? container.decode(String.self, forKey: .gasLimit) else {
throw JSONRPCError.decodingError
}

guard let gasUsed = try? container.decode(String.self, forKey: .gasUsed) else {
throw JSONRPCError.decodingError
}

let baseFeePerGas = try? container.decode(String.self, forKey: .baseFeePerGas)

self.number = number
self.timestamp = Date(timeIntervalSince1970: timestamp)
self.transactions = transactions
self.gasLimit = BigUInt(hex: gasLimit) ?? BigUInt(0)
self.gasUsed = BigUInt(hex: gasUsed) ?? BigUInt(0)
if let baseFee = baseFeePerGas {
self.baseFeePerGas = BigUInt(hex: baseFee)
}
}

public func encode(to encoder: Encoder) throws {
Expand All @@ -45,5 +67,10 @@ extension EthereumBlockInfo: Codable {
try container.encode(number, forKey: .number)
try container.encode(Int(timestamp.timeIntervalSince1970).web3.hexString, forKey: .timestamp)
try container.encode(transactions, forKey: .transactions)
try container.encode(gasLimit.web3.hexString, forKey: .gasLimit)
try container.encode(gasUsed.web3.hexString, forKey: .gasUsed)
if let baseFee = baseFeePerGas {
try container.encode(baseFee.web3.hexString, forKey: .baseFeePerGas)
}
}
}

0 comments on commit cc721d0

Please sign in to comment.