Skip to content

Commit a51cb35

Browse files
Add legacy gas price prediction method (pre EIP-1559)
Method predict `Transaction.gasPrice` value.
1 parent 197b5d7 commit a51cb35

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Sources/web3swift/Web3/Web3+GasOracle.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ extension Web3 {
9999

100100
return try calculateStatistic(lastNthBlocksBaseFees, statistic)
101101
}
102+
103+
private func suggestGasFeeLegacy(_ statistic: Statistic) throws -> BigUInt {
104+
let latestBlockNumber = try eth.getBlockNumber()
105+
106+
// Assigning last block to object var to predict baseFee of the next block
107+
latestBlock = try eth.getBlockByNumber(latestBlockNumber)
108+
// TODO: Make me work with cache
109+
let lastNthBlockGasPrice = try (latestBlockNumber - blocksNumber ... latestBlockNumber)
110+
.map { try eth.getBlockByNumber($0, fullTransactions: true) }
111+
.flatMap { b -> [EthereumTransaction] in
112+
b.transactions.compactMap { t -> EthereumTransaction? in
113+
guard case let .transaction(transaction) = t else { return nil }
114+
return transaction
115+
}
116+
}
117+
.map { $0.gasPrice }
118+
119+
return try calculateStatistic(lastNthBlockGasPrice, statistic)
120+
}
102121
}
103122
}
104123

@@ -143,6 +162,15 @@ public extension Web3.Oracle {
143162

144163
return (baseFee, tip)
145164
}
165+
166+
// MARK: - Legacy GasPrice
167+
/// Method to get legacy gas price
168+
/// - Parameter statistic: Statistic to apply for gas price
169+
/// - Returns: Suggested gas price amount according to statistic, nil if failed to predict
170+
func predictGasPriceLegacy(_ statistic: Statistic) -> BigUInt? {
171+
guard let value = try? suggestGasFeeLegacy(statistic) else { return nil}
172+
return value
173+
}
146174
}
147175

148176
public extension Web3.Oracle {

0 commit comments

Comments
 (0)