-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathBlock.swift
107 lines (90 loc) · 4.08 KB
/
Block.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// Block.swift
//
//
// Created by Yaroslav Yashin on 12.07.2022.
//
import Foundation
import BigInt
/// Ethereum Block
///
/// Official specification: [](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.json)
public struct Block {
public var number: BigUInt // MARK: This is optional in web3js, but required in Ethereum JSON-RPC
public var hash: Data // MARK: This is optional in web3js, but required in Ethereum JSON-RPC
public var parentHash: Data
public var nonce: Data? // MARK: This is optional in web3js but required in Ethereum JSON-RPC
public var sha3Uncles: Data
public var logsBloom: EthereumBloomFilter? // MARK: This is optional in web3js but required in Ethereum JSON-RPC
public var transactionsRoot: Data
public var stateRoot: Data
public var receiptsRoot: Data
public var miner: EthereumAddress? // MARK: This is NOT optional in web3js
public var difficulty: BigUInt
public var totalDifficulty: BigUInt? // MARK by JoshKim: Removed from Ethereum official Blockschema (https://github.com/ethereum/execution-apis/commit/9e16d5e76a554c733613a2db631130166e2d8725)
public var extraData: Data
public var size: BigUInt
public var gasLimit: BigUInt
public var gasUsed: BigUInt
public var baseFeePerGas: BigUInt?
public var timestamp: Date
public var transactions: [TransactionInBlock]
public var uncles: [Data]
enum CodingKeys: String, CodingKey {
case number
case hash
case parentHash
case nonce
case sha3Uncles
case logsBloom
case transactionsRoot
case stateRoot
case receiptsRoot
case miner
case difficulty
case totalDifficulty
case extraData
case size
case gasLimit
case gasUsed
case baseFeePerGas
case timestamp
case transactions
case uncles
}
}
extension Block: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.number = try container.decodeHex(BigUInt.self, forKey: .number)
self.hash = try container.decodeHex(Data.self, forKey: .hash)
self.parentHash = try container.decodeHex(Data.self, forKey: .parentHash)
self.nonce = try? container.decodeHex(Data.self, forKey: .nonce)
self.sha3Uncles = try container.decodeHex(Data.self, forKey: .sha3Uncles)
if let logsBloomData = try? container.decodeHex(Data.self, forKey: .logsBloom) {
self.logsBloom = EthereumBloomFilter(logsBloomData)
}
self.transactionsRoot = try container.decodeHex(Data.self, forKey: .transactionsRoot)
self.stateRoot = try container.decodeHex(Data.self, forKey: .stateRoot)
self.receiptsRoot = try container.decodeHex(Data.self, forKey: .receiptsRoot)
if let minerAddress = try? container.decode(String.self, forKey: .miner) {
self.miner = EthereumAddress(minerAddress)
}
self.difficulty = try container.decodeHex(BigUInt.self, forKey: .difficulty)
self.totalDifficulty = try? container.decodeHex(BigUInt.self, forKey: .totalDifficulty)
self.extraData = try container.decodeHex(Data.self, forKey: .extraData)
self.size = try container.decodeHex(BigUInt.self, forKey: .size)
self.gasLimit = try container.decodeHex(BigUInt.self, forKey: .gasLimit)
self.gasUsed = try container.decodeHex(BigUInt.self, forKey: .gasUsed)
// optional, since pre EIP-1559 block haven't such property.
self.baseFeePerGas = try? container.decodeHex(BigUInt.self, forKey: .baseFeePerGas)
self.timestamp = try container.decodeHex(Date.self, forKey: .timestamp)
self.transactions = try container.decode([TransactionInBlock].self, forKey: .transactions)
let unclesStrings = try container.decode([String].self, forKey: .uncles)
self.uncles = try unclesStrings.map {
guard let data = Data.fromHex($0) else { throw Web3Error.dataError }
return data
}
}
}
extension Block: APIResultType { }