Skip to content

Commit f8528f8

Browse files
Merge pull request #658 from yaroslavyaroslav/resolver-enhance
2 parents 510c663 + e4b73e9 commit f8528f8

File tree

7 files changed

+35
-168
lines changed

7 files changed

+35
-168
lines changed

Sources/Core/Oracle/GasOracle.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ final public class Oracle {
136136
let block: BigUInt = try await combineRequest(request: .blockNumber)
137137
latestBlockNumber = block
138138
case let .exact(number): latestBlockNumber = number
139-
// Error throws since pending and erliest are unable to be used in this method.
140-
default: throw Web3Error.valueError
139+
default: throw Web3Error.valueError(desc: "Unable to use '\(block)' policy to resolve block number to calculate gas fee suggestion.")
141140
}
142141

143142
/// checking if latest block number is greather than number of blocks to take in account

Sources/Core/Transaction/Policies.swift

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,37 @@ 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 (see [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559))
17+
/// - maximum priority fee per gas (see [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559))
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
5241
self.maxFeePerGasPolicy = maxFeePerGasPolicy
5342
self.maxPriorityFeePerGasPolicy = maxPriorityFeePerGasPolicy
5443
}
5544

56-
public static var auto: Policies {
57-
return Policies(
58-
noncePolicy: .latest,
59-
gasLimitPolicy: .automatic,
60-
gasPricePolicy: .automatic,
61-
maxFeePerGasPolicy: .automatic,
62-
maxPriorityFeePerGasPolicy: .automatic
63-
)
64-
}
45+
public static let auto = Policies()
6546
}

Sources/Core/Web3Error/Web3Error.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public enum Web3Error: Error {
1313

1414
case dataError
1515
case typeError
16-
case valueError
16+
case valueError(desc: String? = nil)
1717
case serverError(code: Int)
1818
case clientError(code: Int)
1919

Sources/web3swift/Operations/ReadTransaction.swift renamed to Sources/web3swift/Operations/ReadOperation.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ReadOperation {
1616
public var method: String
1717
public var data: Data? { transaction.data }
1818

19-
var resolver: PolicyResolver
19+
var policyResolver: PolicyResolver
2020
var web3: Web3
2121

2222
// FIXME: Rewrite this to CodableTransaction
@@ -31,12 +31,11 @@ public class ReadOperation {
3131
if let network = self.web3.provider.network {
3232
self.transaction.chainID = network.chainID
3333
}
34-
self.resolver = PolicyResolver(provider: web3.provider)
34+
self.policyResolver = PolicyResolver(provider: web3.provider)
3535
}
3636

3737
// TODO: Remove type erasing here, some broad wide protocol should be added instead
3838
public func callContractMethod() async throws -> [String: Any] {
39-
try await resolver.resolveAll(for: &transaction)
4039
// MARK: Read data from ABI flow
4140
// FIXME: This should be dropped, and after `execute()` call, just to decode raw data.
4241
let data: Data = try await self.web3.eth.callTransaction(transaction)

Sources/web3swift/Operations/WriteOperation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import Core
1111
public class WriteOperation: ReadOperation {
1212

1313
// FIXME: Rewrite this to CodableTransaction
14-
/// Sends (raw) transaction for write operation.
14+
/// Sends raw transaction for write operation.
1515
/// - Parameters:
1616
/// - password: Password for private key.
1717
/// - policies: Custom policies for how to resolve (optional). Default is auto.
1818
public func writeToChain(password: String, policies: Policies = .auto) async throws -> TransactionSendingResult {
19-
try await resolver.resolveAll(for: &transaction, with: policies)
19+
try await policyResolver.resolveAll(for: &transaction, with: policies)
2020
if let attachedKeystoreManager = self.web3.provider.attachedKeystoreManager {
2121
do {
2222
try Web3Signer.signTX(transaction: &transaction,

Sources/web3swift/Utils/Hooks/NonceMiddleware.swift

Lines changed: 0 additions & 104 deletions
This file was deleted.

Sources/web3swift/Web3/Web3+Resolver.swift

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class PolicyResolver {
2121
// Nonce should be resolved first - as this might be needed for some
2222
// tx's gas estimation
2323
tx.nonce = try await resolveNonce(for: tx, with: policies.noncePolicy)
24+
} else {
25+
throw Web3Error.valueError(desc: "Could not be resolved with both from and sender are nil")
2426
}
2527

2628
tx.gasLimit = try await resolveGasEstimate(for: tx, with: policies.gasLimitPolicy)
@@ -33,47 +35,37 @@ public class PolicyResolver {
3335
}
3436
}
3537

36-
public func resolveGasBaseFee(for policy: FeePerGasPolicy) async -> BigUInt {
37-
let oracle = Oracle(provider)
38+
public func resolveGasBaseFee(for policy: ValueResolutionPolicy) async -> BigUInt {
3839
switch policy {
3940
case .automatic:
40-
return await oracle.baseFeePercentiles().max() ?? 0
41+
return await Oracle(provider).baseFeePercentiles().max() ?? 0
4142
case .manual(let value):
4243
return value
4344
}
4445
}
4546

46-
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 {
4748
switch policy {
48-
case .automatic, .withMargin:
49+
case .automatic:
4950
return try await estimateGas(for: transaction)
5051
case .manual(let value):
5152
return value
52-
case .limited(let limit):
53-
let result = try await estimateGas(for: transaction)
54-
if limit <= result {
55-
return result
56-
} else {
57-
return limit
58-
}
5953
}
6054
}
6155

62-
public func resolveGasPrice(for policy: GasPricePolicy) async -> BigUInt {
63-
let oracle = Oracle(provider)
56+
public func resolveGasPrice(for policy: ValueResolutionPolicy) async -> BigUInt {
6457
switch policy {
65-
case .automatic, .withMargin:
66-
return await oracle.gasPriceLegacyPercentiles().max() ?? 0
58+
case .automatic:
59+
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 {
73-
let oracle = Oracle(provider)
65+
public func resolveGasPriorityFee(for policy: ValueResolutionPolicy) async -> BigUInt {
7466
switch policy {
7567
case .automatic:
76-
return await oracle.tipFeePercentiles().max() ?? 0
68+
return await Oracle(provider).tipFeePercentiles().max() ?? 0
7769
case .manual(let value):
7870
return value
7971
}
@@ -82,7 +74,7 @@ public class PolicyResolver {
8274
public func resolveNonce(for tx: CodableTransaction, with policy: NoncePolicy) async throws -> BigUInt {
8375
switch policy {
8476
case .pending, .latest, .earliest:
85-
guard let address = tx.from ?? tx.sender else { throw Web3Error.valueError }
77+
guard let address = tx.from ?? tx.sender else { throw Web3Error.valueError() }
8678
let request: APIRequest = .getTransactionCount(address.address, tx.callOnBlock ?? .latest)
8779
let response: APIResponse<BigUInt> = try await APIRequest.sendRequest(with: provider, for: request)
8880
return response.result

0 commit comments

Comments
 (0)