@@ -16,6 +16,7 @@ public protocol CodableTransactionInheritable {
16
16
/// while all fields in this struct are optional, they are not necessarily
17
17
/// optional for the type of transaction they apply to.
18
18
public struct CodableTransaction {
19
+ public typealias NoncePolicy = BlockNumber
19
20
/// internal acccess only. The transaction envelope object itself that contains all the transaction data
20
21
/// and type specific implementation
21
22
internal var envelope : AbstractEnvelope
@@ -45,12 +46,6 @@ public struct CodableTransaction {
45
46
/// this should always be set to give an idea of what other fields to expect
46
47
public var type : TransactionType { return envelope. type }
47
48
48
- /// the nonce for the transaction
49
- public var nonce : BigUInt {
50
- get { return envelope. nonce }
51
- set { envelope. nonce = newValue }
52
- }
53
-
54
49
/// the chainId that transaction is targeted for
55
50
/// should be set for all types, except some Legacy transactions (Pre EIP-155)
56
51
/// will not have this set
@@ -71,21 +66,27 @@ public struct CodableTransaction {
71
66
72
67
// MARK: - Properties transaction type related either sends to a node if exist
73
68
69
+ /// the nonce for the transaction
70
+ public internal ( set) var nonce : BigUInt {
71
+ get { return envelope. nonce }
72
+ set { envelope. nonce = newValue }
73
+ }
74
+
74
75
/// the max number of gas units allowed to process this transaction
75
- public var gasLimit : BigUInt {
76
+ public internal ( set ) var gasLimit : BigUInt {
76
77
get { return envelope. gasLimit }
77
78
set { return envelope. gasLimit = newValue }
78
79
}
79
80
80
81
/// the price per gas unit for the tranaction (Legacy and EIP-2930 only)
81
- public var gasPrice : BigUInt ?
82
+ public internal ( set ) var gasPrice : BigUInt ?
82
83
83
84
/// the max base fee per gas unit (EIP-1559 only)
84
85
/// this value must be >= baseFee + maxPriorityFeePerGas
85
- public var maxFeePerGas : BigUInt ?
86
+ public internal ( set ) var maxFeePerGas : BigUInt ?
86
87
87
88
/// the maximum tip to pay the miner (EIP-1559 only)
88
- public var maxPriorityFeePerGas : BigUInt ?
89
+ public internal ( set ) var maxPriorityFeePerGas : BigUInt ?
89
90
90
91
public var callOnBlock : BlockNumber ?
91
92
@@ -109,10 +110,8 @@ public struct CodableTransaction {
109
110
return hash
110
111
}
111
112
112
- // FIXME: This should made private up to release
113
113
private init ( ) { preconditionFailure ( " Memberwise not supported " ) } // disable the memberwise initializer
114
114
115
-
116
115
/// - Returns: a hash of the transaction suitable for signing
117
116
public func hashForSignature( ) -> Data ? {
118
117
guard let encoded = self . envelope. encode ( for: . signature) else { return nil }
@@ -135,6 +134,8 @@ public struct CodableTransaction {
135
134
}
136
135
137
136
/// Signs the transaction
137
+ ///
138
+ /// This method signs transaction iteself and not related to contract call data signing.
138
139
/// - Parameters:
139
140
/// - privateKey: the private key to use for signing
140
141
/// - useExtraEntropy: boolean whether to use extra entropy when signing (default false)
@@ -164,49 +165,43 @@ public struct CodableTransaction {
164
165
self . envelope. clearSignatureData ( )
165
166
}
166
167
167
- /// Descriptionconverts transaction to the new selected type
168
- /// - Parameter to: TransactionType to select what transaction type to convert to
169
- public mutating func migrate( to type: TransactionType ) {
170
- if self . type == type { return }
171
-
172
- // FIXME: Move it back
173
- // let newEnvelope = EnvelopeFactory.createEnvelope(type: type, to: self.envelope.to,
174
- // nonce: self.envelope.nonce)
175
- // self.envelope = newEnvelope
176
- }
177
-
178
168
/// Create a new CodableTransaction from a raw stream of bytes from the blockchain
179
169
public init ? ( rawValue: Data ) {
180
170
guard let env = EnvelopeFactory . createEnvelope ( rawValue: rawValue) else { return nil }
181
171
self . envelope = env
182
172
// FIXME: This is duplication and should be fixed.
183
173
data = Data ( )
174
+ noncePolicy = . latest
175
+ gasLimitPolicy = . automatic
176
+ gasPricePolicy = . automatic
177
+ maxFeePerGasPolicy = . automatic
178
+ maxPriorityFeePerGasPolicy = . automatic
184
179
}
185
180
186
181
/// - Returns: a raw bytestream of the transaction, encoded according to the transactionType
187
182
public func encode( for type: EncodeType = . transaction) -> Data ? {
188
183
return self . envelope. encode ( for: type)
189
184
}
190
185
191
- public mutating func applyOptions ( _ options : CodableTransaction ) {
192
- // type cannot be changed here, and is ignored
193
- // FIXME: Add appropiate values of resolveAny
194
- self . nonce = options. resolveNonce ( nonce)
195
- self . gasPrice = options. resolveGasPrice ( gasPrice ?? 0 )
196
- self . gasLimit = options. resolveGasLimit ( gasLimit)
197
- self . maxFeePerGas = options. resolveMaxFeePerGas ( maxFeePerGas ?? 0 )
198
- self . maxPriorityFeePerGas = options. resolveMaxPriorityFeePerGas ( maxPriorityFeePerGas ?? 0 )
199
- self . value = options. value ?? value
200
- self . from = options. from
201
- self . to = options. to ?? to
202
- self . accessList = options. accessList
203
- }
204
-
205
- public var noncePolicy : NoncePolicy ?
206
- public var maxFeePerGasPolicy : FeePerGasPolicy ?
207
- public var maxPriorityFeePerGasPolicy : FeePerGasPolicy ?
208
- public var gasPricePolicy : GasPricePolicy ?
209
- public var gasLimitPolicy : GasLimitPolicy ?
186
+ // public mutating func resolve( ) {
187
+ // // type cannot be changed here, and is ignored
188
+ // // FIXME: Add appropiate values of resolveAny
189
+ // self.nonce = options.resolveNonce(nonce)
190
+ // self.gasPrice = options.resolveGasPrice(gasPrice ?? 0)
191
+ // self.gasLimit = options.resolveGasLimit(gasLimit)
192
+ // self.maxFeePerGas = options.resolveMaxFeePerGas(maxFeePerGas ?? 0)
193
+ // self.maxPriorityFeePerGas = options.resolveMaxPriorityFeePerGas(maxPriorityFeePerGas ?? 0)
194
+ // self.value = options.value ?? value
195
+ // self.from = options.from
196
+ // self.to = options.to ?? to
197
+ // self.accessList = options.accessList
198
+ // }
199
+
200
+ public var noncePolicy : NoncePolicy
201
+ public var maxFeePerGasPolicy : FeePerGasPolicy
202
+ public var maxPriorityFeePerGasPolicy : FeePerGasPolicy
203
+ public var gasPricePolicy : GasPricePolicy
204
+ public var gasLimitPolicy : GasLimitPolicy
210
205
211
206
public static var emptyTransaction = CodableTransaction ( to: EthereumAddress . contractDeploymentAddress ( ) )
212
207
}
@@ -235,6 +230,12 @@ extension CodableTransaction: Codable {
235
230
// FIXME: This is duplication and should be fixed.
236
231
data = Data ( )
237
232
233
+ noncePolicy = . latest
234
+ gasLimitPolicy = . automatic
235
+ gasPricePolicy = . automatic
236
+ maxFeePerGasPolicy = . automatic
237
+ maxPriorityFeePerGasPolicy = . automatic
238
+
238
239
// capture any metadata that might be present
239
240
self . meta = try EthereumMetadata ( from: decoder)
240
241
}
@@ -303,24 +304,16 @@ extension CodableTransaction {
303
304
case manual( BigUInt )
304
305
}
305
306
306
- public enum NoncePolicy {
307
- case pending
308
- case latest
309
- case manual( BigUInt )
310
- }
311
-
312
307
public func resolveNonce( _ suggestedByNode: BigUInt ) -> BigUInt {
313
- guard let noncePolicy = self . noncePolicy else { return suggestedByNode }
314
308
switch noncePolicy {
315
- case . pending, . latest:
309
+ case . pending, . latest, . earliest :
316
310
return suggestedByNode
317
- case . manual ( let value) :
311
+ case . exact ( let value) :
318
312
return value
319
313
}
320
314
}
321
315
322
316
public func resolveGasPrice( _ suggestedByNode: BigUInt ) -> BigUInt {
323
- guard let gasPricePolicy = self . gasPricePolicy else { return suggestedByNode }
324
317
switch gasPricePolicy {
325
318
case . automatic, . withMargin:
326
319
return suggestedByNode
@@ -330,7 +323,6 @@ extension CodableTransaction {
330
323
}
331
324
332
325
public func resolveGasLimit( _ suggestedByNode: BigUInt ) -> BigUInt {
333
- guard let gasLimitPolicy = self . gasLimitPolicy else { return suggestedByNode }
334
326
switch gasLimitPolicy {
335
327
case . automatic, . withMargin:
336
328
return suggestedByNode
@@ -346,7 +338,6 @@ extension CodableTransaction {
346
338
}
347
339
348
340
public func resolveMaxFeePerGas( _ suggestedByNode: BigUInt ) -> BigUInt {
349
- guard let maxFeePerGasPolicy = self . maxFeePerGasPolicy else { return suggestedByNode }
350
341
switch maxFeePerGasPolicy {
351
342
case . automatic:
352
343
return suggestedByNode
@@ -356,42 +347,13 @@ extension CodableTransaction {
356
347
}
357
348
358
349
public func resolveMaxPriorityFeePerGas( _ suggestedByNode: BigUInt ) -> BigUInt {
359
- guard let maxPriorityFeePerGasPolicy = self . maxPriorityFeePerGasPolicy else { return suggestedByNode }
360
350
switch maxPriorityFeePerGasPolicy {
361
351
case . automatic:
362
352
return suggestedByNode
363
353
case . manual( let value) :
364
354
return value
365
355
}
366
356
}
367
-
368
- public func merge( _ otherOptions: CodableTransaction ? ) -> CodableTransaction {
369
- guard let other = otherOptions else { return self }
370
- var opts = CodableTransaction . emptyTransaction
371
- // opts.type = mergeIfNotNil(first: self.type, second: other.type)
372
-
373
- opts. from = mergeIfNotNil ( first: self . from, second: other. from)
374
- opts. chainID = mergeIfNotNil ( first: self . chainID, second: other. chainID)
375
- opts. gasLimitPolicy = mergeIfNotNil ( first: self . gasLimitPolicy, second: other. gasLimitPolicy)
376
- opts. gasPricePolicy = mergeIfNotNil ( first: self . gasPricePolicy, second: other. gasPricePolicy)
377
- opts. maxFeePerGasPolicy = mergeIfNotNil ( first: self . maxFeePerGasPolicy, second: other. maxFeePerGasPolicy)
378
- opts. maxPriorityFeePerGasPolicy = mergeIfNotNil ( first: self . maxPriorityFeePerGasPolicy, second: other. maxPriorityFeePerGasPolicy)
379
- // opts.value = mergeIfNotNil(first: self.value, second: other.value)
380
- opts. noncePolicy = mergeIfNotNil ( first: self . noncePolicy, second: other. noncePolicy)
381
- opts. callOnBlock = mergeIfNotNil ( first: self . callOnBlock, second: other. callOnBlock)
382
- return opts
383
- }
384
-
385
- /// Merges two sets of options by overriding the parameters from the first set by parameters from the second
386
- /// set if those are not nil.
387
- ///
388
- /// Returns default options if both parameters are nil.
389
- public static func merge( _ options: CodableTransaction ? , with other: CodableTransaction ? ) -> CodableTransaction ? {
390
- var newOptions = CodableTransaction . emptyTransaction // default has lowest priority
391
- newOptions = newOptions. merge ( options)
392
- newOptions = newOptions. merge ( other) // other has highest priority
393
- return newOptions
394
- }
395
357
}
396
358
397
359
0 commit comments