Skip to content

Commit

Permalink
Merge pull request #632 from JeneaVranceanu/hot-fix/data-to-hex-string
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslavyaroslav authored Nov 3, 2022
2 parents 53a216e + b2298b3 commit 4c57752
Show file tree
Hide file tree
Showing 25 changed files with 89 additions and 92 deletions.
1 change: 0 additions & 1 deletion Sources/Core/Contract/ContractProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions Sources/Core/EthereumABI/ABIEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]()
Expand Down
4 changes: 2 additions & 2 deletions Sources/web3swift/EthereumAPICalls/Ethereum/Eth+Call.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hash> = try await APIRequest.sendRequest(with: self.provider, for: request)
let request = APIRequest.sendRawTransaction(data.toHexString().addHexPrefix())
let response: APIResponse<Hash> = try await APIRequest.sendRequest(with: provider, for: request)
return try TransactionSendingResult(data: data, hash: response.result)
}
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hash> = try await APIRequest.sendRequest(with: self.provider, for: request)
let request = APIRequest.sendTransaction(transaction)
let response: APIResponse<Hash> = try await APIRequest.sendRequest(with: provider, for: request)
return TransactionSendingResult(transaction: transaction, hash: response.result)
}
}
2 changes: 1 addition & 1 deletion Sources/web3swift/Web3/Web3+Contract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions Tests/web3swiftTests/localTests/AdvancedABIv2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 17 additions & 19 deletions Tests/web3swiftTests/localTests/BasicLocalNodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)


Expand Down
1 change: 1 addition & 0 deletions Tests/web3swiftTests/localTests/LocalTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions Tests/web3swiftTests/localTests/PersonalSignatureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/web3swiftTests/localTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 4c57752

Please sign in to comment.