@@ -10,33 +10,33 @@ import Foundation
10
10
import BigInt
11
11
12
12
extension Web3 {
13
- /// Oracle is the class to of a transaction fee suggestion
13
+ /// Oracle is the class to do a transaction fee suggestion
14
14
///
15
- /// It designed for EIP-1559 transactions only.
15
+ /// Designed for EIP-1559 transactions only.
16
16
final public class Oracle {
17
17
private var latestBlock : Block ?
18
18
19
- /// Web3 provider by wich accessing to the blockchain
19
+ /// Web3 provider by which accessing to the blockchain
20
20
private let web3Provider : web3
21
21
22
22
/// Ethereum scope shortcut
23
23
private var eth : web3 . Eth { web3Provider. eth }
24
24
25
- /// Number of block to caltulate statistics
26
- public private( set) var blocksNumber : BigUInt
25
+ /// Count of block to calculate statistics
26
+ public private( set) var blocksCount : BigUInt
27
27
28
- /// Number of transacrtions to filter block for tip calculation
29
- public private( set) var transactionsNumber : BigUInt
28
+ /// Count of transactions to filter block for tip calculation
29
+ public private( set) var transactionsCount : BigUInt
30
30
31
31
/// Oracle initializer
32
32
/// - Parameters:
33
33
/// - provider: Web3 Ethereum provider
34
- /// - blocksNumber: Number of block to caltulate statistics
35
- /// - transactionsNumber: Number of transacrtions to filter block for tip calculation
36
- public init ( _ provider: web3 , blocksNumber : BigUInt = 20 , transactionsNumber : BigUInt = 50 ) {
34
+ /// - blocksCount: Count of block to caltulate statistics
35
+ /// - transactionsCount: Count of transacrtions to filter block for tip calculation
36
+ public init ( _ provider: web3 , blocksCount : BigUInt = 20 , transactionsCount : BigUInt = 50 ) {
37
37
self . web3Provider = provider
38
- self . blocksNumber = blocksNumber
39
- self . transactionsNumber = transactionsNumber
38
+ self . blocksCount = blocksCount
39
+ self . transactionsCount = transactionsCount
40
40
}
41
41
42
42
private func calcBaseFee( for block: Block ? ) -> BigUInt {
@@ -45,20 +45,20 @@ extension Web3 {
45
45
}
46
46
47
47
private func calculateStatistic( _ data: [ BigUInt ] , _ statistic: Statistic ) throws -> BigUInt {
48
- let sortedData = data. sorted ( )
49
- let noAnomalyArray = sortedData. cropAnomalyValues ( )
48
+ let noAnomalyArray = data. cropAnomalyValues ( )
50
49
51
- guard !noAnomalyArray. isEmpty else { throw Web3Error . unknownError }
50
+ // FIXME: Set appropriate error thrown.
51
+ guard let unwrappedArray = noAnomalyArray, !unwrappedArray. isEmpty else { throw Web3Error . unknownError }
52
52
53
53
switch statistic {
54
54
// Force unwrapping is ok, since array checked for epmtiness above
55
- case . minimum: return noAnomalyArray . min ( ) !
56
- case . mean: return noAnomalyArray . mean ( ) !
57
- case . median: return noAnomalyArray . median ( ) !
55
+ case . minimum: return unwrappedArray . min ( ) !
56
+ case . mean: return unwrappedArray . mean ( ) !
57
+ case . median: return unwrappedArray . median ( ) !
58
58
case . maximum:
59
- // Checking that suggestedBaseFee are not lower than will be in the next block
60
- // because in tne maximum statistic we should guarantee that transaction would be included in it.
61
- return max ( calcBaseFee ( for: latestBlock) , noAnomalyArray . max ( ) !)
59
+ // Checking that suggestedBaseFee is not lower than it will be in the next block
60
+ // because in the maximum statistic we should guarantee that transaction would be included in it.
61
+ return max ( calcBaseFee ( for: latestBlock) , unwrappedArray . max ( ) !)
62
62
}
63
63
}
64
64
@@ -70,7 +70,7 @@ extension Web3 {
70
70
// TODO: Make me work with cache
71
71
repeat {
72
72
block = try eth. getBlockByNumber ( latestBlockNumber, fullTransactions: true )
73
- } while block. transactions. count < transactionsNumber
73
+ } while block. transactions. count < transactionsCount
74
74
75
75
// Storing last block to calculate baseFee of the next block
76
76
latestBlock = block
@@ -92,7 +92,7 @@ extension Web3 {
92
92
// Assigning last block to object var to predict baseFee of the next block
93
93
latestBlock = try eth. getBlockByNumber ( latestBlockNumber)
94
94
// TODO: Make me work with cache
95
- let lastNthBlocksBaseFees = try ( latestBlockNumber - blocksNumber ... latestBlockNumber)
95
+ let lastNthBlocksBaseFees = try ( latestBlockNumber - blocksCount ... latestBlockNumber)
96
96
. map { try eth. getBlockByNumber ( $0) }
97
97
. filter { !$0. transactions. isEmpty }
98
98
. map { $0. baseFeePerGas }
@@ -106,7 +106,7 @@ extension Web3 {
106
106
// Assigning last block to object var to predict baseFee of the next block
107
107
latestBlock = try eth. getBlockByNumber ( latestBlockNumber)
108
108
// TODO: Make me work with cache
109
- let lastNthBlockGasPrice = try ( latestBlockNumber - blocksNumber ... latestBlockNumber)
109
+ let lastNthBlockGasPrice = try ( latestBlockNumber - blocksCount ... latestBlockNumber)
110
110
. map { try eth. getBlockByNumber ( $0, fullTransactions: true ) }
111
111
. flatMap { b -> [ EthereumTransaction ] in
112
112
b. transactions. compactMap { t -> EthereumTransaction ? in
@@ -141,7 +141,7 @@ public extension Web3.Oracle {
141
141
///
142
142
/// Normalized means that most high and most low value were droped from calculation.
143
143
///
144
- /// Account first of the latest block that have more than `transactionsNumber` value .
144
+ /// Method will takes in accounting a latest block that have transactions included more than `transactionsCount` property .
145
145
///
146
146
/// - Parameter statistic: Statistic to apply for tip calculation
147
147
/// - Returns: Suggested tip amount according to statistic, nil if failed to perdict
@@ -155,12 +155,12 @@ public extension Web3.Oracle {
155
155
/// - Parameters:
156
156
/// - baseFee: Statistic to apply for baseFee
157
157
/// - tip: Statistic to apply for tip
158
- /// - Returns: Touple where [0] — base fee, [1] — tip, nil if failed to predict
159
- func predictBothFees( baseFee: Statistic , tip: Statistic ) -> ( BigUInt , BigUInt ) ? {
158
+ /// - Returns: Tuple where `baseFee` — base fee, `tip` — tip, nil if failed to predict
159
+ func predictBothFees( baseFee: Statistic , tip: Statistic ) -> ( baseFee : BigUInt , tip : BigUInt ) ? {
160
160
guard let baseFee = try ? suggestBaseFee ( baseFee) else { return nil }
161
161
guard let tip = try ? suggestTipValue ( tip) else { return nil }
162
162
163
- return ( baseFee, tip)
163
+ return ( baseFee: baseFee , tip : tip)
164
164
}
165
165
166
166
// MARK: - Legacy GasPrice
@@ -174,7 +174,7 @@ public extension Web3.Oracle {
174
174
}
175
175
176
176
public extension Web3 . Oracle {
177
- // TODO: Make me struct and incapsulate math within to make me extendable
177
+ // TODO: Make me struct and encapsulate math within to make me extendable
178
178
enum Statistic {
179
179
/// Mininum statistic
180
180
case minimum
@@ -187,11 +187,17 @@ public extension Web3.Oracle {
187
187
}
188
188
}
189
189
190
- extension Array {
191
- func cropAnomalyValues( ) -> Self {
192
- var tmpArr = self . dropFirst ( )
193
- tmpArr = self . dropLast ( )
194
- return Array ( tmpArr)
190
+ extension Array where Element: Comparable {
191
+
192
+ /// Sorts array and drops most and least values.
193
+ /// - Returns: Sorted array without most and least values, nil if `array.count` <= 2
194
+ func cropAnomalyValues( ) -> Self ? {
195
+ var sortedArray = self . sorted ( )
196
+ // Array should at least counts two to pass that formations.
197
+ guard sortedArray. count > 1 else { return nil }
198
+ sortedArray. removeLast ( )
199
+ sortedArray. removeFirst ( )
200
+ return sortedArray
195
201
}
196
202
}
197
203
0 commit comments