Skip to content

Commit 2c4b7ae

Browse files
ReadTransaction now accepts only password:String as a parameter,
and have no logic to assemble transactions. `Eth.estimateGas` now accepts only `CodableTransaction` as a parameter. Building.
1 parent cd5bc46 commit 2c4b7ae

File tree

6 files changed

+52
-31
lines changed

6 files changed

+52
-31
lines changed

Sources/web3swift/EthereumAPICalls/Ethereum/Eth+EstimateGas.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import Core
1111
extension web3.Eth {
1212

1313
// FIXME: Rewrite this to CodableTransaction
14-
public func estimateGas(for transaction: CodableTransaction, transactionOptions: CodableTransaction?) async throws -> BigUInt {
14+
public func estimateGas(for transaction: CodableTransaction) async throws -> BigUInt {
1515

1616
// FIXME: Something wrong with this. We should not to get parameters + options in one method.
17-
let request: APIRequest = .estimateGas(transaction, transactionOptions?.callOnBlock ?? .latest)
17+
let request: APIRequest = .estimateGas(transaction, transaction.callOnBlock ?? .latest)
1818
let response: APIResponse<BigUInt> = try await APIRequest.sendRequest(with: provider, for: request)
1919

20-
if let policy = transactionOptions?.gasLimitPolicy {
20+
if let policy = transaction.gasLimitPolicy {
2121
switch policy {
2222
case .automatic:
2323
return response.result

Sources/web3swift/HookedFunctions/Web3+BrowserFunctions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ extension web3.BrowserFunctions {
101101
// }
102102

103103
// FIXME: Rewrite this to CodableTransaction
104-
public func estimateGas(_ transaction: CodableTransaction, transactionOptions: CodableTransaction) async -> BigUInt? {
104+
public func estimateGas(_ transaction: CodableTransaction) async -> BigUInt? {
105105
do {
106-
let result = try await self.web3.eth.estimateGas(for: transaction, transactionOptions: transactionOptions)
106+
let result = try await self.web3.eth.estimateGas(for: transaction)
107107
return result
108108
} catch {
109109
return nil
@@ -131,7 +131,7 @@ extension web3.BrowserFunctions {
131131
let gasPrice = try await self.web3.eth.gasPrice()
132132
transaction.gasPrice = gasPrice
133133
options.gasPricePolicy = .manual(gasPrice)
134-
guard let gasEstimate = await self.estimateGas(transaction, transactionOptions: options) else {return (nil, nil)}
134+
guard let gasEstimate = await self.estimateGas(transaction) else {return (nil, nil)}
135135
transaction.gasLimit = gasEstimate
136136

137137
options.gasLimitPolicy = .limited(gasEstimate)

Sources/web3swift/Utils/ENS/ENSRegistry.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ public extension ENS {
7878
}
7979
guard let nameHash = NameHash.nameHash(node) else {throw Web3Error.processingError(desc: "Failed to get name hash")}
8080
guard let transaction = self.registryContract.write("setOwner", parameters: [nameHash, owner] as [AnyObject], extraData: Data(), transactionOptions: options) else {throw Web3Error.transactionSerializationError}
81-
guard let result = await password == nil ? try? transaction.send(): try? transaction.send(password: password!, transactionOptions: options) else {throw Web3Error.processingError(desc: "Can't send transaction")}
81+
guard let result = await password == nil
82+
? try? transaction.send()
83+
: try? transaction.send(password: password!)
84+
else {throw Web3Error.processingError(desc: "Can't send transaction")}
8285
return result
8386
}
8487

@@ -91,7 +94,10 @@ public extension ENS {
9194
guard let nameHash = NameHash.nameHash(node) else {throw Web3Error.processingError(desc: "Failed to get name hash")}
9295
guard let labelHash = NameHash.nameHash(label) else {throw Web3Error.processingError(desc: "Failed to get label hash")}
9396
guard let transaction = self.registryContract.write("setSubnodeOwner", parameters: [nameHash, labelHash, owner] as [AnyObject], extraData: Data(), transactionOptions: options) else {throw Web3Error.transactionSerializationError}
94-
guard let result = await password == nil ? try? transaction.send(): try? transaction.send(password: password!, transactionOptions: options) else {throw Web3Error.processingError(desc: "Can't send transaction")}
97+
guard let result = await password == nil
98+
? try? transaction.send()
99+
: try? transaction.send(password: password!)
100+
else {throw Web3Error.processingError(desc: "Can't send transaction")}
95101
return result
96102
}
97103

@@ -103,7 +109,10 @@ public extension ENS {
103109
}
104110
guard let nameHash = NameHash.nameHash(node) else {throw Web3Error.processingError(desc: "Failed to get name hash")}
105111
guard let transaction = self.registryContract.write("setResolver", parameters: [nameHash, resolver] as [AnyObject], extraData: Data(), transactionOptions: options) else {throw Web3Error.transactionSerializationError}
106-
guard let result = await password == nil ? try? transaction.send(): try? transaction.send(password: password!, transactionOptions: options) else {throw Web3Error.processingError(desc: "Can't send transaction")}
112+
guard let result = await password == nil
113+
? try? transaction.send()
114+
: try? transaction.send(password: password!)
115+
else {throw Web3Error.processingError(desc: "Can't send transaction")}
107116
return result
108117
}
109118

@@ -115,7 +124,10 @@ public extension ENS {
115124
}
116125
guard let nameHash = NameHash.nameHash(node) else {throw Web3Error.processingError(desc: "Failed to get name hash")}
117126
guard let transaction = self.registryContract.write("setTTL", parameters: [nameHash, ttl] as [AnyObject], extraData: Data(), transactionOptions: options) else {throw Web3Error.transactionSerializationError}
118-
guard let result = await password == nil ? try? transaction.send(): try? transaction.send(password: password!, transactionOptions: options) else {throw Web3Error.processingError(desc: "Can't send transaction")}
127+
guard let result = await password == nil
128+
? try? transaction.send()
129+
: try? transaction.send(password: password!)
130+
else {throw Web3Error.processingError(desc: "Can't send transaction")}
119131
return result
120132
}
121133
}

Sources/web3swift/Utils/ENS/ENSResolver.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public extension ENS {
9696
options.to = self.resolverContractAddress
9797
guard let nameHash = NameHash.nameHash(node) else {throw Web3Error.processingError(desc: "Failed to get name hash")}
9898
guard let transaction = self.resolverContract.write("setAddr", parameters: [nameHash, address] as [AnyObject], extraData: Data(), transactionOptions: options) else {throw Web3Error.transactionSerializationError}
99-
guard let result = await password == nil ? try? transaction.send(): try? transaction.send(password: password!, transactionOptions: options) else {throw Web3Error.processingError(desc: "Can't send transaction")}
99+
guard let result = await password == nil ? try? transaction.send(): try? transaction.send(password: password!) else {throw Web3Error.processingError(desc: "Can't send transaction")}
100100
return result
101101
}
102102

@@ -115,7 +115,10 @@ public extension ENS {
115115
options.to = self.resolverContractAddress
116116
guard let nameHash = NameHash.nameHash(node) else {throw Web3Error.processingError(desc: "Failed to get name hash")}
117117
guard let transaction = self.resolverContract.write("setName", parameters: [nameHash, name] as [AnyObject], extraData: Data(), transactionOptions: options) else {throw Web3Error.transactionSerializationError}
118-
guard let result = await password == nil ? try? transaction.send(): try? transaction.send(password: password!, transactionOptions: options) else {throw Web3Error.processingError(desc: "Can't send transaction")}
118+
guard let result = await password == nil
119+
? try? transaction.send()
120+
: try? transaction.send(password: password!)
121+
else {throw Web3Error.processingError(desc: "Can't send transaction")}
119122
return result
120123
}
121124

@@ -134,7 +137,10 @@ public extension ENS {
134137
options.to = self.resolverContractAddress
135138
guard let nameHash = NameHash.nameHash(node) else {throw Web3Error.processingError(desc: "Failed to get name hash")}
136139
guard let transaction = self.resolverContract.write("setContenthash", parameters: [nameHash, hash] as [AnyObject], extraData: Data(), transactionOptions: options) else {throw Web3Error.transactionSerializationError}
137-
guard let result = await password == nil ? try? transaction.send(): try? transaction.send(password: password!, transactionOptions: options) else {throw Web3Error.processingError(desc: "Can't send transaction")}
140+
guard let result = await password == nil
141+
? try? transaction.send()
142+
: try? transaction.send(password: password!)
143+
else {throw Web3Error.processingError(desc: "Can't send transaction")}
138144
return result
139145
}
140146

@@ -154,7 +160,10 @@ public extension ENS {
154160
options.to = self.resolverContractAddress
155161
guard let nameHash = NameHash.nameHash(node) else {throw Web3Error.processingError(desc: "Failed to get name hash")}
156162
guard let transaction = self.resolverContract.write("setABI", parameters: [nameHash, contentType.rawValue, data] as [AnyObject], extraData: Data(), transactionOptions: options) else {throw Web3Error.transactionSerializationError}
157-
guard let result = await password == nil ? try? transaction.send(): try? transaction.send(password: password!, transactionOptions: options) else {throw Web3Error.processingError(desc: "Can't send transaction")}
163+
guard let result = await password == nil
164+
? try? transaction.send()
165+
: try? transaction.send(password: password!)
166+
else {throw Web3Error.processingError(desc: "Can't send transaction")}
158167
return result
159168
}
160169

@@ -176,7 +185,10 @@ public extension ENS {
176185
let pubkeyWithoutPrefix = publicKey.getComponentsWithoutPrefix()
177186
guard let nameHash = NameHash.nameHash(node) else {throw Web3Error.processingError(desc: "Failed to get name hash")}
178187
guard let transaction = self.resolverContract.write("setPubkey", parameters: [nameHash, pubkeyWithoutPrefix.x, pubkeyWithoutPrefix.y] as [AnyObject], extraData: Data(), transactionOptions: options) else {throw Web3Error.transactionSerializationError}
179-
guard let result = await password == nil ? try? transaction.send(): try? transaction.send(password: password!, transactionOptions: options) else {throw Web3Error.processingError(desc: "Can't send transaction")}
188+
guard let result = await password == nil
189+
? try? transaction.send()
190+
: try? transaction.send(password: password!)
191+
else {throw Web3Error.processingError(desc: "Can't send transaction")}
180192
return result
181193
}
182194

@@ -195,7 +207,10 @@ public extension ENS {
195207
options.to = self.resolverContractAddress
196208
guard let nameHash = NameHash.nameHash(node) else {throw Web3Error.processingError(desc: "Failed to get name hash")}
197209
guard let transaction = self.resolverContract.write("setText", parameters: [nameHash, key, value] as [AnyObject], extraData: Data(), transactionOptions: options) else {throw Web3Error.transactionSerializationError}
198-
guard let result = await password == nil ? try? transaction.send(): try? transaction.send(password: password!, transactionOptions: options) else {throw Web3Error.processingError(desc: "Can't send transaction")}
210+
guard let result = await password == nil
211+
? try? transaction.send()
212+
: try? transaction.send(password: password!)
213+
else {throw Web3Error.processingError(desc: "Can't send transaction")}
199214
return result
200215
}
201216
}

Sources/web3swift/Web3/Web3+MutatingTransaction.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,26 +179,20 @@ public class WriteTransaction: ReadTransaction {
179179
}
180180

181181
// FIXME: Rewrite this to CodableTransaction
182-
public func send(password: String = "web3swift", transactionOptions: CodableTransaction? = nil) async throws -> TransactionSendingResult {
183-
var assembledTransaction = try await assembleTransaction(transactionOptions: transactionOptions)
184-
let mergedOptions = self.transactionOptions.merge(transactionOptions)
185-
var cleanedOptions = CodableTransaction.emptyTransaction
186-
cleanedOptions.from = mergedOptions.from
187-
cleanedOptions.to = mergedOptions.to
188-
182+
public func send(password: String = "web3swift") async throws -> TransactionSendingResult {
189183
if let attachedKeystoreManager = self.web3.provider.attachedKeystoreManager {
190-
guard let from = mergedOptions.from else {
191-
throw Web3Error.inputError(desc: "No 'from' field provided")
192-
}
193184
do {
194-
try Web3Signer.signTX(transaction: &assembledTransaction, keystore: attachedKeystoreManager, account: from, password: password)
185+
try Web3Signer.signTX(transaction: &transaction,
186+
keystore: attachedKeystoreManager,
187+
account: transaction.from ?? transaction.sender ?? EthereumAddress.contractDeploymentAddress(),
188+
password: password)
195189
} catch {
196190
throw Web3Error.inputError(desc: "Failed to locally sign a transaction")
197191
}
198-
return try await self.web3.eth.send(raw: assembledTransaction)
192+
return try await web3.eth.send(raw: transaction)
199193
}
200194
// MARK: Sending Data flow
201-
return try await web3.eth.send(assembledTransaction)
195+
return try await web3.eth.send(transaction)
202196
}
203197

204198
// FIXME: Rewrite this to CodableTransaction
@@ -207,7 +201,7 @@ public class WriteTransaction: ReadTransaction {
207201
optionsForGasEstimation: CodableTransaction) async throws -> BigUInt {
208202
switch policy {
209203
case .automatic, .withMargin, .limited:
210-
return try await web3.eth.estimateGas(for: assembledTransaction, transactionOptions: optionsForGasEstimation)
204+
return try await web3.eth.estimateGas(for: assembledTransaction)
211205
case .manual(let gasLimit):
212206
return gasLimit
213207
}

Sources/web3swift/Web3/Web3+ReadingTransaction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class ReadTransaction {
8989
assembledTransaction.value = mergedOptions.value
9090
}
9191

92-
return try await self.web3.eth.estimateGas(for: assembledTransaction, transactionOptions: optionsForGasEstimation)
92+
return try await self.web3.eth.estimateGas(for: assembledTransaction)
9393

9494
}
9595

0 commit comments

Comments
 (0)