Skip to content

Commit 91bd902

Browse files
committed
restore gas oracle
1 parent f7db2ea commit 91bd902

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

Sources/web3swift/Web3/Web3+GasOracle.swift

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,26 @@ extension Web3 {
131131
guard latestBlockNumber != 0 else { return [] }
132132

133133
// TODO: Make me work with cache
134-
let lastNthBlockGasPrice = try (latestBlockNumber - blockCount ... latestBlockNumber)
135-
.map {
136-
try eth.getBlockByNumber($0, fullTransactions: true)
134+
let blocks = try await withThrowingTaskGroup(of: Block.self, returning: [Block].self) { group in
135+
136+
(latestBlockNumber - blockCount ... latestBlockNumber)
137+
.forEach { transaction in
138+
group.addTask {
139+
try await self.eth.getBlockByNumber(transaction, fullTransactions: true)
140+
}
141+
}
142+
143+
144+
var collected = [Block]()
145+
146+
for try await value in group {
147+
collected.append(value)
137148
}
138-
.flatMap { b -> [EthereumTransaction] in
149+
150+
return collected
151+
}
152+
153+
let lastNthBlockGasPrice = blocks.flatMap { b -> [EthereumTransaction] in
139154
b.transactions.compactMap { t -> EthereumTransaction? in
140155
guard case let .transaction(transaction) = t else { return nil }
141156
return transaction
@@ -154,8 +169,8 @@ public extension Web3.Oracle {
154169
///
155170
/// - Returns: `[percentile_1, percentile_2, percentile_3, ...].count == self.percentile.count`
156171
/// empty array if failed to predict. By default there's 3 percentile.
157-
var baseFeePercentiles: [BigUInt] {
158-
guard let value = try? suggestBaseFee() else { return [] }
172+
func baseFeePercentiles() async -> [BigUInt] {
173+
guard let value = try? await suggestBaseFee() else { return [] }
159174
return value
160175
}
161176

@@ -164,8 +179,8 @@ public extension Web3.Oracle {
164179
///
165180
/// - Returns: `[percentile_1, percentile_2, percentile_3, ...].count == self.percentile.count`
166181
/// empty array if failed to predict. By default there's 3 percentile.
167-
var tipFeePercentiles: [BigUInt] {
168-
guard let value = try? suggestTipValue() else { return [] }
182+
func tipFeePercentiles() async -> [BigUInt] {
183+
guard let value = try? await suggestTipValue() else { return [] }
169184
return value
170185
}
171186

@@ -174,13 +189,13 @@ public extension Web3.Oracle {
174189
///
175190
/// - Returns: `[percentile_1, percentile_2, percentile_3, ...].count == self.percentile.count`
176191
/// nil if failed to predict. By default there's 3 percentile.
177-
var bothFeesPercentiles: (baseFee: [BigUInt], tip: [BigUInt])? {
192+
func bothFeesPercentiles() async -> (baseFee: [BigUInt], tip: [BigUInt])? {
178193
var baseFeeArr: [BigUInt] = []
179194
var tipArr: [BigUInt] = []
180-
if let baseFee = try? suggestBaseFee() {
195+
if let baseFee = try? await suggestBaseFee() {
181196
baseFeeArr = baseFee
182197
}
183-
if let tip = try? suggestTipValue() {
198+
if let tip = try? await suggestTipValue() {
184199
tipArr = tip
185200
}
186201
return (baseFee: baseFeeArr, tip: tipArr)
@@ -191,8 +206,8 @@ public extension Web3.Oracle {
191206
///
192207
/// - Returns: `[percentile_1, percentile_2, percentile_3, ...].count == self.percentile.count`
193208
/// empty array if failed to predict. By default there's 3 percentile.
194-
var gasPriceLegacyPercentiles: [BigUInt] {
195-
guard let value = try? suggestGasFeeLegacy() else { return [] }
209+
func gasPriceLegacyPercentiles() async -> [BigUInt] {
210+
guard let value = try? await suggestGasFeeLegacy() else { return [] }
196211
return value
197212
}
198213
}

0 commit comments

Comments
 (0)