Skip to content

Commit 210337c

Browse files
committed
Remove policyable protocol. Add policies. Remove all deprecated resolve functions
1 parent f512b7b commit 210337c

File tree

7 files changed

+44
-142
lines changed

7 files changed

+44
-142
lines changed

Sources/Core/Structure/Block/BlockNumber.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,3 @@ extension BlockNumber: APIRequestParameterType {
3939
try container.encode(description)
4040
}
4141
}
42-
43-
extension BlockNumber: Policyable {
44-
public func resolve(provider: Web3Provider, transaction: CodableTransaction?) async throws -> BigUInt {
45-
guard let transaction = transaction else { throw Web3Error.valueError }
46-
switch self {
47-
case .pending, .latest, .earliest:
48-
guard let address = transaction.from ?? transaction.sender else { throw Web3Error.valueError }
49-
let request: APIRequest = .getTransactionCount(address.address, transaction.callOnBlock ?? .latest)
50-
let response: APIResponse<BigUInt> = try await APIRequest.sendRequest(with: provider, for: request)
51-
return response.result
52-
case .exact(let value):
53-
return value
54-
}
55-
}
56-
}

Sources/Core/Transaction/CodableTransaction.swift

Lines changed: 3 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import Foundation
88
import BigInt
99

10+
1011
/// Structure capable of carying the parameters for any transaction type.
1112
/// while all fields in this struct are optional, they are not necessarily
1213
/// optional for the type of transaction they apply to.
@@ -187,22 +188,6 @@ public struct CodableTransaction {
187188
return self.envelope.encode(for: type)
188189
}
189190

190-
@available(*, deprecated, message: "Please use PolicyResolver instead")
191-
public mutating func resolve(provider: Web3Provider) async {
192-
// FIXME: Delete force try
193-
self.gasLimit = try! await self.gasLimitPolicy.resolve(provider: provider, transaction: self)
194-
195-
if from != nil || sender != nil {
196-
self.nonce = try! await self.resolveNonce(provider: provider)
197-
}
198-
if case .eip1559 = type {
199-
self.maxFeePerGas = try! await self.maxFeePerGasPolicy.resolve(provider: provider)
200-
self.maxPriorityFeePerGas = try! await self.maxPriorityFeePerGasPolicy.resolve(provider: provider)
201-
} else {
202-
self.gasPrice = try! await self.gasPricePolicy.resolve(provider: provider)
203-
}
204-
}
205-
206191
public var noncePolicy: NoncePolicy
207192
public var maxFeePerGasPolicy: FeePerGasPolicy
208193
public var maxPriorityFeePerGasPolicy: PriorityFeePerGasPolicy
@@ -264,7 +249,7 @@ extension CodableTransaction: Codable {
264249
if let accessList = accessList, !accessList.isEmpty {
265250
try containier.encode(accessList, forKey: .accessList)
266251
}
267-
252+
268253
if !gasLimit.isZero {
269254
try containier.encode(gasLimit.hexString, forKey: .gasLimit)
270255
}
@@ -293,97 +278,6 @@ extension CodableTransaction: Codable {
293278

294279
}
295280

296-
extension CodableTransaction {
297-
public enum GasLimitPolicy {
298-
case automatic
299-
case manual(BigUInt)
300-
case limited(BigUInt)
301-
case withMargin(Double)
302-
303-
@available(*, deprecated, message: "Please use PolicyResolver instead")
304-
func resolve(provider: Web3Provider, transaction: CodableTransaction?) async throws -> BigUInt {
305-
guard let transaction = transaction else { throw Web3Error.valueError }
306-
let request: APIRequest = .estimateGas(transaction, transaction.callOnBlock ?? .latest)
307-
let response: APIResponse<BigUInt> = try await APIRequest.sendRequest(with: provider, for: request)
308-
switch self {
309-
case .automatic, .withMargin:
310-
return response.result
311-
case .manual(let value):
312-
return value
313-
case .limited(let limit):
314-
if limit <= response.result {
315-
return response.result
316-
} else {
317-
return limit
318-
}
319-
}
320-
}
321-
}
322-
323-
public enum GasPricePolicy {
324-
case automatic
325-
case manual(BigUInt)
326-
case withMargin(Double)
327-
328-
@available(*, deprecated, message: "Please use PolicyResolver instead")
329-
func resolve(provider: Web3Provider, transaction: CodableTransaction? = nil) async throws -> BigUInt {
330-
let oracle = Oracle(provider)
331-
switch self {
332-
case .automatic, .withMargin:
333-
return await oracle.gasPriceLegacyPercentiles().max() ?? 0
334-
case .manual(let value):
335-
return value
336-
}
337-
}
338-
}
339-
340-
public enum PriorityFeePerGasPolicy: Policyable {
341-
case automatic
342-
case manual(BigUInt)
343-
344-
@available(*, deprecated, message: "Please use PolicyResolver instead")
345-
public func resolve(provider: Web3Provider, transaction: CodableTransaction? = nil) async throws -> BigUInt {
346-
let oracle = Oracle(provider)
347-
switch self {
348-
case .automatic:
349-
return await oracle.tipFeePercentiles().max() ?? 0
350-
case .manual(let value):
351-
return value
352-
}
353-
}
354-
}
355-
356-
public enum FeePerGasPolicy: Policyable {
357-
case automatic
358-
case manual(BigUInt)
359-
360-
@available(*, deprecated, message: "Please use PolicyResolver instead")
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-
}
370-
}
371-
372-
@available(*, deprecated, message: "Please use PolicyResolver instead")
373-
func resolveNonce(provider: Web3Provider) async throws -> BigUInt {
374-
switch noncePolicy {
375-
case .pending, .latest, .earliest:
376-
guard let address = from ?? sender else { throw Web3Error.valueError }
377-
let request: APIRequest = .getTransactionCount(address.address, callOnBlock ?? .latest)
378-
let response: APIResponse<BigUInt> = try await APIRequest.sendRequest(with: provider, for: request)
379-
return response.result
380-
case .exact(let value):
381-
return value
382-
}
383-
}
384-
385-
}
386-
387281
extension CodableTransaction: CustomStringConvertible {
388282
/// required by CustomString convertable
389283
/// returns a string description for the transaction and its data
@@ -413,7 +307,7 @@ extension CodableTransaction {
413307
/// - parameters: EthereumParameters object containing additional parametrs for the transaction like gas
414308
public init(type: TransactionType? = nil, to: EthereumAddress, nonce: BigUInt = 0,
415309
chainID: BigUInt = 0, value: BigUInt = 0, data: Data = Data(),
416-
gasLimit: BigUInt = 0, maxFeePerGas: BigUInt? = nil, maxPriorityFeePerGas: BigUInt? = nil, gasPrice: BigUInt? = nil,
310+
gasLimit: BigUInt = 0, maxFeePerGas: BigUInt? = nil, maxPriorityFeePerGas: BigUInt? = nil, gasPrice: BigUInt? = nil,
417311
accessList: [AccessListEntry]? = nil, v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0) {
418312
// FIXME: This is duplication and should be fixed.
419313
self.data = data
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Jann Driessen on 01.11.22.
6+
//
7+
8+
import Foundation
9+
import BigInt
10+
11+
public enum GasLimitPolicy {
12+
case automatic
13+
case manual(BigUInt)
14+
case limited(BigUInt)
15+
case withMargin(Double)
16+
}
17+
18+
public enum GasPricePolicy {
19+
case automatic
20+
case manual(BigUInt)
21+
case withMargin(Double)
22+
}
23+
24+
public enum PriorityFeePerGasPolicy {
25+
case automatic
26+
case manual(BigUInt)
27+
}
28+
29+
public enum FeePerGasPolicy {
30+
case automatic
31+
case manual(BigUInt)
32+
}

Sources/Core/Transaction/Policyable.swift

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

Sources/web3swift/Operations/ReadTransaction.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public class ReadOperation {
3434

3535
// TODO: Remove type erasing here, some broad wide protocol should be added instead
3636
public func callContractMethod() async throws -> [String: Any] {
37-
await transaction.resolve(provider: web3.provider)
37+
// TODO: might change to be dependency
38+
let resolver = PolicyResolver(provider: web3.provider)
39+
try await resolver.resolveAll(for: &transaction)
3840
// MARK: Read data from ABI flow
3941
// FIXME: This should be dropped, and after `execute()` call, just to decode raw data.
4042
let data: Data = try await self.web3.eth.callTransaction(transaction)

Sources/web3swift/Operations/WriteOperation.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public class WriteOperation: ReadOperation {
1212

1313
// FIXME: Rewrite this to CodableTransaction
1414
public func writeToChain(password: String) async throws -> TransactionSendingResult {
15-
await transaction.resolve(provider: web3.provider)
15+
// TODO: might change to be dependency
16+
let resolver = PolicyResolver(provider: web3.provider)
17+
try await resolver.resolveAll(for: &transaction)
1618
if let attachedKeystoreManager = self.web3.provider.attachedKeystoreManager {
1719
do {
1820
try Web3Signer.signTX(transaction: &transaction,

Sources/web3swift/Web3/Web3+Resolver.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class PolicyResolver {
3333
}
3434
}
3535

36-
public func resolveGasBaseFee(for policy: CodableTransaction.FeePerGasPolicy) async -> BigUInt {
36+
public func resolveGasBaseFee(for policy: FeePerGasPolicy) async -> BigUInt {
3737
let oracle = Oracle(provider)
3838
switch policy {
3939
case .automatic:
@@ -59,7 +59,7 @@ public class PolicyResolver {
5959
}
6060
}
6161

62-
public func resolveGasPrice(for policy: CodableTransaction.GasPricePolicy) async -> BigUInt {
62+
public func resolveGasPrice(for policy: GasPricePolicy) async -> BigUInt {
6363
let oracle = Oracle(provider)
6464
switch policy {
6565
case .automatic, .withMargin:
@@ -69,7 +69,7 @@ public class PolicyResolver {
6969
}
7070
}
7171

72-
public func resolveGasPriorityFee(for policy: CodableTransaction.PriorityFeePerGasPolicy) async -> BigUInt {
72+
public func resolveGasPriorityFee(for policy: PriorityFeePerGasPolicy) async -> BigUInt {
7373
let oracle = Oracle(provider)
7474
switch policy {
7575
case .automatic:

0 commit comments

Comments
 (0)