Skip to content

Commit 64da360

Browse files
Fixes Web3Swift API methods worked with data.
1 parent dcdb273 commit 64da360

File tree

8 files changed

+44
-11
lines changed

8 files changed

+44
-11
lines changed

Sources/Core/Transaction/CodableTransaction.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import Foundation
88
import BigInt
99

1010

11-
public protocol CodableTransactionInheritable {
12-
var transactionOptions: CodableTransaction { get }
13-
}
14-
1511
/// Structure capable of carying the parameters for any transaction type.
1612
/// while all fields in this struct are optional, they are not necessarily
1713
/// optional for the type of transaction they apply to.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Core
1010

1111
extension web3.Eth {
1212
public func block(by hash: Data, fullTransactions: Bool = false) async throws -> Block {
13+
guard let hexString = String(data: hash, encoding: .utf8)?.addHexPrefix() else { throw Web3Error.dataError }
1314
let requestCall: APIRequest = .getBlockByHash(hash.toHexString().addHexPrefix(), fullTransactions)
1415
let response: APIResponse<Block> = try await APIRequest.sendRequest(with: self.provider, for: requestCall)
1516
return response.result

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import Core
99

1010
extension web3.Eth {
1111
public func transactionDetails(_ txhash: Data) async throws -> TransactionDetails {
12-
let requestCall: APIRequest = .getTransactionByHash(txhash.toHexString().addHexPrefix())
12+
guard let hexString = String(data: txhash, encoding: .utf8)?.addHexPrefix() else { throw Web3Error.dataError }
13+
let requestCall: APIRequest = .getTransactionByHash(hexString)
1314
let response: APIResponse<TransactionDetails> = try await APIRequest.sendRequest(with: self.provider, for: requestCall)
1415
return response.result
1516
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import Core
99

1010
extension web3.Eth {
1111
public func transactionReceipt(_ txhash: Data) async throws -> TransactionReceipt {
12-
let requestCall: APIRequest = .getTransactionReceipt(txhash.toHexString().addHexPrefix())
12+
guard let hexString = String(data: txhash, encoding: .utf8)?.addHexPrefix() else { throw Web3Error.dataError }
13+
let requestCall: APIRequest = .getTransactionReceipt(hexString)
1314
let response: APIResponse<TransactionReceipt> = try await APIRequest.sendRequest(with: self.provider, for: requestCall)
1415
return response.result
1516
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Core
99

1010
extension web3.Eth {
1111
public func send(raw data: Data) async throws -> TransactionSendingResult {
12-
let hexString = data.toHexString().addHexPrefix()
12+
guard let hexString = String(data: data, encoding: .utf8)?.addHexPrefix() else { throw Web3Error.dataError }
1313
let request: APIRequest = .sendRawTransaction(hexString)
1414
let response: APIResponse<Hash> = try await APIRequest.sendRequest(with: self.provider, for: request)
1515

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// web3swift
2+
//
3+
// Created by Alex Vlasov.
4+
// Copyright © 2018 Alex Vlasov. All rights reserved.
5+
//
6+
7+
import Foundation
8+
import BigInt
9+
import Core
10+
11+
12+
extension web3.Eth {
13+
14+
public func send(_ transaction: CodableTransaction, password: String) async throws -> TransactionSendingResult {
15+
if let attachedKeystoreManager = self.web3.provider.attachedKeystoreManager {
16+
var tmpTransaction = transaction
17+
do {
18+
try Web3Signer.signTX(transaction: &tmpTransaction,
19+
keystore: attachedKeystoreManager,
20+
account: tmpTransaction.from ?? tmpTransaction.sender ?? EthereumAddress.contractDeploymentAddress(),
21+
password: password)
22+
} catch {
23+
throw Web3Error.inputError(desc: "Failed to locally sign a transaction")
24+
}
25+
return try await self.web3.eth.send(raw: tmpTransaction.encode(for: .transaction)!)
26+
}
27+
28+
// MARK: Sending Data flow
29+
let request: APIRequest = .sendTransaction(transaction)
30+
let response: APIResponse<Hash> = try await APIRequest.sendRequest(with: self.provider, for: request)
31+
32+
let result = TransactionSendingResult(transaction: transaction, hash: response.result)
33+
return result
34+
}
35+
}

Sources/web3swift/Operations/WriteOperation.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public class WriteOperation: ReadOperation {
2626
return try await web3.eth.send(raw: transactionData)
2727
}
2828
// MARK: Sending Data flow
29-
guard let transactionData = transaction.encode(for: .transaction) else { throw Web3Error.dataError }
30-
return try await web3.eth.send(raw: transactionData)
29+
return try await web3.eth.send(transaction, password: password)
3130
}
3231

3332
// FIXME: Rewrite this to CodableTransaction

Tests/web3swiftTests/localTests/BasicLocalNodeTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class BasicLocalNodeTests: LocalTestCase {
3333

3434
Thread.sleep(forTimeInterval: 1.0)
3535

36-
let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!)
36+
let receipt = try await web3.eth.transactionReceipt(txHash.stripHexPrefix().data(using: .utf8)!)
3737
print(receipt)
3838

3939
switch receipt.status {
@@ -43,7 +43,7 @@ class BasicLocalNodeTests: LocalTestCase {
4343
break
4444
}
4545

46-
let details = try await web3.eth.transactionDetails(txHash.data(using: .utf8)!)
46+
let details = try await web3.eth.transactionDetails(txHash.stripHexPrefix().data(using: .utf8)!)
4747
print(details)
4848
}
4949

0 commit comments

Comments
 (0)