Skip to content

Commit 74fd793

Browse files
Further Resolver enhancement
This one is #645 evolution based (mostly) on @JeneaVranceanu comments.
1 parent 173a3ba commit 74fd793

File tree

5 files changed

+12
-73
lines changed

5 files changed

+12
-73
lines changed

Sources/Core/Transaction/Policies.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public struct Policies {
5454
}
5555

5656
public static var auto: Policies {
57-
return Policies(
57+
Policies(
5858
noncePolicy: .latest,
5959
gasLimitPolicy: .automatic,
6060
gasPricePolicy: .automatic,

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ 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

22+
public var provider: Web3Provider { web3.provider }
23+
2224
// FIXME: Rewrite this to CodableTransaction
2325
public init(transaction: CodableTransaction = CodableTransaction.emptyTransaction,
2426
web3 web3Instance: Web3,
@@ -31,12 +33,12 @@ public class ReadOperation {
3133
if let network = self.web3.provider.network {
3234
self.transaction.chainID = network.chainID
3335
}
34-
self.resolver = PolicyResolver(provider: web3.provider)
36+
self.policyResolver = PolicyResolver(provider: web3.provider)
3537
}
3638

3739
// TODO: Remove type erasing here, some broad wide protocol should be added instead
3840
public func callContractMethod() async throws -> [String: Any] {
39-
try await resolver.resolveAll(for: &transaction)
41+
try await policyResolver.resolveAll(for: &transaction)
4042
// MARK: Read data from ABI flow
4143
// FIXME: This should be dropped, and after `execute()` call, just to decode raw data.
4244
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: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import BigInt
88
import Core
99

1010
extension Web3.Utils {
11-
12-
// fileprivate typealias AssemblyHook = web3.AssemblyHook
13-
// fileprivate typealias SubmissionResultHook = web3.SubmissionResultHook
14-
1511
public class NonceMiddleware: EventLoopRunnableProtocol {
1612
var web3: Web3?
1713
var nonceLookups: [EthereumAddress: BigUInt] = [EthereumAddress: BigUInt]()
@@ -21,16 +17,12 @@ extension Web3.Utils {
2117
var lastSyncTime: Date = Date()
2218

2319
public func functionToRun() async {
24-
guard let w3 = self.web3 else {return}
25-
20+
guard let w3 = self.web3 else { return }
2621
let knownKeys = Array(nonceLookups.keys)
2722

2823
await withTaskGroup(of: BigUInt?.self, returning: Void.self) { group -> Void in
29-
3024
knownKeys.forEach { key in
31-
group.addTask {
32-
try? await w3.eth.getTransactionCount(for: key, onBlock: .latest)
33-
}
25+
group.addTask { try? await w3.eth.getTransactionCount(for: key, onBlock: .latest) }
3426
}
3527

3628
var i = 0
@@ -40,65 +32,9 @@ extension Web3.Utils {
4032
self.nonceLookups[key] = value
4133
i = i + 1
4234
}
43-
4435
}
4536
}
4637

47-
public init() {
48-
49-
}
50-
51-
// FIXME: Rewrite this to CodableTransaction
52-
// func preAssemblyFunction(tx: inout CodableTransaction, contract: EthereumContract) -> (CodableTransaction, EthereumContract, Bool) {
53-
// guard let from = tx.from else {
54-
// // do nothing
55-
// return (tx, contract, true)
56-
// }
57-
// guard let knownNonce = self.nonceLookups[from] else {
58-
// return (tx, contract, true)
59-
// }
60-
//
61-
// let newNonce = knownNonce + 1
62-
//
63-
// self.queue.async {
64-
// self.nonceLookups[from] = newNonce
65-
// }
66-
//
67-
// // FIXME:
68-
// tx.noncePolicy = .exact(newNonce)
69-
// return (tx, contract, true)
70-
// }
71-
72-
// func postSubmissionFunction(result: TransactionSendingResult) {
73-
// guard let from = result.transaction.sender else {
74-
// // do nothing
75-
// return
76-
// }
77-
//
78-
// let newNonce = result.transaction.nonceRe
79-
//
80-
// if let knownNonce = self.nonceLookups[from] {
81-
// if knownNonce != newNonce {
82-
// self.queue.async {
83-
// self.nonceLookups[from] = newNonce
84-
// }
85-
// }
86-
// return
87-
// }
88-
// self.queue.async {
89-
// self.nonceLookups[from] = newNonce
90-
// }
91-
// return
92-
// }
93-
94-
// public func attach(_ web3: web3) {
95-
// self.web3 = web3
96-
// web3.eventLoop.monitoredUserFunctions.append(self)
97-
// let preHook = AssemblyHook(function: self.preAssemblyFunction)
98-
// web3.preAssemblyHooks.append(preHook)
99-
// let postHook = SubmissionResultHook(function: self.postSubmissionFunction)
100-
// web3.postSubmissionHooks.append(postHook)
101-
// }
102-
38+
public init() { }
10339
}
10440
}

Sources/web3swift/Web3/Web3+Resolver.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class PolicyResolver {
1717
}
1818

1919
public func resolveAll(for tx: inout CodableTransaction, with policies: Policies = .auto) async throws {
20+
// FIXME: This should throw an appropriate error, since this condition is critical to whole function tast
2021
if tx.from != nil || tx.sender != nil {
2122
// Nonce should be resolved first - as this might be needed for some
2223
// tx's gas estimation

0 commit comments

Comments
 (0)