diff --git a/Sources/Core/Contract/ContractProtocol.swift b/Sources/Core/Contract/ContractProtocol.swift index 26da2a06a..bcf7d8aa8 100755 --- a/Sources/Core/Contract/ContractProtocol.swift +++ b/Sources/Core/Contract/ContractProtocol.swift @@ -225,7 +225,6 @@ extension DefaultContractProtocol { let parameters = parameters, !parameters.isEmpty { guard constructor.inputs.count == parameters.count, - // FIXME: This should be zipped, because Arrays don't guarantee it's elements order let encodedData = constructor.encodeParameters(parameters) else { NSLog("Constructor encoding will fail as the number of input arguments doesn't match the number of given arguments.") return nil diff --git a/Sources/Core/EthereumABI/ABIEncoding.swift b/Sources/Core/EthereumABI/ABIEncoding.swift index 35cc241c7..a995365d8 100755 --- a/Sources/Core/EthereumABI/ABIEncoding.swift +++ b/Sources/Core/EthereumABI/ABIEncoding.swift @@ -137,7 +137,6 @@ extension ABIEncoder { /// - values: Contract values of a given element to encode /// - Returns: Encoded data public static func encode(types: [ABI.Element.InOut], values: [AnyObject]) -> Data? { - // FIXME: This should be zipped, because Arrays don't guarantee it's elements order guard types.count == values.count else {return nil} let params = types.compactMap { (el) -> ABI.Element.ParameterType in return el.type @@ -152,7 +151,6 @@ extension ABIEncoder { /// - values: Contract values of a given element to encode /// - Returns: Encoded data public static func encode(types: [ABI.Element.ParameterType], values: [AnyObject]) -> Data? { - // FIXME: This should be zipped, because Arrays don't guarantee it's elements order guard types.count == values.count else {return nil} var tails = [Data]() var heads = [Data]() diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+Call.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+Call.swift index d058d4dfc..e6b7b5d86 100755 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+Call.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+Call.swift @@ -9,7 +9,7 @@ import Core extension Web3.Eth { public func callTransaction(_ transaction: CodableTransaction) async throws -> Data { - let request: APIRequest = .call(transaction, transaction.callOnBlock ?? .latest) - return try await APIRequest.sendRequest(with: self.provider, for: request).result + let request = APIRequest.call(transaction, transaction.callOnBlock ?? .latest) + return try await APIRequest.sendRequest(with: provider, for: request).result } } diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+EstimateGas.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+EstimateGas.swift index fd2e763d2..4d8388baa 100755 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+EstimateGas.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+EstimateGas.swift @@ -10,6 +10,7 @@ import Core extension Web3.Eth { public func estimateGas(for transaction: CodableTransaction, onBlock: BlockNumber = .latest) async throws -> BigUInt { - try await APIRequest.sendRequest(with: provider, for: .estimateGas(transaction, onBlock)).result + let request = APIRequest.estimateGas(transaction, onBlock) + return try await APIRequest.sendRequest(with: provider, for: request).result } } diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+FeeHistory.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+FeeHistory.swift index 0375a99d4..434a6509a 100644 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+FeeHistory.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+FeeHistory.swift @@ -9,7 +9,7 @@ import Core extension Web3.Eth { func feeHistory(blockCount: BigUInt, block: BlockNumber, percentiles:[Double]) async throws -> Oracle.FeeHistory { - let requestCall: APIRequest = .feeHistory(blockCount, block, percentiles) - return try await APIRequest.sendRequest(with: web3.provider, for: requestCall).result + let request = APIRequest.feeHistory(blockCount, block, percentiles) + return try await APIRequest.sendRequest(with: web3.provider, for: request).result } } diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetAccounts.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetAccounts.swift index 8ed5c2d98..f433fabe2 100755 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetAccounts.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetAccounts.swift @@ -9,8 +9,8 @@ import Core extension Web3.Eth { public func ownedAccounts() async throws -> [EthereumAddress] { - guard self.web3.provider.attachedKeystoreManager == nil else { - return try self.web3.wallet.getAccounts() + guard web3.provider.attachedKeystoreManager == nil else { + return try web3.wallet.getAccounts() } return try await APIRequest.sendRequest(with: web3.provider, for: .getAccounts).result } diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetBalance.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetBalance.swift index 5a2755207..fb189e2fa 100755 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetBalance.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetBalance.swift @@ -9,7 +9,7 @@ import BigInt extension Web3.Eth { public func getBalance(for address: EthereumAddress, onBlock: BlockNumber = .latest) async throws -> BigUInt { - let requestCall: APIRequest = .getBalance(address.address, onBlock) - return try await APIRequest.sendRequest(with: web3.provider, for: requestCall).result + let request = APIRequest.getBalance(address.address, onBlock) + return try await APIRequest.sendRequest(with: web3.provider, for: request).result } } diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetBlockByHash.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetBlockByHash.swift index ac1453ef7..d8103091a 100755 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetBlockByHash.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetBlockByHash.swift @@ -10,8 +10,7 @@ import Core extension Web3.Eth { public func block(by hash: Data, fullTransactions: Bool = false) async throws -> Block { - guard let hexString = String(data: hash, encoding: .utf8)?.addHexPrefix() else { throw Web3Error.dataError } - let requestCall: APIRequest = .getBlockByHash(hash.toHexString().addHexPrefix(), fullTransactions) - return try await APIRequest.sendRequest(with: self.provider, for: requestCall).result + let request = APIRequest.getBlockByHash(hash.toHexString().addHexPrefix(), fullTransactions) + return try await APIRequest.sendRequest(with: provider, for: request).result } } diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetBlockByNumber.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetBlockByNumber.swift index c6e266cf7..2082a3fc5 100755 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetBlockByNumber.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetBlockByNumber.swift @@ -10,12 +10,12 @@ import Core extension Web3.Eth { public func block(by hash: Hash, fullTransactions: Bool = false) async throws -> Block { - let requestCall: APIRequest = .getBlockByHash(hash, fullTransactions) - return try await APIRequest.sendRequest(with: self.provider, for: requestCall).result + let request = APIRequest.getBlockByHash(hash, fullTransactions) + return try await APIRequest.sendRequest(with: provider, for: request).result } public func block(by number: BlockNumber, fullTransactions: Bool = false) async throws -> Block { - let requestCall: APIRequest = .getBlockByNumber(number, fullTransactions) - return try await APIRequest.sendRequest(with: self.provider, for: requestCall).result + let request = APIRequest.getBlockByNumber(number, fullTransactions) + return try await APIRequest.sendRequest(with: provider, for: request).result } } diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetCode.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetCode.swift index 4872c1cb6..5e7c03e84 100644 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetCode.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetCode.swift @@ -9,6 +9,7 @@ import BigInt extension Web3.Eth { public func code(for address: EthereumAddress, onBlock: BlockNumber = .latest) async throws -> Hash { - try await APIRequest.sendRequest(with: self.provider, for: .getCode(address.address, onBlock)).result + let request = APIRequest.getCode(address.address, onBlock) + return try await APIRequest.sendRequest(with: provider, for: request).result } } diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetTransactionCount.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetTransactionCount.swift index 4ca0df400..91187a010 100755 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetTransactionCount.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetTransactionCount.swift @@ -10,7 +10,7 @@ import Core extension Web3.Eth { public func getTransactionCount(for address: EthereumAddress, onBlock: BlockNumber = .latest) async throws -> BigUInt { - let requestCall: APIRequest = .getTransactionCount(address.address, onBlock) - return try await APIRequest.sendRequest(with: self.provider, for: requestCall).result + let request = APIRequest.getTransactionCount(address.address, onBlock) + return try await APIRequest.sendRequest(with: provider, for: request).result } } diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetTransactionDetails.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetTransactionDetails.swift index 344f1f963..34a30a98c 100755 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetTransactionDetails.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetTransactionDetails.swift @@ -8,9 +8,8 @@ import BigInt import Core extension Web3.Eth { - public func transactionDetails(_ txhash: Data) async throws -> TransactionDetails { - guard let hexString = String(data: txhash, encoding: .utf8)?.addHexPrefix() else { throw Web3Error.dataError } - let requestCall: APIRequest = .getTransactionByHash(hexString) - return try await APIRequest.sendRequest(with: self.provider, for: requestCall).result + public func transactionDetails(_ txHash: Data) async throws -> TransactionDetails { + let request = APIRequest.getTransactionByHash(txHash.toHexString().addHexPrefix()) + return try await APIRequest.sendRequest(with: provider, for: request).result } } diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetTransactionReceipt.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetTransactionReceipt.swift index 23ba7b9d9..a960c5791 100755 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetTransactionReceipt.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+GetTransactionReceipt.swift @@ -8,9 +8,8 @@ import BigInt import Core extension Web3.Eth { - public func transactionReceipt(_ txhash: Data) async throws -> TransactionReceipt { - guard let hexString = String(data: txhash, encoding: .utf8)?.addHexPrefix() else { throw Web3Error.dataError } - let requestCall: APIRequest = .getTransactionReceipt(hexString) - return try await APIRequest.sendRequest(with: self.provider, for: requestCall).result + public func transactionReceipt(_ txHash: Data) async throws -> TransactionReceipt { + let request = APIRequest.getTransactionReceipt(txHash.toHexString().addHexPrefix()) + return try await APIRequest.sendRequest(with: provider, for: request).result } } diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+SendRawTransaction.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+SendRawTransaction.swift index 250d4be9e..fb1cc1f4b 100755 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+SendRawTransaction.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+SendRawTransaction.swift @@ -9,9 +9,8 @@ import Core extension Web3.Eth { public func send(raw data: Data) async throws -> TransactionSendingResult { - guard let hexString = String(data: data, encoding: .utf8)?.addHexPrefix() else { throw Web3Error.dataError } - let request: APIRequest = .sendRawTransaction(hexString) - let response: APIResponse = try await APIRequest.sendRequest(with: self.provider, for: request) + let request = APIRequest.sendRawTransaction(data.toHexString().addHexPrefix()) + let response: APIResponse = try await APIRequest.sendRequest(with: provider, for: request) return try TransactionSendingResult(data: data, hash: response.result) } } @@ -21,7 +20,7 @@ public struct TransactionSendingResult { public var hash: String } -extension TransactionSendingResult { +fileprivate extension TransactionSendingResult { init(data: Data, hash: Hash) throws { guard let transaction = CodableTransaction(rawValue: data) else { throw Web3Error.dataError } self.transaction = transaction diff --git a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+SendTransaction.swift b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+SendTransaction.swift index 0d301e6b8..82e403c41 100644 --- a/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+SendTransaction.swift +++ b/Sources/web3swift/EthereumAPICalls/Ethereum/Eth+SendTransaction.swift @@ -11,8 +11,8 @@ import Core extension Web3.Eth { public func send(_ transaction: CodableTransaction) async throws -> TransactionSendingResult { - let request: APIRequest = .sendTransaction(transaction) - let response: APIResponse = try await APIRequest.sendRequest(with: self.provider, for: request) + let request = APIRequest.sendTransaction(transaction) + let response: APIResponse = try await APIRequest.sendRequest(with: provider, for: request) return TransactionSendingResult(transaction: transaction, hash: response.result) } } diff --git a/Sources/web3swift/Web3/Web3+Contract.swift b/Sources/web3swift/Web3/Web3+Contract.swift index dc2d135f0..abefbea29 100755 --- a/Sources/web3swift/Web3/Web3+Contract.swift +++ b/Sources/web3swift/Web3/Web3+Contract.swift @@ -105,7 +105,7 @@ extension Web3 { /// /// Returns a "Transaction intermediate" object. public func createWriteOperation(_ method: String = "fallback", parameters: [AnyObject] = [AnyObject](), extraData: Data = Data()) -> WriteOperation? { - guard var data = self.contract.method(method, parameters: parameters, extraData: extraData) else {return nil} + guard let data = self.contract.method(method, parameters: parameters, extraData: extraData) else {return nil} transaction.data = data if let network = self.web3.provider.network { transaction.chainID = network.chainID diff --git a/Tests/web3swiftTests/localTests/AdvancedABIv2Tests.swift b/Tests/web3swiftTests/localTests/AdvancedABIv2Tests.swift index d5776fa33..873d90038 100755 --- a/Tests/web3swiftTests/localTests/AdvancedABIv2Tests.swift +++ b/Tests/web3swiftTests/localTests/AdvancedABIv2Tests.swift @@ -26,11 +26,11 @@ class AdvancedABIv2Tests: LocalTestCase { deployTx.transaction.gasLimitPolicy = .manual(3000000) // MARK: Sending Data flow let result = try await deployTx.writeToChain(password: "web3swift") - let txHash = result.hash + let txHash = result.hash.stripHexPrefix() Thread.sleep(forTimeInterval: 1.0) - let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!) + let receipt = try await web3.eth.transactionReceipt(Data.fromHex(txHash)!) print(receipt) switch receipt.status { @@ -62,11 +62,11 @@ class AdvancedABIv2Tests: LocalTestCase { deployTx.transaction.from = allAddresses[0] deployTx.transaction.gasLimitPolicy = .manual(3000000) let result = try await deployTx.writeToChain(password: "web3swift") - let txHash = result.hash + let txHash = result.hash.stripHexPrefix() Thread.sleep(forTimeInterval: 1.0) - let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!) + let receipt = try await web3.eth.transactionReceipt(Data.fromHex(txHash)!) print(receipt) switch receipt.status { @@ -98,11 +98,11 @@ class AdvancedABIv2Tests: LocalTestCase { deployTx.transaction.from = allAddresses[0] deployTx.transaction.gasLimitPolicy = .manual(3000000) let result = try await deployTx.writeToChain(password: "web3swift") - let txHash = result.hash + let txHash = result.hash.stripHexPrefix() Thread.sleep(forTimeInterval: 1.0) - let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!) + let receipt = try await web3.eth.transactionReceipt(Data.fromHex(txHash)!) print(receipt) switch receipt.status { @@ -133,11 +133,11 @@ class AdvancedABIv2Tests: LocalTestCase { deployTx.transaction.from = allAddresses[0] deployTx.transaction.gasLimitPolicy = .manual(3000000) let result = try await deployTx.writeToChain(password: "web3swift") - let txHash = result.hash + let txHash = result.hash.stripHexPrefix() Thread.sleep(forTimeInterval: 1.0) - let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!) + let receipt = try await web3.eth.transactionReceipt(Data.fromHex(txHash)!) print(receipt) switch receipt.status { @@ -169,11 +169,11 @@ class AdvancedABIv2Tests: LocalTestCase { deployTx.transaction.from = allAddresses[0] deployTx.transaction.gasLimitPolicy = .manual(3000000) let result = try await deployTx.writeToChain(password: "web3swift") - let txHash = result.hash + let txHash = result.hash.stripHexPrefix() Thread.sleep(forTimeInterval: 1.0) - let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!) + let receipt = try await web3.eth.transactionReceipt(Data.fromHex(txHash)!) print(receipt) switch receipt.status { diff --git a/Tests/web3swiftTests/localTests/BasicLocalNodeTests.swift b/Tests/web3swiftTests/localTests/BasicLocalNodeTests.swift index b38036365..3445601ba 100755 --- a/Tests/web3swiftTests/localTests/BasicLocalNodeTests.swift +++ b/Tests/web3swiftTests/localTests/BasicLocalNodeTests.swift @@ -22,28 +22,26 @@ class BasicLocalNodeTests: LocalTestCase { let contract = web3.contract(abiString, at: nil, abiVersion: 2)! let parameters = [] as [AnyObject] - // MARK: Writing Data flow + let deployTx = contract.prepareDeploy(bytecode: bytecode, parameters: parameters)! deployTx.transaction.from = allAddresses[0] deployTx.transaction.gasLimitPolicy = .manual(3000000) let result = try await deployTx.writeToChain(password: "web3swift") - let txHash = result.hash - - Thread.sleep(forTimeInterval: 1.0) - - let receipt = try await web3.eth.transactionReceipt(txHash.stripHexPrefix().data(using: .utf8)!) - print(receipt) - - switch receipt.status { - case .notYetProcessed: - return - default: - break + let txHash = result.hash.stripHexPrefix() + + while true { + let receipt = try await web3.eth.transactionReceipt(Data.fromHex(txHash)!) + switch receipt.status { + case .notYetProcessed: + continue + case .failed: + XCTFail("Failed to deploy a contract!") + case .ok: + XCTAssertNotNil(receipt.contractAddress) + return + } } - - let details = try await web3.eth.transactionDetails(txHash.stripHexPrefix().data(using: .utf8)!) - print(details) } func testEthSendExampleWithRemoteSigning() async throws { @@ -65,11 +63,11 @@ class BasicLocalNodeTests: LocalTestCase { print("Balance before from: " + balanceBeforeFrom.description) let result = try await sendTx.writeToChain(password: "web3swift") - let txHash = result.hash + let txHash = Data.fromHex(result.hash.stripHexPrefix())! Thread.sleep(forTimeInterval: 1.0) - let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!) + let receipt = try await web3.eth.transactionReceipt(txHash) print(receipt) switch receipt.status { @@ -79,7 +77,7 @@ class BasicLocalNodeTests: LocalTestCase { break } - let details = try await web3.eth.transactionDetails(txHash.data(using: .utf8)!) + let details = try await web3.eth.transactionDetails(txHash) print(details) diff --git a/Tests/web3swiftTests/localTests/LocalTestCase.swift b/Tests/web3swiftTests/localTests/LocalTestCase.swift index 38b9198e1..2714e6c8d 100644 --- a/Tests/web3swiftTests/localTests/LocalTestCase.swift +++ b/Tests/web3swiftTests/localTests/LocalTestCase.swift @@ -10,6 +10,7 @@ import web3swift class LocalTestCase: XCTestCase { static let url = URL(string: "http://127.0.0.1:8545")! + static let keyStoreManager: KeystoreManager = KeystoreManager([try! EthereumKeystoreV3(password: "web3swift")!]) override func setUp() async throws { let web3 = try! await Web3.new(LocalTestCase.url) diff --git a/Tests/web3swiftTests/localTests/PersonalSignatureTests.swift b/Tests/web3swiftTests/localTests/PersonalSignatureTests.swift index b3c9b821e..ca1114e41 100755 --- a/Tests/web3swiftTests/localTests/PersonalSignatureTests.swift +++ b/Tests/web3swiftTests/localTests/PersonalSignatureTests.swift @@ -43,11 +43,11 @@ class PersonalSignatureTests: XCTestCase { deployTx.transaction.from = allAddresses[0] deployTx.transaction.gasLimitPolicy = .manual(3000000) let deployResult = try await deployTx.writeToChain(password: "web3swift") - let txHash = deployResult.hash + let txHash = Data.fromHex(deployResult.hash.stripHexPrefix())! Thread.sleep(forTimeInterval: 1.0) - let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!) + let receipt = try await web3.eth.transactionReceipt(txHash) print(receipt) switch receipt.status { @@ -76,13 +76,13 @@ class PersonalSignatureTests: XCTestCase { var tx = contract.createReadOperation("hashPersonalMessage", parameters: [message as AnyObject]) tx?.transaction.from = expectedAddress var result = try await tx!.callContractMethod() - guard let hash = result["hash"]! as? Data else {return XCTFail()} + guard let hash = result["hash"]! as? Data else { return XCTFail() } XCTAssert(Utilities.hashPersonalMessage(message.data(using: .utf8)!)! == hash) tx = contract.createReadOperation("recoverSigner", parameters: [message, unmarshalledSignature.v, Data(unmarshalledSignature.r), Data(unmarshalledSignature.s)] as [AnyObject]) tx?.transaction.from = expectedAddress result = try await tx!.callContractMethod() - guard let signer = result["signer"]! as? EthereumAddress else {return XCTFail()} + guard let signer = result["signer"]! as? EthereumAddress else { return XCTFail() } XCTAssert(signer == expectedAddress) } diff --git a/Tests/web3swiftTests/localTests/TestHelpers.swift b/Tests/web3swiftTests/localTests/TestHelpers.swift index 5c8d5cf81..d1381f2b3 100644 --- a/Tests/web3swiftTests/localTests/TestHelpers.swift +++ b/Tests/web3swiftTests/localTests/TestHelpers.swift @@ -34,11 +34,11 @@ class TestHelpers { deployTx.transaction.from = allAddresses[0] deployTx.transaction.gasLimitPolicy = .manual(3000000) let result = try await deployTx.writeToChain(password: "web3swift") - let txHash = result.hash + let txHash = Data.fromHex(result.hash.stripHexPrefix())! Thread.sleep(forTimeInterval: 1.0) - let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!) + let receipt = try await web3.eth.transactionReceipt(txHash) print(receipt) switch receipt.status { diff --git a/Tests/web3swiftTests/localTests/TransactionsTests.swift b/Tests/web3swiftTests/localTests/TransactionsTests.swift index d54fcd2ec..a5c3e82e0 100755 --- a/Tests/web3swiftTests/localTests/TransactionsTests.swift +++ b/Tests/web3swiftTests/localTests/TransactionsTests.swift @@ -639,12 +639,12 @@ class TransactionsTests: XCTestCase { writeTX.transaction.value = value! writeTX.transaction.gasLimitPolicy = .manual(78423) let result = try await writeTX.writeToChain(password: "") - let txHash = result.hash - print("Transaction with hash " + txHash) + let txHash = Data.fromHex(result.hash.stripHexPrefix())! + print("Transaction with hash " + result.hash) Thread.sleep(forTimeInterval: 1.0) - let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!) + let receipt = try await web3.eth.transactionReceipt(txHash) print(receipt) XCTAssert(receipt.status == .ok) @@ -655,7 +655,7 @@ class TransactionsTests: XCTestCase { break } - let details = try await web3.eth.transactionDetails(txHash.data(using: .utf8)!) + let details = try await web3.eth.transactionDetails(txHash) print(details) // FIXME: Reenable this test. // XCTAssertEqual(details.transaction.gasLimit, BigUInt(78423)) diff --git a/Tests/web3swiftTests/localTests/UserCases.swift b/Tests/web3swiftTests/localTests/UserCases.swift index 77c905260..814b6178b 100755 --- a/Tests/web3swiftTests/localTests/UserCases.swift +++ b/Tests/web3swiftTests/localTests/UserCases.swift @@ -81,11 +81,11 @@ class UserCases: XCTestCase { deployTx.transaction.from = allAddresses[0] deployTx.transaction.gasLimitPolicy = .manual(3000000) let result = try await deployTx.writeToChain(password: "web3swift") - let txHash = result.hash + let txHash = Data.fromHex(result.hash.stripHexPrefix())! Thread.sleep(forTimeInterval: 1.0) - let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!) + let receipt = try await web3.eth.transactionReceipt(txHash) print(receipt) XCTAssert(receipt.contractAddress != nil) @@ -96,7 +96,7 @@ class UserCases: XCTestCase { break } - let details = try await web3.eth.transactionDetails(txHash.data(using: .utf8)!) + let details = try await web3.eth.transactionDetails(txHash) print(details) XCTAssert(details.transaction.to == .contractDeploymentAddress()) } diff --git a/Tests/web3swiftTests/remoteTests/InfuraTests.swift b/Tests/web3swiftTests/remoteTests/InfuraTests.swift index 4f1d6dd61..68570091c 100755 --- a/Tests/web3swiftTests/remoteTests/InfuraTests.swift +++ b/Tests/web3swiftTests/remoteTests/InfuraTests.swift @@ -12,20 +12,24 @@ import Core class InfuraTests: XCTestCase { func testGetBalance() async throws { - do { - let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken) - let address = EthereumAddress("0xd61b5ca425F8C8775882d4defefC68A6979DBbce")! - let balance = try await web3.eth.getBalance(for: address) - let balString = Utilities.formatToPrecision(balance, numberDecimals: Utilities.Units.eth.decimals, formattingDecimals: 3) - XCTAssertNotNil(balString) - } catch { - XCTFail() - } + let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken) + let address = EthereumAddress("0xd61b5ca425F8C8775882d4defefC68A6979DBbce")! + let balance = try await web3.eth.getBalance(for: address) + let balString = Utilities.formatToPrecision(balance, numberDecimals: Utilities.Units.eth.decimals, formattingDecimals: 3) + XCTAssertNotNil(balString) } func testGetBlockByHash() async throws { let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken) let result = try await web3.eth.block(by: "0x6d05ba24da6b7a1af22dc6cc2a1fe42f58b2a5ea4c406b19c8cf672ed8ec0695", fullTransactions: false) + XCTAssertEqual(result.number, 5184323) + } + + func testGetBlockByHash_hashAsData() async throws { + let blockHash = Data.fromHex("6d05ba24da6b7a1af22dc6cc2a1fe42f58b2a5ea4c406b19c8cf672ed8ec0695")! + let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken) + let result = try await web3.eth.block(by: blockHash, fullTransactions: false) + XCTAssertEqual(result.number, 5184323) } func testGetBlockByNumber1() async throws { @@ -44,8 +48,7 @@ class InfuraTests: XCTestCase { let _ = try await web3.eth.block(by: .exact(10000000000000), fullTransactions: true) XCTFail("The expression above must throw DecodingError.") } catch { - // DecodingError is throws as a block for the given block number is - // + // DecodingError is thrown as a block for the given block number does not exist XCTAssert(error is DecodingError) } } diff --git a/Tests/web3swiftTests/remoteTests/ST20AndSecurityTokenTests.swift b/Tests/web3swiftTests/remoteTests/ST20AndSecurityTokenTests.swift index a590b5e93..418c408ab 100644 --- a/Tests/web3swiftTests/remoteTests/ST20AndSecurityTokenTests.swift +++ b/Tests/web3swiftTests/remoteTests/ST20AndSecurityTokenTests.swift @@ -14,12 +14,12 @@ import Core // MARK: Works only with network connection class ST20AndSecurityTokenTests: XCTestCase { - + // FIXME: Enable me back again // Test fails because there's no such wallet on goerli chain as well as token. // func testERC20TokenCreation() async throws { // let web3 = await Web3.InfuraGoerliWeb3(accessToken: Constants.infuraToken) -// let w3sTokenAddress = EthereumAddress("0x2dD33957C90880bE4Ee9fd5F703110BDA2E579EC")! +// let w3sTokenAddress = EthereumAddress("0x33d191db2486e0d245b44fde3fae5ed667d5694b")! // let st20token = ST20.init(web3: web3, provider: web3.provider, address: w3sTokenAddress) // try await st20token.readProperties() // XCTAssertEqual(st20token.symbol(), "MIMI")