Skip to content

Commit 7de0c9f

Browse files
Merge TransactionOptions into CodableTransaction
Building, most tests are green, but not all. (Remote all, local not)
1 parent 3fd2db2 commit 7de0c9f

37 files changed

+660
-564
lines changed

Sources/Core/Transaction/CodableTransaction.swift

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public struct CodableTransaction {
183183
return self.envelope.encode(for: type)
184184
}
185185

186-
public mutating func applyOptions(_ options: TransactionOptions) {
186+
public mutating func applyOptions(_ options: CodableTransaction) {
187187
// type cannot be changed here, and is ignored
188188
// FIXME: Add appropiate values of resolveAny
189189
self.nonce = options.resolveNonce(nonce)
@@ -197,6 +197,12 @@ public struct CodableTransaction {
197197
self.accessList = options.accessList
198198
}
199199

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?
205+
200206
public static var emptyTransaction = CodableTransaction(to: EthereumAddress.contractDeploymentAddress())
201207
}
202208

@@ -297,6 +303,90 @@ extension CodableTransaction {
297303
case latest
298304
case manual(BigUInt)
299305
}
306+
307+
public func resolveNonce(_ suggestedByNode: BigUInt) -> BigUInt {
308+
guard let noncePolicy = self.noncePolicy else { return suggestedByNode }
309+
switch noncePolicy {
310+
case .pending, .latest:
311+
return suggestedByNode
312+
case .manual(let value):
313+
return value
314+
}
315+
}
316+
317+
public func resolveGasPrice(_ suggestedByNode: BigUInt) -> BigUInt {
318+
guard let gasPricePolicy = self.gasPricePolicy else { return suggestedByNode }
319+
switch gasPricePolicy {
320+
case .automatic, .withMargin:
321+
return suggestedByNode
322+
case .manual(let value):
323+
return value
324+
}
325+
}
326+
327+
public func resolveGasLimit(_ suggestedByNode: BigUInt) -> BigUInt {
328+
guard let gasLimitPolicy = self.gasLimitPolicy else { return suggestedByNode }
329+
switch gasLimitPolicy {
330+
case .automatic, .withMargin:
331+
return suggestedByNode
332+
case .manual(let value):
333+
return value
334+
case .limited(let limit):
335+
if limit <= suggestedByNode {
336+
return suggestedByNode
337+
} else {
338+
return limit
339+
}
340+
}
341+
}
342+
343+
public func resolveMaxFeePerGas(_ suggestedByNode: BigUInt) -> BigUInt {
344+
guard let maxFeePerGasPolicy = self.maxFeePerGasPolicy else { return suggestedByNode }
345+
switch maxFeePerGasPolicy {
346+
case .automatic:
347+
return suggestedByNode
348+
case .manual(let value):
349+
return value
350+
}
351+
}
352+
353+
public func resolveMaxPriorityFeePerGas(_ suggestedByNode: BigUInt) -> BigUInt {
354+
guard let maxPriorityFeePerGasPolicy = self.maxPriorityFeePerGasPolicy else { return suggestedByNode }
355+
switch maxPriorityFeePerGasPolicy {
356+
case .automatic:
357+
return suggestedByNode
358+
case .manual(let value):
359+
return value
360+
}
361+
}
362+
363+
public func merge(_ otherOptions: CodableTransaction?) -> CodableTransaction {
364+
guard let other = otherOptions else { return self }
365+
var opts = CodableTransaction.emptyTransaction
366+
// opts.type = mergeIfNotNil(first: self.type, second: other.type)
367+
368+
opts.from = mergeIfNotNil(first: self.from, second: other.from)
369+
opts.chainID = mergeIfNotNil(first: self.chainID, second: other.chainID)
370+
opts.gasLimitPolicy = mergeIfNotNil(first: self.gasLimitPolicy, second: other.gasLimitPolicy)
371+
opts.gasPricePolicy = mergeIfNotNil(first: self.gasPricePolicy, second: other.gasPricePolicy)
372+
opts.maxFeePerGasPolicy = mergeIfNotNil(first: self.maxFeePerGasPolicy, second: other.maxFeePerGasPolicy)
373+
opts.maxPriorityFeePerGasPolicy = mergeIfNotNil(first: self.maxPriorityFeePerGasPolicy, second: other.maxPriorityFeePerGasPolicy)
374+
// opts.value = mergeIfNotNil(first: self.value, second: other.value)
375+
opts.noncePolicy = mergeIfNotNil(first: self.noncePolicy, second: other.noncePolicy)
376+
opts.callOnBlock = mergeIfNotNil(first: self.callOnBlock, second: other.callOnBlock)
377+
return opts
378+
}
379+
380+
/// Merges two sets of options by overriding the parameters from the first set by parameters from the second
381+
/// set if those are not nil.
382+
///
383+
/// Returns default options if both parameters are nil.
384+
public static func merge(_ options: CodableTransaction?, with other: CodableTransaction?) -> CodableTransaction? {
385+
var newOptions = CodableTransaction.emptyTransaction // default has lowest priority
386+
newOptions = newOptions.merge(options)
387+
newOptions = newOptions.merge(other) // other has highest priority
388+
return newOptions
389+
}
300390
}
301391

302392

@@ -342,3 +432,12 @@ extension CodableTransaction {
342432
}
343433

344434
extension CodableTransaction: APIRequestParameterType { }
435+
436+
private func mergeIfNotNil<T>(first: T?, second: T?) -> T? {
437+
if second != nil {
438+
return second
439+
} else if first != nil {
440+
return first
441+
}
442+
return nil
443+
}

0 commit comments

Comments
 (0)