Skip to content

Commit b069ed5

Browse files
Fixes by PR review.
1 parent d8463bc commit b069ed5

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

Sources/web3swift/Web3/Web3+GasOracle.swift

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,33 @@ import Foundation
1010
import BigInt
1111

1212
extension Web3 {
13-
/// Oracle is the class to of a transaction fee suggestion
13+
/// Oracle is the class to do a transaction fee suggestion
1414
///
15-
/// It designed for EIP-1559 transactions only.
15+
/// Designed for EIP-1559 transactions only.
1616
final public class Oracle {
1717
private var latestBlock: Block?
1818

19-
/// Web3 provider by wich accessing to the blockchain
19+
/// Web3 provider by which accessing to the blockchain
2020
private let web3Provider: web3
2121

2222
/// Ethereum scope shortcut
2323
private var eth: web3.Eth { web3Provider.eth }
2424

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
2727

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
3030

3131
/// Oracle initializer
3232
/// - Parameters:
3333
/// - 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) {
3737
self.web3Provider = provider
38-
self.blocksNumber = blocksNumber
39-
self.transactionsNumber = transactionsNumber
38+
self.blocksCount = blocksCount
39+
self.transactionsCount = transactionsCount
4040
}
4141

4242
private func calcBaseFee(for block: Block?) -> BigUInt {
@@ -45,20 +45,20 @@ extension Web3 {
4545
}
4646

4747
private func calculateStatistic(_ data: [BigUInt], _ statistic: Statistic) throws -> BigUInt {
48-
let sortedData = data.sorted()
49-
let noAnomalyArray = sortedData.cropAnomalyValues()
48+
let noAnomalyArray = data.cropAnomalyValues()
5049

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 }
5252

5353
switch statistic {
5454
// 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()!
5858
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()!)
6262
}
6363
}
6464

@@ -70,7 +70,7 @@ extension Web3 {
7070
// TODO: Make me work with cache
7171
repeat {
7272
block = try eth.getBlockByNumber(latestBlockNumber, fullTransactions: true)
73-
} while block.transactions.count < transactionsNumber
73+
} while block.transactions.count < transactionsCount
7474

7575
// Storing last block to calculate baseFee of the next block
7676
latestBlock = block
@@ -92,7 +92,7 @@ extension Web3 {
9292
// Assigning last block to object var to predict baseFee of the next block
9393
latestBlock = try eth.getBlockByNumber(latestBlockNumber)
9494
// TODO: Make me work with cache
95-
let lastNthBlocksBaseFees = try (latestBlockNumber - blocksNumber ... latestBlockNumber)
95+
let lastNthBlocksBaseFees = try (latestBlockNumber - blocksCount ... latestBlockNumber)
9696
.map { try eth.getBlockByNumber($0) }
9797
.filter { !$0.transactions.isEmpty }
9898
.map { $0.baseFeePerGas }
@@ -106,7 +106,7 @@ extension Web3 {
106106
// Assigning last block to object var to predict baseFee of the next block
107107
latestBlock = try eth.getBlockByNumber(latestBlockNumber)
108108
// TODO: Make me work with cache
109-
let lastNthBlockGasPrice = try (latestBlockNumber - blocksNumber ... latestBlockNumber)
109+
let lastNthBlockGasPrice = try (latestBlockNumber - blocksCount ... latestBlockNumber)
110110
.map { try eth.getBlockByNumber($0, fullTransactions: true) }
111111
.flatMap { b -> [EthereumTransaction] in
112112
b.transactions.compactMap { t -> EthereumTransaction? in
@@ -141,7 +141,7 @@ public extension Web3.Oracle {
141141
///
142142
/// Normalized means that most high and most low value were droped from calculation.
143143
///
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.
145145
///
146146
/// - Parameter statistic: Statistic to apply for tip calculation
147147
/// - Returns: Suggested tip amount according to statistic, nil if failed to perdict
@@ -155,12 +155,12 @@ public extension Web3.Oracle {
155155
/// - Parameters:
156156
/// - baseFee: Statistic to apply for baseFee
157157
/// - 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)? {
160160
guard let baseFee = try? suggestBaseFee(baseFee) else { return nil }
161161
guard let tip = try? suggestTipValue(tip) else { return nil }
162162

163-
return (baseFee, tip)
163+
return (baseFee: baseFee, tip: tip)
164164
}
165165

166166
// MARK: - Legacy GasPrice
@@ -174,7 +174,7 @@ public extension Web3.Oracle {
174174
}
175175

176176
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
178178
enum Statistic {
179179
/// Mininum statistic
180180
case minimum
@@ -187,11 +187,17 @@ public extension Web3.Oracle {
187187
}
188188
}
189189

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
195201
}
196202
}
197203

0 commit comments

Comments
 (0)