9
9
import Foundation
10
10
import BigInt
11
11
12
- extension Web3 {
12
+ extension web3 . Eth {
13
13
/// Oracle is the class to do a transaction fee suggestion
14
- ///
15
- /// Designed for EIP-1559 transactions only.
16
14
final public class Oracle {
17
- private var latestBlock : Block ?
18
15
19
16
/// Web3 provider by which accessing to the blockchain
20
17
private let web3Provider : web3
21
18
22
19
/// Ethereum scope shortcut
23
20
private var eth : web3 . Eth { web3Provider. eth }
24
21
22
+ /// Block to start getting history
23
+ var block : String
24
+
25
25
/// Count of blocks to calculate statistics
26
- public private ( set ) var blockCount : BigUInt
26
+ var blockCount : BigUInt
27
27
28
28
/// Count of transactions to filter block for tip calculation
29
- public private ( set ) var transactionCount : BigUInt
29
+ var percentiles : [ Double ]
30
30
31
31
/// Oracle initializer
32
32
/// - Parameters:
33
33
/// - provider: Web3 Ethereum provider
34
+ /// - block: Number of block from which counts starts
34
35
/// - blockCount: Count of block to calculate statistics
35
- /// - transactionCount: Count of transaction to filter block for tip calculation
36
- public init ( _ provider: web3 , blockCount: BigUInt = 20 , transactionCount : BigUInt = 50 ) {
36
+ /// - percentiles: Percentiles of fees which will split in fees history
37
+ public init ( _ provider: web3 , block : String = " latest " , blockCount: BigUInt = 20 , percentiles : [ Double ] = [ 25 , 50 , 75 ] ) {
37
38
self . web3Provider = provider
39
+ self . block = block
38
40
self . blockCount = blockCount
39
- self . transactionCount = transactionCount
41
+ self . percentiles = percentiles
40
42
}
41
43
42
- private func calcBaseFee( for block: Block ? ) -> BigUInt {
43
- guard let block = block else { return 0 }
44
- return Web3 . calcBaseFee ( block) ?? 0
45
- }
44
+ // private func calcBaseFee(for block: Block?) -> BigUInt {
45
+ // guard let block = block else { return 0 }
46
+ // return Web3.calcBaseFee(block) ?? 0
47
+ // }
46
48
47
49
private func calculateStatistic( for statistic: Statistic , data: [ BigUInt ] ) throws -> BigUInt {
48
50
let noAnomalyArray = data. cropAnomalyValues ( )
@@ -59,23 +61,29 @@ extension Web3 {
59
61
case . maximum:
60
62
// Checking that suggestedBaseFee is not lower than it will be in the next block
61
63
// because in the maximum statistic we should guarantee that transaction would be included in it.
62
- return max ( calcBaseFee ( for: latestBlock) , unwrappedArray. max ( ) !)
64
+ // return max(calcBaseFee(for: latestBlock), unwrappedArray.max()!)
65
+ return unwrappedArray. max ( ) !
63
66
}
64
67
// swiftlint:enable force_unwrapping
65
68
}
66
69
70
+ // private func suggestGasValues( statistic: Statistic) throws -> FeeHistory {
71
+ //
72
+ // }
73
+
67
74
private func suggestTipValue( _ statistic: Statistic ) throws -> BigUInt {
75
+
68
76
let latestBlockNumber = try eth. getBlockNumber ( )
69
77
70
78
var block : Block
71
79
72
80
// TODO: Make me work with cache
73
81
repeat {
74
82
block = try eth. getBlockByNumber ( latestBlockNumber, fullTransactions: true )
75
- } while block. transactions. count < transactionCount
83
+ } while block. transactions. count < 20
76
84
77
85
// Storing last block to calculate baseFee of the next block
78
- latestBlock = block
86
+ // latestBlock = block
79
87
80
88
let transactionsTips = block. transactions
81
89
. compactMap { t -> EthereumTransaction ? in
@@ -92,7 +100,7 @@ extension Web3 {
92
100
let latestBlockNumber = try eth. getBlockNumber ( )
93
101
94
102
// Assigning last block to object var to predict baseFee of the next block
95
- latestBlock = try eth. getBlockByNumber ( latestBlockNumber)
103
+ // latestBlock = try eth.getBlockByNumber(latestBlockNumber)
96
104
// TODO: Make me work with cache
97
105
let lastNthBlocksBaseFees = try ( latestBlockNumber - blockCount ... latestBlockNumber)
98
106
. map { try eth. getBlockByNumber ( $0) }
@@ -106,7 +114,7 @@ extension Web3 {
106
114
let latestBlockNumber = try eth. getBlockNumber ( )
107
115
108
116
// Assigning last block to object var to predict baseFee of the next block
109
- latestBlock = try eth. getBlockByNumber ( latestBlockNumber)
117
+ // latestBlock = try eth.getBlockByNumber(latestBlockNumber)
110
118
// TODO: Make me work with cache
111
119
let lastNthBlockGasPrice = try ( latestBlockNumber - blockCount ... latestBlockNumber)
112
120
. map { try eth. getBlockByNumber ( $0, fullTransactions: true ) }
@@ -123,7 +131,7 @@ extension Web3 {
123
131
}
124
132
}
125
133
126
- public extension Web3 . Oracle {
134
+ public extension web3 . Eth . Oracle {
127
135
// MARK: - Base Fee
128
136
/// Base fee amount based on last Nth blocks
129
137
///
@@ -175,7 +183,7 @@ public extension Web3.Oracle {
175
183
}
176
184
}
177
185
178
- public extension Web3 . Oracle {
186
+ public extension web3 . Eth . Oracle {
179
187
// TODO: Make me struct and encapsulate math within to make me extendable
180
188
enum Statistic {
181
189
/// Mininum statistic
@@ -189,6 +197,33 @@ public extension Web3.Oracle {
189
197
}
190
198
}
191
199
200
+ public extension web3 . Eth . Oracle {
201
+ struct FeeHistory {
202
+ let baseFeePerGas : [ BigUInt ]
203
+ let gasUsedRatio : [ Double ]
204
+ let oldestBlock : BigUInt
205
+ let reward : [ [ BigUInt ] ]
206
+ }
207
+ }
208
+
209
+ extension web3 . Eth . Oracle . FeeHistory : Decodable {
210
+ enum CodingKeys : String , CodingKey {
211
+ case baseFeePerGas
212
+ case gasUsedRatio
213
+ case oldestBlock
214
+ case reward
215
+ }
216
+
217
+ public init ( from decoder: Decoder ) throws {
218
+ let values = try decoder. container ( keyedBy: CodingKeys . self)
219
+
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)
224
+ }
225
+ }
226
+
192
227
extension Array where Element: Comparable {
193
228
194
229
/// Sorts array and drops most and least values.
0 commit comments