Skip to content

Commit f586dda

Browse files
committed
reset to original state (or close to it) --- prep for refactor of EthereumTransaction
1 parent a632e2e commit f586dda

File tree

3 files changed

+20
-44
lines changed

3 files changed

+20
-44
lines changed

Sources/web3swift/Contract/EthereumContract.swift

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -114,41 +114,25 @@ public struct EthereumContract: ContractProtocol {
114114
} else if extraData != Data() {
115115
fullData.append(extraData)
116116
}
117-
var opts = TransactionOptions()
118-
opts.gasPrice = .manual(0)
119-
opts.gasLimit = .manual(0)
120-
opts.value = 0
121-
opts.to = to
122-
123-
return EthereumTransaction(to: to, data: fullData, options: opts)
117+
let transaction = EthereumTransaction(gasPrice: BigUInt(0), gasLimit: BigUInt(0), to: to, value: BigUInt(0), data: fullData)
118+
return transaction
124119
}
125120

126121
public func method(_ method: String = "fallback", parameters: [AnyObject] = [AnyObject](), extraData: Data = Data()) -> EthereumTransaction? {
127122
guard let to = self.address else {return nil}
128123

129124
if (method == "fallback") {
130-
var opts = TransactionOptions()
131-
opts.gasPrice = .manual(0)
132-
opts.gasLimit = .manual(0)
133-
opts.value = 0
134-
opts.to = to
135-
136-
return EthereumTransaction(to: to, data: extraData, options: opts)
137-
125+
let transaction = EthereumTransaction(gasPrice: BigUInt(0), gasLimit: BigUInt(0), to: to, value: BigUInt(0), data: extraData)
126+
return transaction
138127
}
139128
let foundMethod = self.methods.filter { (key, value) -> Bool in
140129
return key == method
141130
}
142131
guard foundMethod.count == 1 else {return nil}
143132
let abiMethod = foundMethod[method]
144-
guard let encodedData = abiMethod?.encodeParameters(parameters) else { return nil }
145-
var opts = TransactionOptions()
146-
opts.gasPrice = .manual(0)
147-
opts.gasLimit = .manual(0)
148-
opts.value = 0
149-
opts.to = to
150-
151-
return EthereumTransaction(to: to, data: encodedData, options: opts)
133+
guard let encodedData = abiMethod?.encodeParameters(parameters) else {return nil}
134+
let transaction = EthereumTransaction(gasPrice: BigUInt(0), gasLimit: BigUInt(0), to: to, value: BigUInt(0), data: encodedData)
135+
return transaction
152136
}
153137

154138
public func parseEvent(_ eventLog: EventLog) -> (eventName: String?, eventData: [String: Any]?) {

Sources/web3swift/HookedFunctions/Web3+BrowserFunctions.swift

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,13 @@ extension web3.BrowserFunctions {
132132
do {
133133
var transaction = trans
134134
var options = opts
135-
var gasOptions = TransactionOptions()
136135
guard let _ = options.from else {return (nil, nil)}
137136
let gasPrice = try self.web3.eth.getGasPrice()
138-
gasOptions.gasPrice = .manual(gasPrice)
137+
transaction.gasPrice = gasPrice
139138
options.gasPrice = .manual(gasPrice)
140139
guard let gasEstimate = self.estimateGas(transaction, transactionOptions: options) else {return (nil, nil)}
141-
gasOptions.gasLimit = .limited(gasEstimate)
140+
transaction.gasLimit = gasEstimate
142141
options.gasLimit = .limited(gasEstimate)
143-
144-
transaction.applyOptions(gasOptions)
145-
146142
print(transaction)
147143
return (transaction, options)
148144
} catch {
@@ -173,28 +169,26 @@ extension web3.BrowserFunctions {
173169
public func signTransaction(_ trans: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") -> String? {
174170
do {
175171
var transaction = trans
176-
var gasOptions = TransactionOptions()
177-
guard let from = transactionOptions.from else { return nil }
178-
guard let keystoreManager = self.web3.provider.attachedKeystoreManager else { return nil }
179-
guard let gasPricePolicy = transactionOptions.gasPrice else { return nil }
180-
guard let gasLimitPolicy = transactionOptions.gasLimit else { return nil }
181-
guard let noncePolicy = transactionOptions.nonce else { return nil }
172+
guard let from = transactionOptions.from else {return nil}
173+
guard let keystoreManager = self.web3.provider.attachedKeystoreManager else {return nil}
174+
guard let gasPricePolicy = transactionOptions.gasPrice else {return nil}
175+
guard let gasLimitPolicy = transactionOptions.gasLimit else {return nil}
176+
guard let noncePolicy = transactionOptions.nonce else {return nil}
182177
switch gasPricePolicy {
183178
case .manual(let gasPrice):
184-
gasOptions.gasPrice = .manual(gasPrice)
179+
transaction.gasPrice = gasPrice
185180
default:
186181
let gasPrice = try self.web3.eth.getGasPrice()
187-
gasOptions.gasPrice = .manual(gasPrice)
182+
transaction.gasPrice = gasPrice
188183
}
189184

190185
switch gasLimitPolicy {
191186
case .manual(let gasLimit):
192-
gasOptions.gasLimit = .manual(gasLimit)
187+
transaction.gasLimit = gasLimit
193188
default:
194189
let gasLimit = try self.web3.eth.estimateGas(transaction, transactionOptions: transactionOptions)
195-
gasOptions.gasLimit = .manual(gasLimit)
190+
transaction.gasLimit = gasLimit
196191
}
197-
transaction.applyOptions(gasOptions)
198192

199193
switch noncePolicy {
200194
case .manual(let nonce):
@@ -211,7 +205,7 @@ extension web3.BrowserFunctions {
211205
guard let keystore = keystoreManager.walletForAddress(from) else {return nil}
212206
try Web3Signer.signTX(transaction: &transaction, keystore: keystore, account: from, password: password)
213207
print(transaction)
214-
let signedData = transaction.encode()?.toHexString().addHexPrefix()
208+
let signedData = transaction.encode(forSignature: false, chainID: nil)?.toHexString().addHexPrefix()
215209
return signedData
216210
} catch {
217211
return nil

Tests/web3swiftTests/localTests/web3swiftBasicLocalNodeTests.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ class web3swiftBasicLocalNodeTests: XCTestCase {
9292
print("Balance after from: " + balanceAfterFrom.description)
9393

9494
XCTAssert(balanceAfterTo - balanceBeforeTo == valueToSend)
95-
let options = details.transaction.getOptions()
96-
let txnGasPrice = options.resolveGasPrice(0)
97-
XCTAssert(balanceBeforeFrom - (balanceAfterFrom + receipt.gasUsed * txnGasPrice) == valueToSend)
95+
XCTAssert(balanceBeforeFrom - (balanceAfterFrom + receipt.gasUsed * details.transaction.gasPrice) == valueToSend)
9896
}
9997

10098
// FIXME: Crashes on CI/CD

0 commit comments

Comments
 (0)