Skip to content

Commit dcdb273

Browse files
Extend AbstractEnvelope to all gas related properties,
to let them get values by resolver, to reach this had made them optional.
1 parent d040e49 commit dcdb273

File tree

7 files changed

+41
-15
lines changed

7 files changed

+41
-15
lines changed

Sources/Core/Transaction/CodableTransaction.swift

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,23 @@ public struct CodableTransaction {
7979
}
8080

8181
/// the price per gas unit for the tranaction (Legacy and EIP-2930 only)
82-
public internal (set) var gasPrice: BigUInt?
82+
public internal (set) var gasPrice: BigUInt? {
83+
get { return envelope.gasPrice }
84+
set { return envelope.gasPrice = newValue }
85+
}
8386

8487
/// the max base fee per gas unit (EIP-1559 only)
8588
/// this value must be >= baseFee + maxPriorityFeePerGas
86-
public internal (set) var maxFeePerGas: BigUInt?
89+
public internal (set) var maxFeePerGas: BigUInt? {
90+
get { return envelope.maxFeePerGas }
91+
set { return envelope.maxFeePerGas = newValue }
92+
}
8793

8894
/// the maximum tip to pay the miner (EIP-1559 only)
89-
public internal (set) var maxPriorityFeePerGas: BigUInt?
95+
public internal (set) var maxPriorityFeePerGas: BigUInt? {
96+
get { return envelope.maxPriorityFeePerGas }
97+
set { return envelope.maxPriorityFeePerGas = newValue }
98+
}
9099

91100
public var callOnBlock: BlockNumber?
92101

@@ -317,7 +326,7 @@ extension CodableTransaction {
317326
let oracle = Oracle(provider)
318327
switch gasPricePolicy {
319328
case .automatic, .withMargin:
320-
return await oracle.gasPriceLegacyPercentiles().max()!
329+
return await oracle.gasPriceLegacyPercentiles().max() ?? 0
321330
case .manual(let value):
322331
return value
323332
}
@@ -344,7 +353,7 @@ extension CodableTransaction {
344353
let oracle = Oracle(provider)
345354
switch maxFeePerGasPolicy {
346355
case .automatic:
347-
return await oracle.baseFeePercentiles().max()!
356+
return await oracle.baseFeePercentiles().max() ?? 0
348357
case .manual(let value):
349358
return value
350359
}
@@ -354,7 +363,7 @@ extension CodableTransaction {
354363
let oracle = Oracle(provider)
355364
switch maxPriorityFeePerGasPolicy {
356365
case .automatic:
357-
return await oracle.tipFeePercentiles().max()!
366+
return await oracle.tipFeePercentiles().max() ?? 0
358367
case .manual(let value):
359368
return value
360369
}
@@ -395,9 +404,6 @@ extension CodableTransaction {
395404
accessList: [AccessListEntry]? = nil, v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0) {
396405
// FIXME: This is duplication and should be fixed.
397406
self.data = data
398-
self.gasPrice = gasPrice
399-
self.maxFeePerGas = maxFeePerGas
400-
self.maxPriorityFeePerGas = maxPriorityFeePerGas
401407
self.accessList = accessList
402408
self.gasLimitPolicy = .automatic
403409
self.noncePolicy = .pending

Sources/Core/Transaction/Envelope/AbstractEnvelope.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ protocol AbstractEnvelope: CustomStringConvertible { // possibly add Codable?
8383
var value: BigUInt { get set }
8484

8585
var gasLimit: BigUInt { get set }
86+
87+
var gasPrice: BigUInt? { get set }
88+
89+
/// the max base fee per gas unit (EIP-1559 only)
90+
/// this value must be >= baseFee + maxPriorityFeePerGas
91+
var maxFeePerGas: BigUInt? { get set }
92+
93+
/// the maximum tip to pay the miner (EIP-1559 only)
94+
var maxPriorityFeePerGas: BigUInt? { get set }
8695

8796
/// Any encoded data accompanying the transaction
8897
var data: Data { get set }

Sources/Core/Transaction/Envelope/EIP1559Envelope.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ public struct EIP1559Envelope: EIP2718Envelope {
2828

2929
// EIP-1559 specific parameters
3030
public var gasLimit: BigUInt
31+
32+
var gasPrice: BigUInt? = nil
33+
3134
/// Value of the tip to the miner for transaction processing.
3235
///
3336
/// Full amount of this variable goes to a miner.
34-
public var maxPriorityFeePerGas: BigUInt
37+
public var maxPriorityFeePerGas: BigUInt?
3538
/// Value of the fee for one gas unit
3639
///
3740
/// This value should be greater than sum of:
@@ -44,7 +47,7 @@ public struct EIP1559Envelope: EIP2718Envelope {
4447
/// If amount of this will be **lower** than sum of `Block.baseFeePerGas` and `maxPriorityFeePerGas`
4548
/// miner will recieve amount calculated by the following equation: `maxFeePerGas - Block.baseFeePerGas`
4649
/// where 'Block' is the block that the transaction will be included.
47-
public var maxFeePerGas: BigUInt
50+
public var maxFeePerGas: BigUInt?
4851
public var accessList: [AccessListEntry] // from EIP-2930
4952

5053
// for CustomStringConvertible

Sources/Core/Transaction/Envelope/EIP2930Envelope.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@ public struct EIP2930Envelope: EIP2718Envelope {
2222
public var s: BigUInt
2323

2424
// EIP-2930 specific parameters
25-
public var gasPrice: BigUInt = 0
25+
public var gasPrice: BigUInt? = 0
2626
public var gasLimit: BigUInt = 0
2727
public var accessList: [AccessListEntry] = []
2828
public var publicKey: Data?
2929

30+
var maxFeePerGas: BigUInt? = nil
31+
var maxPriorityFeePerGas: BigUInt? = nil
32+
3033
// for CustomStringConvertible
3134
public var description: String {
3235
var toReturn = ""
3336
toReturn += "Type: " + String(describing: self.type) + "\n"
3437
toReturn += "chainID: " + String(describing: self.chainID) + "\n"
3538
toReturn += "Nonce: " + String(describing: self.nonce) + "\n"
36-
toReturn += "Gas price: " + String(self.gasPrice) + "\n"
39+
toReturn += "Gas price: " + String(self.gasPrice ?? 0) + "\n"
3740
toReturn += "Gas limit: " + String(describing: self.gasLimit) + "\n"
3841
toReturn += "To: " + self.to.address + "\n"
3942
toReturn += "Value: " + String(describing: self.value) + "\n"

Sources/Core/Transaction/Envelope/LegacyEnvelope.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ public struct LegacyEnvelope: AbstractEnvelope {
2727
public var s: BigUInt
2828

2929
// legacy specific parameters
30-
public var gasPrice: BigUInt = 0
30+
public var gasPrice: BigUInt? = 0
3131
public var gasLimit: BigUInt = 0
3232

33+
var maxFeePerGas: BigUInt? = nil
34+
var maxPriorityFeePerGas: BigUInt? = nil
35+
3336
// legacy chainID Mechanism
3437
private var explicitChainID: BigUInt? // set directly or via options
3538
// private var impliedChainID: BigUInt? // we calculate this once, or when explicitely asked to
@@ -44,7 +47,7 @@ public struct LegacyEnvelope: AbstractEnvelope {
4447
var toReturn = ""
4548
toReturn += "Type: " + String(describing: self.type) + "\n"
4649
toReturn += "Nonce: " + String(describing: self.nonce) + "\n"
47-
toReturn += "Gas price: " + String(self.gasPrice) + "\n"
50+
toReturn += "Gas price: " + String(self.gasPrice ?? 0) + "\n"
4851
toReturn += "Gas limit: " + String(describing: self.gasLimit) + "\n"
4952
toReturn += "To: " + self.to.address + "\n"
5053
toReturn += "Value: " + String(describing: self.value) + "\n"

Sources/web3swift/Operations/ReadTransaction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ 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)
3738
// MARK: Read data from ABI flow
3839
// FIXME: This should be dropped, and after `execute()` call, just to decode raw data.
3940
let data: Data = try await self.web3.eth.callTransaction(transaction)

Sources/web3swift/Operations/WriteOperation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ 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)
1516
if let attachedKeystoreManager = self.web3.provider.attachedKeystoreManager {
1617
do {
1718
try Web3Signer.signTX(transaction: &transaction,

0 commit comments

Comments
 (0)