Skip to content

Commit cd5bc46

Browse files
Clean send(: CodableTransaction) code.
Add `ReadTransaction.send` attachedKeystoreManager logic for code signing.
1 parent 7b6e0e8 commit cd5bc46

File tree

2 files changed

+17
-49
lines changed

2 files changed

+17
-49
lines changed

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

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

1313
// FIXME: Rewrite this to CodableTransaction
14-
public func send(_ transaction: CodableTransaction, transactionOptions: CodableTransaction? = nil, password: String = "web3swift") async throws -> TransactionSendingResult {
15-
// print(transaction)
16-
var assembledTransaction: CodableTransaction = transaction
17-
18-
var mergedOptions = self.web3.transactionOptions.merge(transactionOptions)
19-
20-
var forAssemblyPipeline: (CodableTransaction, CodableTransaction) = (assembledTransaction, mergedOptions)
21-
22-
// usually not calling
23-
// Can't find where this hooks are implemented.
24-
for hook in self.web3.preSubmissionHooks {
25-
let hookResult = hook.function(forAssemblyPipeline)
26-
if hookResult.2 {
27-
forAssemblyPipeline = (hookResult.0, hookResult.1)
28-
}
29-
30-
let shouldContinue = hookResult.2
31-
if !shouldContinue {
32-
throw Web3Error.processingError(desc: "Transaction is canceled by middleware")
33-
}
34-
}
35-
36-
assembledTransaction = forAssemblyPipeline.0
37-
mergedOptions = forAssemblyPipeline.1
38-
39-
40-
if let attachedKeystoreManager = self.web3.provider.attachedKeystoreManager {
41-
guard let from = mergedOptions.from else {
42-
throw Web3Error.inputError(desc: "No 'from' field provided")
43-
}
44-
do {
45-
try Web3Signer.signTX(transaction: &assembledTransaction, keystore: attachedKeystoreManager, account: from, password: password)
46-
} catch {
47-
throw Web3Error.inputError(desc: "Failed to locally sign a transaction")
48-
}
49-
return try await self.web3.eth.send(raw: assembledTransaction)
50-
}
51-
// MARK: Writing Data flow
52-
// From CodableTransaction.data to TransactionParameters.data
53-
assembledTransaction.applyOptions(mergedOptions)
54-
// guard let transactionParameters = transaction.encodeAsDictionary(from: transactionOptions?.from) else { throw Web3Error.dataError }
55-
14+
public func send(_ transaction: CodableTransaction) async throws -> TransactionSendingResult {
5615
// MARK: Sending Data flow
5716
// FIXME: This gives empty object, fix me, there were TransactionParameters applied.
58-
let request: APIRequest = .sendTransaction(assembledTransaction)
17+
let request: APIRequest = .sendTransaction(transaction)
5918
let response: APIResponse<Hash> = try await APIRequest.sendRequest(with: self.provider, for: request)
6019

61-
let result = TransactionSendingResult(transaction: assembledTransaction, hash: response.result)
62-
for hook in self.web3.postSubmissionHooks {
63-
hook.function(result)
64-
}
20+
let result = TransactionSendingResult(transaction: transaction, hash: response.result)
6521
return result
6622
}
6723
}

Sources/web3swift/Web3/Web3+MutatingTransaction.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,25 @@ public class WriteTransaction: ReadTransaction {
180180

181181
// FIXME: Rewrite this to CodableTransaction
182182
public func send(password: String = "web3swift", transactionOptions: CodableTransaction? = nil) async throws -> TransactionSendingResult {
183-
let transaction = try await assembleTransaction(transactionOptions: transactionOptions)
183+
var assembledTransaction = try await assembleTransaction(transactionOptions: transactionOptions)
184184
let mergedOptions = self.transactionOptions.merge(transactionOptions)
185185
var cleanedOptions = CodableTransaction.emptyTransaction
186186
cleanedOptions.from = mergedOptions.from
187187
cleanedOptions.to = mergedOptions.to
188+
189+
if let attachedKeystoreManager = self.web3.provider.attachedKeystoreManager {
190+
guard let from = mergedOptions.from else {
191+
throw Web3Error.inputError(desc: "No 'from' field provided")
192+
}
193+
do {
194+
try Web3Signer.signTX(transaction: &assembledTransaction, keystore: attachedKeystoreManager, account: from, password: password)
195+
} catch {
196+
throw Web3Error.inputError(desc: "Failed to locally sign a transaction")
197+
}
198+
return try await self.web3.eth.send(raw: assembledTransaction)
199+
}
188200
// MARK: Sending Data flow
189-
return try await web3.eth.send(transaction, transactionOptions: cleanedOptions, password: password)
201+
return try await web3.eth.send(assembledTransaction)
190202
}
191203

192204
// FIXME: Rewrite this to CodableTransaction

0 commit comments

Comments
 (0)