|
7 | 7 | import Foundation
|
8 | 8 | import BigInt
|
9 | 9 |
|
10 |
| -/// Structure capable of carying the parameters for any transaction type. |
11 |
| -/// while all fields in this struct are optional, they are not necessarily |
12 |
| -/// optional for the type of transaction they apply to. |
| 10 | +/// Structure capable of carying the parameters for any transaction type. |
| 11 | +/// While most fields in this struct are optional, they are not necessarily |
| 12 | +/// optional for the type of transaction they apply to. |
13 | 13 | public struct CodableTransaction {
|
14 |
| - public typealias NoncePolicy = BlockNumber |
15 | 14 | /// internal acccess only. The transaction envelope object itself that contains all the transaction data
|
16 | 15 | /// and type specific implementation
|
17 | 16 | internal var envelope: AbstractEnvelope
|
@@ -62,32 +61,32 @@ public struct CodableTransaction {
|
62 | 61 | // MARK: - Properties transaction type related either sends to a node if exist
|
63 | 62 |
|
64 | 63 | /// the nonce for the transaction
|
65 |
| - internal var nonce: BigUInt { |
| 64 | + public var nonce: BigUInt { |
66 | 65 | get { return envelope.nonce }
|
67 | 66 | set { envelope.nonce = newValue }
|
68 | 67 | }
|
69 | 68 |
|
70 | 69 | /// the max number of gas units allowed to process this transaction
|
71 |
| - internal var gasLimit: BigUInt { |
| 70 | + public var gasLimit: BigUInt { |
72 | 71 | get { return envelope.gasLimit }
|
73 | 72 | set { return envelope.gasLimit = newValue }
|
74 | 73 | }
|
75 | 74 |
|
76 | 75 | /// the price per gas unit for the tranaction (Legacy and EIP-2930 only)
|
77 |
| - internal var gasPrice: BigUInt? { |
| 76 | + public var gasPrice: BigUInt? { |
78 | 77 | get { return envelope.gasPrice }
|
79 | 78 | set { return envelope.gasPrice = newValue }
|
80 | 79 | }
|
81 | 80 |
|
82 | 81 | /// the max base fee per gas unit (EIP-1559 only)
|
83 | 82 | /// this value must be >= baseFee + maxPriorityFeePerGas
|
84 |
| - internal var maxFeePerGas: BigUInt? { |
| 83 | + public var maxFeePerGas: BigUInt? { |
85 | 84 | get { return envelope.maxFeePerGas }
|
86 | 85 | set { return envelope.maxFeePerGas = newValue }
|
87 | 86 | }
|
88 | 87 |
|
89 | 88 | /// the maximum tip to pay the miner (EIP-1559 only)
|
90 |
| - internal var maxPriorityFeePerGas: BigUInt? { |
| 89 | + public var maxPriorityFeePerGas: BigUInt? { |
91 | 90 | get { return envelope.maxPriorityFeePerGas }
|
92 | 91 | set { return envelope.maxPriorityFeePerGas = newValue }
|
93 | 92 | }
|
@@ -175,39 +174,13 @@ public struct CodableTransaction {
|
175 | 174 | self.envelope = env
|
176 | 175 | // FIXME: This is duplication and should be fixed.
|
177 | 176 | data = Data()
|
178 |
| - noncePolicy = .latest |
179 |
| - gasLimitPolicy = .automatic |
180 |
| - gasPricePolicy = .automatic |
181 |
| - maxFeePerGasPolicy = .automatic |
182 |
| - maxPriorityFeePerGasPolicy = .automatic |
183 | 177 | }
|
184 | 178 |
|
185 | 179 | /// - Returns: a raw bytestream of the transaction, encoded according to the transactionType
|
186 | 180 | public func encode(for type: EncodeType = .transaction) -> Data? {
|
187 | 181 | return self.envelope.encode(for: type)
|
188 | 182 | }
|
189 | 183 |
|
190 |
| - public mutating func resolve(provider: Web3Provider) async { |
191 |
| - // FIXME: Delete force try |
192 |
| - self.gasLimit = try! await self.gasLimitPolicy.resolve(provider: provider, transaction: self) |
193 |
| - |
194 |
| - if from != nil || sender != nil { |
195 |
| - self.nonce = try! await self.resolveNonce(provider: provider) |
196 |
| - } |
197 |
| - if case .eip1559 = type { |
198 |
| - self.maxFeePerGas = try! await self.maxFeePerGasPolicy.resolve(provider: provider) |
199 |
| - self.maxPriorityFeePerGas = try! await self.maxPriorityFeePerGasPolicy.resolve(provider: provider) |
200 |
| - } else { |
201 |
| - self.gasPrice = try! await self.gasPricePolicy.resolve(provider: provider) |
202 |
| - } |
203 |
| - } |
204 |
| - |
205 |
| - public var noncePolicy: NoncePolicy |
206 |
| - public var maxFeePerGasPolicy: FeePerGasPolicy |
207 |
| - public var maxPriorityFeePerGasPolicy: PriorityFeePerGasPolicy |
208 |
| - public var gasPricePolicy: GasPricePolicy |
209 |
| - public var gasLimitPolicy: GasLimitPolicy |
210 |
| - |
211 | 184 | public static var emptyTransaction = CodableTransaction(to: EthereumAddress.contractDeploymentAddress())
|
212 | 185 | }
|
213 | 186 |
|
@@ -235,12 +208,6 @@ extension CodableTransaction: Codable {
|
235 | 208 | // FIXME: This is duplication and should be fixed.
|
236 | 209 | data = Data()
|
237 | 210 |
|
238 |
| - noncePolicy = .latest |
239 |
| - gasLimitPolicy = .automatic |
240 |
| - gasPricePolicy = .automatic |
241 |
| - maxFeePerGasPolicy = .automatic |
242 |
| - maxPriorityFeePerGasPolicy = .automatic |
243 |
| - |
244 | 211 | // capture any metadata that might be present
|
245 | 212 | self.meta = try TransactionMetadata(from: decoder)
|
246 | 213 | }
|
@@ -292,96 +259,6 @@ extension CodableTransaction: Codable {
|
292 | 259 |
|
293 | 260 | }
|
294 | 261 |
|
295 |
| -public protocol Policyable { |
296 |
| - func resolve(provider: Web3Provider, transaction: CodableTransaction?) async throws -> BigUInt |
297 |
| -} |
298 |
| - |
299 |
| -extension CodableTransaction { |
300 |
| - public enum GasLimitPolicy { |
301 |
| - case automatic |
302 |
| - case manual(BigUInt) |
303 |
| - case limited(BigUInt) |
304 |
| - case withMargin(Double) |
305 |
| - |
306 |
| - func resolve(provider: Web3Provider, transaction: CodableTransaction?) async throws -> BigUInt { |
307 |
| - guard let transaction = transaction else { throw Web3Error.valueError } |
308 |
| - let request: APIRequest = .estimateGas(transaction, transaction.callOnBlock ?? .latest) |
309 |
| - let response: APIResponse<BigUInt> = try await APIRequest.sendRequest(with: provider, for: request) |
310 |
| - switch self { |
311 |
| - case .automatic, .withMargin: |
312 |
| - return response.result |
313 |
| - case .manual(let value): |
314 |
| - return value |
315 |
| - case .limited(let limit): |
316 |
| - if limit <= response.result { |
317 |
| - return response.result |
318 |
| - } else { |
319 |
| - return limit |
320 |
| - } |
321 |
| - } |
322 |
| - } |
323 |
| - } |
324 |
| - |
325 |
| - public enum GasPricePolicy { |
326 |
| - case automatic |
327 |
| - case manual(BigUInt) |
328 |
| - case withMargin(Double) |
329 |
| - |
330 |
| - func resolve(provider: Web3Provider, transaction: CodableTransaction? = nil) async throws -> BigUInt { |
331 |
| - let oracle = Oracle(provider) |
332 |
| - switch self { |
333 |
| - case .automatic, .withMargin: |
334 |
| - return await oracle.gasPriceLegacyPercentiles().max() ?? 0 |
335 |
| - case .manual(let value): |
336 |
| - return value |
337 |
| - } |
338 |
| - } |
339 |
| - } |
340 |
| - |
341 |
| - public enum PriorityFeePerGasPolicy: Policyable { |
342 |
| - case automatic |
343 |
| - case manual(BigUInt) |
344 |
| - |
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 |
| - public func resolve(provider: Web3Provider, transaction: CodableTransaction? = nil) async throws -> BigUInt { |
361 |
| - let oracle = Oracle(provider) |
362 |
| - switch self { |
363 |
| - case .automatic: |
364 |
| - return await oracle.baseFeePercentiles().max() ?? 0 |
365 |
| - case .manual(let value): |
366 |
| - return value |
367 |
| - } |
368 |
| - } |
369 |
| - } |
370 |
| - |
371 |
| - func resolveNonce(provider: Web3Provider) async throws -> BigUInt { |
372 |
| - switch noncePolicy { |
373 |
| - case .pending, .latest, .earliest: |
374 |
| - guard let address = from ?? sender else { throw Web3Error.valueError } |
375 |
| - let request: APIRequest = .getTransactionCount(address.address, callOnBlock ?? .latest) |
376 |
| - let response: APIResponse<BigUInt> = try await APIRequest.sendRequest(with: provider, for: request) |
377 |
| - return response.result |
378 |
| - case .exact(let value): |
379 |
| - return value |
380 |
| - } |
381 |
| - } |
382 |
| - |
383 |
| -} |
384 |
| - |
385 | 262 | extension CodableTransaction: CustomStringConvertible {
|
386 | 263 | /// required by CustomString convertable
|
387 | 264 | /// returns a string description for the transaction and its data
|
@@ -416,11 +293,6 @@ extension CodableTransaction {
|
416 | 293 | // FIXME: This is duplication and should be fixed.
|
417 | 294 | self.data = data
|
418 | 295 | self.accessList = accessList
|
419 |
| - self.gasLimitPolicy = .automatic |
420 |
| - self.noncePolicy = .pending |
421 |
| - self.gasPricePolicy = .automatic |
422 |
| - self.maxFeePerGasPolicy = .automatic |
423 |
| - self.maxPriorityFeePerGasPolicy = .automatic |
424 | 296 | self.callOnBlock = .latest
|
425 | 297 |
|
426 | 298 | self.envelope = EnvelopeFactory.createEnvelope(type: type, to: to, nonce: nonce, chainID: chainID, value: value, data: data, gasLimit: gasLimit, maxFeePerGas: maxFeePerGas, maxPriorityFeePerGas: maxPriorityFeePerGas, gasPrice: gasPrice, accessList: accessList, v: v, r: r, s: s)
|
|
0 commit comments