Skip to content

Commit 76b463b

Browse files
Reducing Policies types and cases to bare minimum.
1 parent 24cc14d commit 76b463b

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

Sources/Core/Transaction/Policies.swift

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,31 @@ import BigInt
1010

1111
public typealias NoncePolicy = BlockNumber
1212

13-
public enum GasLimitPolicy {
14-
case automatic
15-
case manual(BigUInt)
16-
case limited(BigUInt)
17-
case withMargin(Double)
18-
}
19-
20-
public enum GasPricePolicy {
21-
case automatic
22-
case manual(BigUInt)
23-
case withMargin(Double)
24-
}
25-
26-
public enum FeePerGasPolicy {
27-
case automatic
28-
case manual(BigUInt)
29-
}
30-
31-
public enum PriorityFeePerGasPolicy {
13+
/// Policies for resolving values like:
14+
/// - gas required for transaction execution
15+
/// - gas price
16+
/// - maximum fee per gas
17+
/// - maximum priority fee per gas
18+
public enum ValueResolutionPolicy {
19+
/// What ever value will be resolved is the one to be applied
3220
case automatic
21+
/// Specific value to be applied
3322
case manual(BigUInt)
3423
}
3524

3625
public struct Policies {
3726
public let noncePolicy: NoncePolicy
38-
public let gasLimitPolicy: GasLimitPolicy
39-
public let gasPricePolicy: GasPricePolicy
40-
public let maxFeePerGasPolicy: FeePerGasPolicy
41-
public let maxPriorityFeePerGasPolicy: PriorityFeePerGasPolicy
27+
public let gasLimitPolicy: ValueResolutionPolicy
28+
public let gasPricePolicy: ValueResolutionPolicy
29+
public let maxFeePerGasPolicy: ValueResolutionPolicy
30+
public let maxPriorityFeePerGasPolicy: ValueResolutionPolicy
4231

4332
public init(
4433
noncePolicy: NoncePolicy = .latest,
45-
gasLimitPolicy: GasLimitPolicy = .automatic,
46-
gasPricePolicy: GasPricePolicy = .automatic,
47-
maxFeePerGasPolicy: FeePerGasPolicy = .automatic,
48-
maxPriorityFeePerGasPolicy: PriorityFeePerGasPolicy = .automatic) {
34+
gasLimitPolicy: ValueResolutionPolicy = .automatic,
35+
gasPricePolicy: ValueResolutionPolicy = .automatic,
36+
maxFeePerGasPolicy: ValueResolutionPolicy = .automatic,
37+
maxPriorityFeePerGasPolicy: ValueResolutionPolicy = .automatic) {
4938
self.noncePolicy = noncePolicy
5039
self.gasLimitPolicy = gasLimitPolicy
5140
self.gasPricePolicy = gasPricePolicy

Sources/web3swift/Web3/Web3+Resolver.swift

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class PolicyResolver {
3535
}
3636
}
3737

38-
public func resolveGasBaseFee(for policy: FeePerGasPolicy) async -> BigUInt {
38+
public func resolveGasBaseFee(for policy: ValueResolutionPolicy) async -> BigUInt {
3939
switch policy {
4040
case .automatic:
4141
return await Oracle(provider).baseFeePercentiles().max() ?? 0
@@ -44,32 +44,25 @@ public class PolicyResolver {
4444
}
4545
}
4646

47-
public func resolveGasEstimate(for transaction: CodableTransaction, with policy: GasLimitPolicy) async throws -> BigUInt {
47+
public func resolveGasEstimate(for transaction: CodableTransaction, with policy: ValueResolutionPolicy) async throws -> BigUInt {
4848
switch policy {
49-
case .automatic, .withMargin:
49+
case .automatic:
5050
return try await estimateGas(for: transaction)
5151
case .manual(let value):
5252
return value
53-
case .limited(let limit):
54-
let result = try await estimateGas(for: transaction)
55-
if limit <= result {
56-
return result
57-
} else {
58-
return limit
59-
}
6053
}
6154
}
6255

63-
public func resolveGasPrice(for policy: GasPricePolicy) async -> BigUInt {
56+
public func resolveGasPrice(for policy: ValueResolutionPolicy) async -> BigUInt {
6457
switch policy {
65-
case .automatic, .withMargin:
58+
case .automatic:
6659
return await Oracle(provider).gasPriceLegacyPercentiles().max() ?? 0
6760
case .manual(let value):
6861
return value
6962
}
7063
}
7164

72-
public func resolveGasPriorityFee(for policy: PriorityFeePerGasPolicy) async -> BigUInt {
65+
public func resolveGasPriorityFee(for policy: ValueResolutionPolicy) async -> BigUInt {
7366
switch policy {
7467
case .automatic:
7568
return await Oracle(provider).tipFeePercentiles().max() ?? 0

0 commit comments

Comments
 (0)