Skip to content

Commit b9055ee

Browse files
Move chainID property back to optional.
Update tests
1 parent a3850c6 commit b9055ee

File tree

10 files changed

+60
-153
lines changed

10 files changed

+60
-153
lines changed

Sources/Core/Transaction/EncodableTransaction.swift

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public struct EncodableTransaction {
4949
/// the chainId that transaction is targeted for
5050
/// should be set for all types, except some Legacy transactions (Pre EIP-155)
5151
/// will not have this set
52-
public var chainID: BigUInt {
52+
public var chainID: BigUInt? {
5353
get { return envelope.chainID }
5454
set { envelope.chainID = newValue }
5555
}
@@ -164,9 +164,10 @@ public struct EncodableTransaction {
164164
public mutating func migrate(to type: TransactionType) {
165165
if self.type == type { return }
166166

167-
let newEnvelope = EnvelopeFactory.createEnvelope(type: type, to: self.envelope.to,
168-
nonce: self.envelope.nonce)
169-
self.envelope = newEnvelope
167+
// FIXME: Move it back
168+
// let newEnvelope = EnvelopeFactory.createEnvelope(type: type, to: self.envelope.to,
169+
// nonce: self.envelope.nonce)
170+
// self.envelope = newEnvelope
170171
}
171172

172173
/// Create a new EncodableTransaction from a raw stream of bytes from the blockchain
@@ -187,7 +188,7 @@ public struct EncodableTransaction {
187188
// FIXME: Add appropiate values of resolveAny
188189
self.nonce = options.resolveNonce(nonce)
189190
self.gasPrice = options.resolveGasPrice(gasPrice ?? 0)
190-
self.gasLimit = options.resolveGasLimit(gasLimit ?? 0)
191+
self.gasLimit = options.resolveGasLimit(gasLimit)
191192
self.maxFeePerGas = options.resolveMaxFeePerGas(maxFeePerGas ?? 0)
192193
self.maxPriorityFeePerGas = options.resolveMaxPriorityFeePerGas(maxPriorityFeePerGas ?? 0)
193194
self.value = options.value ?? value
@@ -230,15 +231,16 @@ extension EncodableTransaction: Codable {
230231
public func encode(to encoder: Encoder) throws {
231232
var containier = encoder.container(keyedBy: CodingKeys.self)
232233
try containier.encode(type.rawValue.hexString, forKey: .type)
233-
try containier.encode(from, forKey: .from)
234234
try containier.encode(nonce.hexString, forKey: .nonce)
235-
try containier.encode(chainID.hexString, forKey: .chainID)
236235
try containier.encode(accessList, forKey: .accessList)
237236
try containier.encode(data.toHexString().addHexPrefix(), forKey: .data)
238237
try containier.encode(value.hexString, forKey: .value)
239238

240239
// Encoding only fields with value.
241240
// TODO: Rewrite me somehow better.
241+
if let chainID = chainID {
242+
// try containier.encode(chainID.hexString, forKey: .chainID)
243+
}
242244
if !gasLimit.isZero {
243245
try containier.encode(gasLimit.hexString, forKey: .gasLimit)
244246
}
@@ -259,6 +261,10 @@ extension EncodableTransaction: Codable {
259261
if !to.address.elementsEqual("0x") {
260262
try containier.encode(to, forKey: .to)
261263
}
264+
265+
if let from = from {
266+
try containier.encode(from, forKey: .from)
267+
}
262268
}
263269

264270
}
@@ -291,11 +297,16 @@ extension EncodableTransaction {
291297
/// - s: signature s parameter (default 0) - will get set properly once signed
292298
/// - parameters: EthereumParameters object containing additional parametrs for the transaction like gas
293299
public init(type: TransactionType? = nil, to: EthereumAddress, nonce: BigUInt = 0,
294-
chainID: BigUInt? = nil, value: BigUInt? = nil, data: Data = Data(),
295-
v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0) {
300+
chainID: BigUInt = 0, value: BigUInt = 0, data: Data = Data(),
301+
gasLimit: BigUInt = 0, maxFeePerGas: BigUInt? = nil, maxPriorityFeePerGas: BigUInt? = nil, gasPrice: BigUInt? = nil,
302+
accessList: [AccessListEntry]? = nil, v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0) {
296303
// FIXME: This is duplication and should be fixed.
297304
self.data = data
298-
self.envelope = EnvelopeFactory.createEnvelope(type: type, to: to, nonce: nonce, v: v, r: r, s: s)
305+
self.gasPrice = gasPrice
306+
self.maxFeePerGas = maxFeePerGas
307+
self.maxPriorityFeePerGas = maxPriorityFeePerGas
308+
self.accessList = accessList
309+
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)
299310
}
300311
}
301312

Sources/Core/Transaction/Envelope/AbstractEnvelope.swift

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public protocol AbstractEnvelope: CustomStringConvertible { // possibly add Coda
7474

7575
var sender: EthereumAddress? { get }
7676

77-
var chainID: BigUInt { get set }
77+
var chainID: BigUInt? { get set }
7878

7979
/// On chain address that this transaction is being sent to
8080
var to: EthereumAddress { get set }
@@ -118,21 +118,6 @@ public protocol AbstractEnvelope: CustomStringConvertible { // possibly add Coda
118118
/// returns nil if a required field is not found in the decoder stream, or can't be decoded
119119
init?(rawValue: Data) // Decode from Ethereum Data
120120

121-
// pseudo memberwise initializer
122-
// accepts all common parameters (full description with default implementation below)
123-
// Note the `nil` parameters, even though non-optional in the struct, are there to allow
124-
// fallback to pulling the value from `options`. If options also does not have a value, a suitable default is used
125-
// precedence is as follows: direct parameter > options value > default value
126-
/// Default memberwse initializer that all envelopes must support
127-
/// - Parameters:
128-
/// - to: EthereumAddress of destination
129-
/// - nonce: nonce for the transaction
130-
/// - v: Signature V component
131-
/// - r: Signature R component
132-
/// - s: Signature S component
133-
/// - parameters: EthereumParameters struct containing any other required parameters
134-
init(to: EthereumAddress, nonce: BigUInt?, v: BigUInt, r: BigUInt, s: BigUInt, parameters: EncodableTransaction?)
135-
136121
/// Transaction encoder for transmission or signing
137122
/// - Parameters:
138123
/// - {default}: EncodeType enum

Sources/Core/Transaction/Envelope/EIP1559Envelope.swift

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public struct EIP1559Envelope: EIP2718Envelope {
1212

1313
// common parameters for any transaction
1414
public var nonce: BigUInt = 0
15-
public var chainID: BigUInt
15+
public var chainID: BigUInt?
1616

1717
public var from: EthereumAddress? {
1818
guard let publicKey = publicKey else { return nil }
@@ -65,25 +65,6 @@ public struct EIP1559Envelope: EIP2718Envelope {
6565
toReturn += "s: " + String(self.s) + "\n"
6666
return toReturn
6767
}
68-
69-
// FIXME: Delete me
70-
public var parameters: EncodableTransaction {
71-
get {
72-
return EncodableTransaction.emptyTransaction
73-
}
74-
set(val) {
75-
nonce = val.nonce ?? nonce
76-
chainID = val.chainID ?? chainID
77-
to = val.to ?? to
78-
value = val.value ?? value
79-
data = val.data ?? data
80-
gasLimit = val.gasLimit ?? gasLimit
81-
maxFeePerGas = val.maxFeePerGas ?? maxFeePerGas
82-
maxPriorityFeePerGas = val.maxPriorityFeePerGas ?? maxPriorityFeePerGas
83-
accessList = val.accessList ?? accessList
84-
}
85-
}
86-
8768
}
8869

8970
extension EIP1559Envelope {
@@ -229,23 +210,6 @@ extension EIP1559Envelope {
229210
}
230211
}
231212

232-
public init(to: EthereumAddress, nonce: BigUInt? = nil,
233-
v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0,
234-
parameters: EncodableTransaction? = nil) {
235-
self.to = to
236-
self.nonce = nonce ?? parameters?.nonce ?? 0
237-
self.chainID = parameters?.chainID ?? 0
238-
self.value = parameters?.value ?? 0
239-
self.data = parameters?.data ?? Data()
240-
self.v = v
241-
self.r = r
242-
self.s = s
243-
self.maxPriorityFeePerGas = parameters?.maxPriorityFeePerGas ?? 0
244-
self.maxFeePerGas = parameters?.maxFeePerGas ?? 0
245-
self.gasLimit = parameters?.gasLimit ?? 0
246-
self.accessList = parameters?.accessList ?? []
247-
}
248-
249213
// memberwise
250214
public init(to: EthereumAddress, nonce: BigUInt = 0,
251215
chainID: BigUInt = 0, value: BigUInt = 0, data: Data,

Sources/Core/Transaction/Envelope/EIP2930Envelope.swift

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public struct EIP2930Envelope: EIP2718Envelope {
1212

1313
// common parameters for any transaction
1414
public var nonce: BigUInt = 0
15-
public var chainID: BigUInt
15+
public var chainID: BigUInt?
1616
public var from: EthereumAddress?
1717
public var to: EthereumAddress
1818
public var value: BigUInt
@@ -44,24 +44,6 @@ public struct EIP2930Envelope: EIP2718Envelope {
4444
toReturn += "s: " + String(self.s) + "\n"
4545
return toReturn
4646
}
47-
48-
// FIXME: Delete me
49-
public var parameters: EncodableTransaction {
50-
get {
51-
return EncodableTransaction.emptyTransaction
52-
}
53-
set(val) {
54-
nonce = val.nonce ?? nonce
55-
chainID = val.chainID ?? chainID
56-
to = val.to ?? to
57-
value = val.value ?? value
58-
data = val.data ?? data
59-
gasLimit = val.gasLimit ?? gasLimit
60-
gasPrice = val.gasPrice ?? gasPrice
61-
accessList = val.accessList ?? accessList
62-
}
63-
}
64-
6547
}
6648

6749
extension EIP2930Envelope {
@@ -202,22 +184,6 @@ extension EIP2930Envelope {
202184
}
203185
}
204186

205-
public init(to: EthereumAddress, nonce: BigUInt? = nil,
206-
v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0,
207-
parameters: EncodableTransaction? = nil) {
208-
self.to = to
209-
self.nonce = nonce ?? parameters?.nonce ?? 0
210-
self.chainID = parameters?.chainID ?? 0
211-
self.value = parameters?.value ?? 0
212-
self.data = parameters?.data ?? Data()
213-
self.v = v
214-
self.r = r
215-
self.s = s
216-
self.gasPrice = parameters?.gasPrice ?? 0
217-
self.gasLimit = parameters?.gasLimit ?? 0
218-
self.accessList = parameters?.accessList ?? []
219-
}
220-
221187
// memberwise
222188
public init(to: EthereumAddress, nonce: BigUInt = 0,
223189
chainID: BigUInt = 0, value: BigUInt = 0, data: Data,

Sources/Core/Transaction/Envelope/EnvelopeFactory.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public struct EnvelopeFactory {
6464
}
6565
}
6666

67+
// MARK: Delete all default values in initializer, because of this is a internal factory, so it shouldn't be convenient,
68+
// rather then is should have no magic in itself.
6769
/// Description Create a new transaction envelope of the type dictated by the type parameter
6870
/// - Parameters:
6971
/// - type: TransactionType enum, dictates what kind of envelope to create defaults to .legacy if nil
@@ -74,14 +76,16 @@ public struct EnvelopeFactory {
7476
/// - s: signature s parameter (default 0) - will get set properly once signed
7577
/// - options: TransactionParameters containing additional parametrs for the transaction like gas
7678
/// - Returns: a new envelope of type dictated by 'type'
77-
static func createEnvelope(type: TransactionType? = nil, to: EthereumAddress, nonce: BigUInt = 0,
78-
v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0, parameters: EncodableTransaction? = nil) -> AbstractEnvelope {
79-
let envelopeType: TransactionType = type ?? parameters?.type ?? .legacy
79+
static func createEnvelope(type: TransactionType? = nil, to: EthereumAddress, nonce: BigUInt,
80+
chainID: BigUInt, value: BigUInt, data: Data,
81+
gasLimit: BigUInt, maxFeePerGas: BigUInt?, maxPriorityFeePerGas: BigUInt?, gasPrice: BigUInt?,
82+
accessList: [AccessListEntry]?, v: BigUInt, r: BigUInt, s: BigUInt) -> AbstractEnvelope {
83+
let envelopeType: TransactionType = type ?? .legacy
8084

8185
switch envelopeType {
82-
case .eip2930: return EIP2930Envelope(to: to, nonce: nonce, v: v, r: r, s: s, parameters: parameters)
83-
case .eip1559: return EIP1559Envelope(to: to, nonce: nonce, v: v, r: r, s: s, parameters: parameters)
84-
default: return LegacyEnvelope(to: to, nonce: nonce, v: v, r: r, s: s, parameters: parameters)
86+
case .eip2930: return EIP2930Envelope(to: to, nonce: nonce, chainID: chainID, value: value, data: data, gasPrice: gasPrice ?? 0, gasLimit: gasLimit, accessList: accessList, v: v, r: r, s: s)
87+
case .eip1559: return EIP1559Envelope(to: to, nonce: nonce, chainID: chainID, value: value, data: data, maxPriorityFeePerGas: maxPriorityFeePerGas ?? 0, maxFeePerGas: maxFeePerGas ?? 0, gasLimit: gasLimit, accessList: accessList, v: v, r: r, s: s)
88+
default: return LegacyEnvelope(to: to, nonce: nonce, chainID: chainID, value: value, data: data, gasPrice: gasPrice ?? 0, gasLimit: gasLimit, v: v, r: r, s: s)
8589
}
8690
}
8791
}

Sources/Core/Transaction/Envelope/LegacyEnvelope.swift

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public struct LegacyEnvelope: AbstractEnvelope {
1212

1313
// common parameters for any transaction
1414
public var nonce: BigUInt = 0
15-
public var chainID: BigUInt {
15+
public var chainID: BigUInt? {
1616
get {
1717
if let id = explicitChainID, id != 0 { return id }
1818
return impliedChainID
@@ -33,9 +33,9 @@ public struct LegacyEnvelope: AbstractEnvelope {
3333
// legacy chainID Mechanism
3434
private var explicitChainID: BigUInt? // set directly or via options
3535
// private var impliedChainID: BigUInt? // we calculate this once, or when explicitely asked to
36-
private var impliedChainID: BigUInt {
36+
private var impliedChainID: BigUInt? {
3737
if r == 0 && s == 0 { return v }
38-
if v == 27 || v == 28 || v < 35 { return 0 }
38+
if v == 27 || v == 28 || v < 35 { return nil }
3939
return ((v - 1) / 2) - 17
4040
}
4141

@@ -57,23 +57,6 @@ public struct LegacyEnvelope: AbstractEnvelope {
5757
toReturn += "s: " + String(self.s) + "\n"
5858
return toReturn
5959
}
60-
61-
// FIXME: Delete me
62-
public var parameters: EncodableTransaction {
63-
get {
64-
return EncodableTransaction.emptyTransaction
65-
}
66-
set(val) {
67-
nonce = val.nonce ?? nonce
68-
explicitChainID = val.chainID ?? explicitChainID
69-
to = val.to ?? to
70-
value = val.value ?? value
71-
data = val.data ?? data
72-
gasLimit = val.gasLimit ?? gasLimit
73-
gasPrice = val.gasPrice ?? gasPrice
74-
}
75-
}
76-
7760
}
7861

7962
extension LegacyEnvelope {
@@ -177,21 +160,6 @@ extension LegacyEnvelope {
177160
self.s = BigUInt(sData)
178161
}
179162

180-
public init(to: EthereumAddress, nonce: BigUInt? = nil,
181-
v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0,
182-
parameters: EncodableTransaction? = nil) {
183-
self.to = to
184-
self.nonce = nonce ?? parameters?.nonce ?? 0
185-
self.explicitChainID = parameters?.chainID // Legacy can have a nil ChainID
186-
self.value = parameters?.value ?? 0
187-
self.data = parameters?.data ?? Data()
188-
self.v = v
189-
self.r = r
190-
self.s = s
191-
self.gasPrice = parameters?.gasPrice ?? 0
192-
self.gasLimit = parameters?.gasLimit ?? 0
193-
}
194-
195163
// memberwise
196164
public init(to: EthereumAddress, nonce: BigUInt = 0,
197165
chainID: BigUInt? = nil, value: BigUInt = 0, data: Data,
@@ -225,7 +193,7 @@ extension LegacyEnvelope {
225193
switch type {
226194
case .transaction: fields = [self.nonce, self.gasPrice, self.gasLimit, self.to.addressData, self.value, self.data, v, r, s] as [AnyObject]
227195
case .signature:
228-
if chainID != 0 {
196+
if let chainID = self.chainID, chainID != 0 {
229197
fields = [self.nonce, self.gasPrice, self.gasLimit, self.to.addressData, self.value, self.data, chainID, BigUInt(0), BigUInt(0)] as [AnyObject]
230198
} else {
231199
fields = [self.nonce, self.gasPrice, self.gasLimit, self.to.addressData, self.value, self.data] as [AnyObject]

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,17 @@ extension ERC20BaseProperties {
219219
var transactionOptionsVAR = TransactionOptions.defaultOptions
220220
transactionOptionsVAR.callOnBlock = .latest
221221
let transactionOptions = transactionOptionsVAR
222-
async let namePromise = contract.read("name", parameters: [AnyObject](), extraData: Data(), transactionOptions: transactionOptions)?.decodedData()
222+
async let namePromise = contract
223+
.read("name", parameters: [AnyObject](), extraData: Data(), transactionOptions: transactionOptions)?
224+
.decodedData()
223225

224-
async let symbolPromise = try await contract.read("symbol", parameters: [AnyObject](), extraData: Data(), transactionOptions: transactionOptions)?.decodedData()
226+
async let symbolPromise = try await contract
227+
.read("symbol", parameters: [AnyObject](), extraData: Data(), transactionOptions: transactionOptions)?
228+
.decodedData()
225229

226-
async let decimalPromise = try await contract.read("decimals", parameters: [AnyObject](), extraData: Data(), transactionOptions: transactionOptions)?.decodedData()
230+
async let decimalPromise = try await contract
231+
.read("decimals", parameters: [AnyObject](), extraData: Data(), transactionOptions: transactionOptions)?
232+
.decodedData()
227233

228234
let resolvedPromises = try await ["name":namePromise, "symbol":symbolPromise, "decimals":decimalPromise]
229235

Tests/web3swiftTests/localTests/LocalTestCase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import web3swift
99
// while this class does show up in the navigator, it has no associated tests
1010
class LocalTestCase: XCTestCase {
1111

12-
static let url = URL(string: "http://localhost.proxyman.io:8545")!
12+
static let url = URL(string: "http://127.0.0.1:8545")!
1313

1414
override func setUp() async throws {
1515
let web3 = try! await Web3.new(LocalTestCase.url)

Tests/web3swiftTests/localTests/TransactionsTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,13 @@ class TransactionsTests: XCTestCase {
601601
var transaction = EncodableTransaction(
602602
to: EthereumAddress("0x3535353535353535353535353535353535353535")!,
603603
nonce: 9, value: 1_000_000_000_000_000_000, data: Data(),
604+
gasLimit: 21_000, gasPrice: 20_000_000_000,
604605
// FIXME: Return parameters here
605606
v: 0, r: 0, s: 0)
606-
transaction.gasPrice = 20_000_000_000
607-
transaction.gasLimit = 21_000
607+
// FIXME: Make me work back again.
608+
// Now it not connected to envelope.
609+
// transaction.gasPrice = 20_000_000_000
610+
// transaction.gasLimit = 21_000
608611
let privateKeyData = Data.fromHex("0x4646464646464646464646464646464646464646464646464646464646464646")!
609612
let publicKey = Utilities.privateToPublic(privateKeyData, compressed: false)
610613
let sender = Utilities.publicToAddress(publicKey!)

0 commit comments

Comments
 (0)