@@ -63,32 +63,32 @@ public struct CodableTransaction {
6363 // MARK: - Properties transaction type related either sends to a node if exist
6464
6565 /// the nonce for the transaction
66- public internal ( set ) var nonce : BigUInt {
66+ internal var nonce : BigUInt {
6767 get { return envelope. nonce }
6868 set { envelope. nonce = newValue }
6969 }
7070
7171 /// the max number of gas units allowed to process this transaction
72- public internal ( set ) var gasLimit : BigUInt {
72+ internal var gasLimit : BigUInt {
7373 get { return envelope. gasLimit }
7474 set { return envelope. gasLimit = newValue }
7575 }
7676
7777 /// 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 ? {
7979 get { return envelope. gasPrice }
8080 set { return envelope. gasPrice = newValue }
8181 }
8282
8383 /// the max base fee per gas unit (EIP-1559 only)
8484 /// this value must be >= baseFee + maxPriorityFeePerGas
85- public internal ( set ) var maxFeePerGas : BigUInt ? {
85+ internal var maxFeePerGas : BigUInt ? {
8686 get { return envelope. maxFeePerGas }
8787 set { return envelope. maxFeePerGas = newValue }
8888 }
8989
9090 /// the maximum tip to pay the miner (EIP-1559 only)
91- public internal ( set ) var maxPriorityFeePerGas : BigUInt ? {
91+ internal var maxPriorityFeePerGas : BigUInt ? {
9292 get { return envelope. maxPriorityFeePerGas }
9393 set { return envelope. maxPriorityFeePerGas = newValue }
9494 }
@@ -190,22 +190,22 @@ public struct CodableTransaction {
190190
191191 public mutating func resolve( provider: Web3Provider ) async {
192192 // 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 )
194194
195195 if from != nil || sender != nil {
196196 self . nonce = try ! await self . resolveNonce ( provider: provider)
197197 }
198198 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)
201201 } else {
202- self . gasPrice = try ! await self . resolveGasPrice ( provider: provider)
202+ self . gasPrice = try ! await self . gasPricePolicy . resolve ( provider: provider)
203203 }
204204 }
205205
206206 public var noncePolicy : NoncePolicy
207207 public var maxFeePerGasPolicy : FeePerGasPolicy
208- public var maxPriorityFeePerGasPolicy : FeePerGasPolicy
208+ public var maxPriorityFeePerGasPolicy : PriorityFeePerGasPolicy
209209 public var gasPricePolicy : GasPricePolicy
210210 public var gasLimitPolicy : GasLimitPolicy
211211
@@ -293,23 +293,80 @@ extension CodableTransaction: Codable {
293293
294294}
295295
296+ public protocol Policyable {
297+ func resolve( provider: Web3Provider , transaction: CodableTransaction ? ) async throws -> BigUInt
298+ }
299+
296300extension CodableTransaction {
297301 public enum GasLimitPolicy {
298302 case automatic
299303 case manual( BigUInt )
300304 case limited( BigUInt )
301305 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+ }
302324 }
303325
304326 public enum GasPricePolicy {
305327 case automatic
306328 case manual( BigUInt )
307329 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+ }
308340 }
309341
310- public enum FeePerGasPolicy {
342+ public enum PriorityFeePerGasPolicy : Policyable {
311343 case automatic
312344 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+ }
313370 }
314371
315372 func resolveNonce( provider: Web3Provider ) async throws -> BigUInt {
@@ -324,52 +381,11 @@ extension CodableTransaction {
324381 }
325382 }
326383
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- }
336384
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- }
353385
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- }
363386
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+
373389}
374390
375391
0 commit comments