Skip to content

Commit 4b4a0ed

Browse files
committed
Add policies. Remove policy vars from codable transaction
1 parent 70ba3b8 commit 4b4a0ed

20 files changed

+181
-154
lines changed

Sources/Core/Transaction/CodableTransaction.swift

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import BigInt
1212
/// while all fields in this struct are optional, they are not necessarily
1313
/// optional for the type of transaction they apply to.
1414
public struct CodableTransaction {
15-
public typealias NoncePolicy = BlockNumber
1615
/// internal acccess only. The transaction envelope object itself that contains all the transaction data
1716
/// and type specific implementation
1817
internal var envelope: AbstractEnvelope
@@ -176,24 +175,13 @@ public struct CodableTransaction {
176175
self.envelope = env
177176
// FIXME: This is duplication and should be fixed.
178177
data = Data()
179-
noncePolicy = .latest
180-
gasLimitPolicy = .automatic
181-
gasPricePolicy = .automatic
182-
maxFeePerGasPolicy = .automatic
183-
maxPriorityFeePerGasPolicy = .automatic
184178
}
185179

186180
/// - Returns: a raw bytestream of the transaction, encoded according to the transactionType
187181
public func encode(for type: EncodeType = .transaction) -> Data? {
188182
return self.envelope.encode(for: type)
189183
}
190184

191-
public var noncePolicy: NoncePolicy
192-
public var maxFeePerGasPolicy: FeePerGasPolicy
193-
public var maxPriorityFeePerGasPolicy: PriorityFeePerGasPolicy
194-
public var gasPricePolicy: GasPricePolicy
195-
public var gasLimitPolicy: GasLimitPolicy
196-
197185
public static var emptyTransaction = CodableTransaction(to: EthereumAddress.contractDeploymentAddress())
198186
}
199187

@@ -221,12 +209,6 @@ extension CodableTransaction: Codable {
221209
// FIXME: This is duplication and should be fixed.
222210
data = Data()
223211

224-
noncePolicy = .latest
225-
gasLimitPolicy = .automatic
226-
gasPricePolicy = .automatic
227-
maxFeePerGasPolicy = .automatic
228-
maxPriorityFeePerGasPolicy = .automatic
229-
230212
// capture any metadata that might be present
231213
self.meta = try TransactionMetadata(from: decoder)
232214
}
@@ -312,11 +294,6 @@ extension CodableTransaction {
312294
// FIXME: This is duplication and should be fixed.
313295
self.data = data
314296
self.accessList = accessList
315-
self.gasLimitPolicy = .automatic
316-
self.noncePolicy = .pending
317-
self.gasPricePolicy = .automatic
318-
self.maxFeePerGasPolicy = .automatic
319-
self.maxPriorityFeePerGasPolicy = .automatic
320297
self.callOnBlock = .latest
321298

322299
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)

Sources/Core/Transaction/Policies.swift

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import Foundation
99
import BigInt
1010

11+
public typealias NoncePolicy = BlockNumber
12+
1113
public enum GasLimitPolicy {
1214
case automatic
1315
case manual(BigUInt)
@@ -21,12 +23,43 @@ public enum GasPricePolicy {
2123
case withMargin(Double)
2224
}
2325

24-
public enum PriorityFeePerGasPolicy {
26+
public enum FeePerGasPolicy {
2527
case automatic
2628
case manual(BigUInt)
2729
}
2830

29-
public enum FeePerGasPolicy {
31+
public enum PriorityFeePerGasPolicy {
3032
case automatic
3133
case manual(BigUInt)
3234
}
35+
36+
public struct Policies {
37+
public let noncePolicy: NoncePolicy
38+
public let gasLimitPolicy: GasLimitPolicy
39+
public let gasPricePolicy: GasPricePolicy
40+
public let maxFeePerGasPolicy: FeePerGasPolicy
41+
public let maxPriorityFeePerGasPolicy: PriorityFeePerGasPolicy
42+
43+
public init(
44+
noncePolicy: NoncePolicy = .latest,
45+
gasLimitPolicy: GasLimitPolicy = .automatic,
46+
gasPricePolicy: GasPricePolicy = .automatic,
47+
maxFeePerGasPolicy: FeePerGasPolicy = .automatic,
48+
maxPriorityFeePerGasPolicy: PriorityFeePerGasPolicy = .automatic) {
49+
self.noncePolicy = noncePolicy
50+
self.gasLimitPolicy = gasLimitPolicy
51+
self.gasPricePolicy = gasPricePolicy
52+
self.maxFeePerGasPolicy = maxFeePerGasPolicy
53+
self.maxPriorityFeePerGasPolicy = maxPriorityFeePerGasPolicy
54+
}
55+
56+
public static var auto: Policies {
57+
return Policies(
58+
noncePolicy: .latest,
59+
gasLimitPolicy: .automatic,
60+
gasPricePolicy: .automatic,
61+
maxFeePerGasPolicy: .automatic,
62+
maxPriorityFeePerGasPolicy: .automatic
63+
)
64+
}
65+
}

Sources/web3swift/Operations/WriteOperation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Core
1111
public class WriteOperation: ReadOperation {
1212

1313
// FIXME: Rewrite this to CodableTransaction
14-
public func writeToChain(password: String) async throws -> TransactionSendingResult {
14+
public func writeToChain(password: String, policies: Policies = .auto) async throws -> TransactionSendingResult {
1515
// TODO: might change to be dependency
1616
let resolver = PolicyResolver(provider: web3.provider)
1717
try await resolver.resolveAll(for: &transaction)

Sources/web3swift/Tokens/ERC1155/Web3+ERC1155.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ public class ERC1155: IERC1155 {
136136
public func supportsInterface(interfaceID: String) async throws -> Bool {
137137
let contract = self.contract
138138
self.transaction.callOnBlock = .latest
139-
self.transaction.gasLimitPolicy = .manual(30000)
139+
// FIXME: check if this still works
140+
self.transaction.gasLimit = 300000
141+
// self.transaction.gasLimitPolicy = .manual(30000)
140142
let result = try await contract.createReadOperation("supportsInterface", parameters: [interfaceID] as [AnyObject], extraData: Data() )!.callContractMethod()
141143
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
142144
return res

Sources/web3swift/Tokens/ERC1400/Web3+ERC1400.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,9 @@ extension ERC1400: IERC777 {
686686
public func supportsInterface(interfaceID: String) async throws -> Bool {
687687
let contract = self.contract
688688
self.transaction.callOnBlock = .latest
689-
self.transaction.gasLimitPolicy = .manual(30000)
689+
// FIXME: test if this still works
690+
self.transaction.gasLimit = 30000
691+
// self.transaction.gasLimitPolicy = .manual(30000)
690692
let result = try await contract.createReadOperation("supportsInterface", parameters: [interfaceID] as [AnyObject], extraData: Data() )!.callContractMethod()
691693
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
692694
return res

Sources/web3swift/Tokens/ERC1410/Web3+ERC1410.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,9 @@ extension ERC1410: IERC777 {
452452
public func supportsInterface(interfaceID: String) async throws -> Bool {
453453
let contract = self.contract
454454
self.transaction.callOnBlock = .latest
455-
self.transaction.gasLimitPolicy = .manual(30000)
455+
// FIXME:
456+
self.transaction.gasLimit = 30000
457+
// self.transaction.gasLimitPolicy = .manual(30000)
456458
let result = try await contract.createReadOperation("supportsInterface", parameters: [interfaceID] as [AnyObject], extraData: Data() )!.callContractMethod()
457459
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
458460
return res

Sources/web3swift/Tokens/ERC1633/Web3+ERC1633.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ public class ERC1633: IERC1633, ERC20BaseProperties {
180180
public func supportsInterface(interfaceID: String) async throws -> Bool {
181181
let contract = self.contract
182182
self.transaction.callOnBlock = .latest
183-
self.transaction.gasLimitPolicy = .manual(30000)
183+
// FIXME:
184+
self.transaction.gasLimit = 30000
185+
// self.transaction.gasLimitPolicy = .manual(30000)
184186
let result = try await contract.createReadOperation("supportsInterface", parameters: [interfaceID] as [AnyObject], extraData: Data() )!.callContractMethod()
185187
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
186188
return res

Sources/web3swift/Tokens/ERC721/Web3+ERC721.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ public class ERC721: IERC721 {
192192
public func supportsInterface(interfaceID: String) async throws -> Bool {
193193
let contract = self.contract
194194
transaction.callOnBlock = .latest
195-
transaction.gasLimitPolicy = .manual(30000)
195+
// FIXME:
196+
self.transaction.gasLimit = 30000
197+
// transaction.gasLimitPolicy = .manual(30000)
196198
let result = try await contract.createReadOperation("supportsInterface", parameters: [interfaceID] as [AnyObject], extraData: Data() )!.callContractMethod()
197199
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
198200
return res

Sources/web3swift/Tokens/ERC721x/Web3+ERC721x.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ public class ERC721x: IERC721x {
174174
public func supportsInterface(interfaceID: String) async throws -> Bool {
175175
let contract = self.contract
176176
self.transaction.callOnBlock = .latest
177-
self.transaction.gasLimitPolicy = .manual(30000)
177+
// FIXME:
178+
self.transaction.gasLimit = 30000
179+
// self.transaction.gasLimitPolicy = .manual(30000)
178180
let result = try await contract.createReadOperation("supportsInterface", parameters: [interfaceID] as [AnyObject], extraData: Data() )!.callContractMethod()
179181
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
180182
return res

Sources/web3swift/Tokens/ERC777/Web3+ERC777.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ public class ERC777: IERC777, ERC20BaseProperties {
361361
public func supportsInterface(interfaceID: String) async throws -> Bool {
362362
let contract = self.contract
363363
self.transaction.callOnBlock = .latest
364-
self.transaction.gasLimitPolicy = .manual(30000)
364+
// FIXME:
365+
self.transaction.gasLimit = 30000
366+
// self.transaction.gasLimitPolicy = .manual(30000)
365367
let result = try await contract.createReadOperation("supportsInterface", parameters: [interfaceID] as [AnyObject], extraData: Data() )!.callContractMethod()
366368
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
367369
return res

0 commit comments

Comments
 (0)