@@ -63,32 +63,32 @@ public struct CodableTransaction {
63
63
// MARK: - Properties transaction type related either sends to a node if exist
64
64
65
65
/// the nonce for the transaction
66
- public internal ( set ) var nonce : BigUInt {
66
+ internal var nonce : BigUInt {
67
67
get { return envelope. nonce }
68
68
set { envelope. nonce = newValue }
69
69
}
70
70
71
71
/// the max number of gas units allowed to process this transaction
72
- public internal ( set ) var gasLimit : BigUInt {
72
+ internal var gasLimit : BigUInt {
73
73
get { return envelope. gasLimit }
74
74
set { return envelope. gasLimit = newValue }
75
75
}
76
76
77
77
/// the price per gas unit for the tranaction (Legacy and EIP-2930 only)
78
- public internal ( set ) var gasPrice : BigUInt ? {
78
+ internal var gasPrice : BigUInt ? {
79
79
get { return envelope. gasPrice }
80
80
set { return envelope. gasPrice = newValue }
81
81
}
82
82
83
83
/// the max base fee per gas unit (EIP-1559 only)
84
84
/// this value must be >= baseFee + maxPriorityFeePerGas
85
- public internal ( set ) var maxFeePerGas : BigUInt ? {
85
+ internal var maxFeePerGas : BigUInt ? {
86
86
get { return envelope. maxFeePerGas }
87
87
set { return envelope. maxFeePerGas = newValue }
88
88
}
89
89
90
90
/// the maximum tip to pay the miner (EIP-1559 only)
91
- public internal ( set ) var maxPriorityFeePerGas : BigUInt ? {
91
+ internal var maxPriorityFeePerGas : BigUInt ? {
92
92
get { return envelope. maxPriorityFeePerGas }
93
93
set { return envelope. maxPriorityFeePerGas = newValue }
94
94
}
@@ -190,22 +190,22 @@ public struct CodableTransaction {
190
190
191
191
public mutating func resolve( provider: Web3Provider ) async {
192
192
// FIXME: Delete force try
193
- self . gasLimit = try ! await self . resolveGasLimit ( provider: provider)
193
+ self . gasLimit = try ! await self . gasLimitPolicy . resolve ( provider: provider, transaction : self )
194
194
195
195
if from != nil || sender != nil {
196
196
self . nonce = try ! await self . resolveNonce ( provider: provider)
197
197
}
198
198
if case . eip1559 = type {
199
- self . maxFeePerGas = try ! await self . resolveMaxFeePerGas ( provider: provider)
200
- self . maxPriorityFeePerGas = try ! await self . resolveMaxPriorityFeePerGas ( provider: provider)
199
+ self . maxFeePerGas = try ! await self . maxFeePerGasPolicy . resolve ( provider: provider)
200
+ self . maxPriorityFeePerGas = try ! await self . maxPriorityFeePerGasPolicy . resolve ( provider: provider)
201
201
} else {
202
- self . gasPrice = try ! await self . resolveGasPrice ( provider: provider)
202
+ self . gasPrice = try ! await self . gasPricePolicy . resolve ( provider: provider)
203
203
}
204
204
}
205
205
206
206
public var noncePolicy : NoncePolicy
207
207
public var maxFeePerGasPolicy : FeePerGasPolicy
208
- public var maxPriorityFeePerGasPolicy : FeePerGasPolicy
208
+ public var maxPriorityFeePerGasPolicy : PriorityFeePerGasPolicy
209
209
public var gasPricePolicy : GasPricePolicy
210
210
public var gasLimitPolicy : GasLimitPolicy
211
211
@@ -293,23 +293,80 @@ extension CodableTransaction: Codable {
293
293
294
294
}
295
295
296
+ public protocol Policyable {
297
+ func resolve( provider: Web3Provider , transaction: CodableTransaction ? ) async throws -> BigUInt
298
+ }
299
+
296
300
extension CodableTransaction {
297
301
public enum GasLimitPolicy {
298
302
case automatic
299
303
case manual( BigUInt )
300
304
case limited( BigUInt )
301
305
case withMargin( Double )
306
+
307
+ func resolve( provider: Web3Provider , transaction: CodableTransaction ? ) async throws -> BigUInt {
308
+ guard let transaction = transaction else { throw Web3Error . valueError }
309
+ let request : APIRequest = . estimateGas( transaction, transaction. callOnBlock ?? . latest)
310
+ let response : APIResponse < BigUInt > = try await APIRequest . sendRequest ( with: provider, for: request)
311
+ switch self {
312
+ case . automatic, . withMargin:
313
+ return response. result
314
+ case . manual( let value) :
315
+ return value
316
+ case . limited( let limit) :
317
+ if limit <= response. result {
318
+ return response. result
319
+ } else {
320
+ return limit
321
+ }
322
+ }
323
+ }
302
324
}
303
325
304
326
public enum GasPricePolicy {
305
327
case automatic
306
328
case manual( BigUInt )
307
329
case withMargin( Double )
330
+
331
+ func resolve( provider: Web3Provider , transaction: CodableTransaction ? = nil ) async throws -> BigUInt {
332
+ let oracle = Oracle ( provider)
333
+ switch self {
334
+ case . automatic, . withMargin:
335
+ return await oracle. gasPriceLegacyPercentiles ( ) . max ( ) ?? 0
336
+ case . manual( let value) :
337
+ return value
338
+ }
339
+ }
308
340
}
309
341
310
- public enum FeePerGasPolicy {
342
+ public enum PriorityFeePerGasPolicy : Policyable {
311
343
case automatic
312
344
case manual( BigUInt )
345
+
346
+ public func resolve( provider: Web3Provider , transaction: CodableTransaction ? = nil ) async throws -> BigUInt {
347
+ let oracle = Oracle ( provider)
348
+ switch self {
349
+ case . automatic:
350
+ return await oracle. tipFeePercentiles ( ) . max ( ) ?? 0
351
+ case . manual( let value) :
352
+ return value
353
+ }
354
+ }
355
+ }
356
+
357
+ public enum FeePerGasPolicy : Policyable {
358
+ case automatic
359
+ case manual( BigUInt )
360
+
361
+ public func resolve( provider: Web3Provider , transaction: CodableTransaction ? = nil ) async throws -> BigUInt {
362
+ let oracle = Oracle ( provider)
363
+ switch self {
364
+ case . automatic:
365
+ return await oracle. baseFeePercentiles ( ) . max ( ) ?? 0
366
+ case . manual( let value) :
367
+ return value
368
+ }
369
+ }
313
370
}
314
371
315
372
func resolveNonce( provider: Web3Provider ) async throws -> BigUInt {
@@ -324,52 +381,11 @@ extension CodableTransaction {
324
381
}
325
382
}
326
383
327
- func resolveGasPrice( provider: Web3Provider ) async throws -> BigUInt {
328
- let oracle = Oracle ( provider)
329
- switch gasPricePolicy {
330
- case . automatic, . withMargin:
331
- return await oracle. gasPriceLegacyPercentiles ( ) . max ( ) ?? 0
332
- case . manual( let value) :
333
- return value
334
- }
335
- }
336
384
337
- func resolveGasLimit( provider: Web3Provider ) async throws -> BigUInt {
338
- let request : APIRequest = . estimateGas( self , self . callOnBlock ?? . latest)
339
- let response : APIResponse < BigUInt > = try await APIRequest . sendRequest ( with: provider, for: request)
340
- switch gasLimitPolicy {
341
- case . automatic, . withMargin:
342
- return response. result
343
- case . manual( let value) :
344
- return value
345
- case . limited( let limit) :
346
- if limit <= response. result {
347
- return response. result
348
- } else {
349
- return limit
350
- }
351
- }
352
- }
353
385
354
- func resolveMaxFeePerGas( provider: Web3Provider ) async throws -> BigUInt {
355
- let oracle = Oracle ( provider)
356
- switch maxFeePerGasPolicy {
357
- case . automatic:
358
- return await oracle. baseFeePercentiles ( ) . max ( ) ?? 0
359
- case . manual( let value) :
360
- return value
361
- }
362
- }
363
386
364
- func resolveMaxPriorityFeePerGas( provider: Web3Provider ) async throws -> BigUInt {
365
- let oracle = Oracle ( provider)
366
- switch maxPriorityFeePerGasPolicy {
367
- case . automatic:
368
- return await oracle. tipFeePercentiles ( ) . max ( ) ?? 0
369
- case . manual( let value) :
370
- return value
371
- }
372
- }
387
+
388
+
373
389
}
374
390
375
391
0 commit comments