Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fee info to EthereumBlockInfo #359

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}
Loading