Skip to content

Commit b795104

Browse files
fix: tests fixed
1 parent 48590e5 commit b795104

File tree

10 files changed

+245
-293
lines changed

10 files changed

+245
-293
lines changed

Tests/web3swiftTests/localTests/ABIEncoderTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import XCTest
1111
import Core
1212
@testable import web3swift
1313

14-
class ABIEncoderTest: LocalTestCase {
14+
class ABIEncoderTest: XCTestCase {
1515

1616
func test_soliditySha3() throws {
1717
var hex = try ABIEncoder.soliditySha3(true).toHexString().addHexPrefix()

Tests/web3swiftTests/localTests/AdvancedABIv2Tests.swift

Lines changed: 43 additions & 34 deletions
Large diffs are not rendered by default.

Tests/web3swiftTests/localTests/BasicLocalNodeTests.swift

Lines changed: 27 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,37 @@
33
// Created by Alex Vlasov.
44
// Copyright © 2018 Alex Vlasov. All rights reserved.
55
//
6-
6+
// TODO: Replace `XCTAssert` with more explicite `XCTAssertEqual`, where Applicable
77
import XCTest
88
import CryptoSwift
99
import BigInt
10+
import Core
1011

1112
@testable import web3swift
1213

13-
class BasicLocalNodeTests: LocalTestCase {
14+
class BasicLocalNodeTests: XCTestCase {
1415

15-
func testDeployWithRemoteSigning() throws {
16-
let allAddresses = try ganache.eth.getAccounts()
16+
func testDeployWithRemoteSigning() async throws {
17+
let web3 = try await Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
18+
let allAddresses = try await web3.eth.ownedAccounts()
1719

1820
let abiString = "[{\"constant\":true,\"inputs\":[],\"name\":\"getFlagData\",\"outputs\":[{\"name\":\"data\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"data\",\"type\":\"string\"}],\"name\":\"setFlagData\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"
1921
let bytecode = Data.fromHex("6060604052341561000f57600080fd5b6103358061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063a16e94bf14610051578063a46b5b6b146100df575b600080fd5b341561005c57600080fd5b61006461013c565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a4578082015181840152602081019050610089565b50505050905090810190601f1680156100d15780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156100ea57600080fd5b61013a600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190505061020d565b005b610144610250565b6000808073ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102035780601f106101d857610100808354040283529160200191610203565b820191906000526020600020905b8154815290600101906020018083116101e657829003601f168201915b5050505050905090565b806000808073ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600001908051906020019061024c929190610264565b5050565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102a557805160ff19168380011785556102d3565b828001600101855582156102d3579182015b828111156102d25782518255916020019190600101906102b7565b5b5090506102e091906102e4565b5090565b61030691905b808211156103025760008160009055506001016102ea565b5090565b905600a165627a7a7230582017359d063cd7fdf56f19ca186a54863ce855c8f070acece905d8538fbbc4d1bf0029")!
2022

21-
let contract = ganache.contract(abiString, at: nil, abiVersion: 2)!
22-
let deployTx = contract.deploy(bytecode: bytecode)!
23+
let contract = web3.contract(abiString, at: nil, abiVersion: 2)!
24+
25+
let parameters = [] as [AnyObject]
26+
let deployTx = contract.deploy(bytecode: bytecode, parameters: parameters)!
2327
deployTx.transactionOptions.from = allAddresses[0]
2428
deployTx.transactionOptions.gasLimit = .manual(3000000)
2529

26-
let result = try deployTx.sendPromise().wait()
30+
let result = try await deployTx.send()
2731
let txHash = result.hash
2832
print("Transaction with hash " + txHash)
2933

3034
Thread.sleep(forTimeInterval: 1.0)
3135

32-
let receipt = try ganache.eth.getTransactionReceipt(txHash)
36+
let receipt = try await web3.eth.transactionReceipt(txHash)
3337
print(receipt)
3438

3539
switch receipt.status {
@@ -39,34 +43,35 @@ class BasicLocalNodeTests: LocalTestCase {
3943
break
4044
}
4145

42-
let details = try ganache.eth.getTransactionDetails(txHash)
46+
let details = try await web3.eth.transactionDetails(txHash)
4347
print(details)
4448
}
4549

46-
func testEthSendExampleWithRemoteSigning() throws {
47-
let allAddresses = try ganache.eth.getAccounts()
50+
func testEthSendExampleWithRemoteSigning() async throws {
51+
let web3 = try await Web3.new(URL(string: "http://127.0.0.1:8545")!)
52+
let allAddresses = try await web3.eth.ownedAccounts()
4853
let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
49-
let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
54+
let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
5055

5156
let parameters = [] as [AnyObject]
5257
let sendTx = contract.method("fallback", parameters: parameters)!
5358

54-
let valueToSend = Web3.Utils.parseToBigUInt("1.0", units: .eth)!
59+
let valueToSend = Utilities.parseToBigUInt("1.0", units: .eth)
5560
sendTx.transactionOptions.value = valueToSend
5661
sendTx.transactionOptions.from = allAddresses[0]
5762

58-
let balanceBeforeTo = try ganache.eth.getBalancePromise(address: sendToAddress).wait()
59-
let balanceBeforeFrom = try ganache.eth.getBalancePromise(address: allAddresses[0]).wait()
63+
let balanceBeforeTo = try await web3.eth.getBalance(for: sendToAddress)
64+
let balanceBeforeFrom = try await web3.eth.getBalance(for: allAddresses[0])
6065
print("Balance before to: " + balanceBeforeTo.description)
6166
print("Balance before from: " + balanceBeforeFrom.description)
6267

63-
let result = try sendTx.sendPromise().wait()
68+
let result = try await sendTx.send()
6469
let txHash = result.hash
6570
print("Transaction with hash " + txHash)
6671

6772
Thread.sleep(forTimeInterval: 1.0)
6873

69-
let receipt = try ganache.eth.getTransactionReceipt(txHash)
74+
let receipt = try await web3.eth.transactionReceipt(txHash)
7075
print(receipt)
7176

7277
switch receipt.status {
@@ -76,88 +81,17 @@ class BasicLocalNodeTests: LocalTestCase {
7681
break
7782
}
7883

79-
let details = try ganache.eth.getTransactionDetails(txHash)
84+
let details = try await web3.eth.transactionDetails(txHash)
8085
print(details)
8186

8287

83-
let balanceAfterTo = try ganache.eth.getBalancePromise(address: sendToAddress).wait()
84-
let balanceAfterFrom = try ganache.eth.getBalancePromise(address: allAddresses[0]).wait()
88+
let balanceAfterTo = try await web3.eth.getBalance(for: sendToAddress)
89+
let balanceAfterFrom = try await web3.eth.getBalance(for: allAddresses[0])
8590
print("Balance after to: " + balanceAfterTo.description)
8691
print("Balance after from: " + balanceAfterFrom.description)
8792

88-
XCTAssertEqual(balanceAfterTo - balanceBeforeTo, valueToSend)
93+
XCTAssert(balanceAfterTo - balanceBeforeTo == valueToSend)
8994
let txnGasPrice = details.transaction.parameters.gasPrice ?? 0
90-
XCTAssertEqual(balanceBeforeFrom - (balanceAfterFrom + receipt.gasUsed * txnGasPrice), valueToSend)
95+
XCTAssert(balanceBeforeFrom - (balanceAfterFrom + receipt.gasUsed * txnGasPrice) == valueToSend)
9196
}
92-
93-
// FIXME: Crashes on CI/CD
94-
// FIXME: Fails on ganache
95-
// func testSignPersonal() throws {
96-
// let allAddresses = try ganache.eth.getAccounts()
97-
98-
// let response = try ganache.personal.signPersonalMessage(message: "hello world".data(using: .utf8)!, from: allAddresses[0])
99-
100-
// XCTAssert(response.toHexString() == "b686c8ddc854bd49de9eb62eb4e52af4c69a89802b40fe9a295e346b111406393c6e3f05114561ab845a47196ad22c33cec67592af9a9e42bfc067a20c7d4b6101")
101-
// }
102-
103-
// MARK: Ganache doesn't support a mempool for now
104-
// func testTxPoolStatus() throws {
105-
// let allAddresses = try ganache.eth.getAccounts()
106-
//
107-
// let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
108-
// let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
109-
//
110-
// let parameters = [] as [AnyObject]
111-
// let sendTx = contract.method("fallback", parameters: parameters)!
112-
//
113-
// let valueToSend = Web3.Utils.parseToBigUInt("1.0", units: .eth)
114-
// sendTx.transactionOptions.value = valueToSend
115-
// sendTx.transactionOptions.from = allAddresses[0]
116-
//
117-
// let _ = try sendTx.sendPromise().wait()
118-
//
119-
// let result = try ganache.txPool.getStatus()
120-
//
121-
// print(result)
122-
// }
123-
//
124-
// func testTxPoolInspect() throws {
125-
// let allAddresses = try ganache.eth.getAccounts()
126-
//
127-
// let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
128-
// let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
129-
//
130-
// let parameters = [] as [AnyObject]
131-
// let sendTx = contract.method("fallback", parameters: parameters)!
132-
//
133-
// let valueToSend = Web3.Utils.parseToBigUInt("1.0", units: .eth)
134-
// sendTx.transactionOptions.value = valueToSend
135-
// sendTx.transactionOptions.from = allAddresses[0]
136-
//
137-
// let _ = try sendTx.sendPromise().wait()
138-
//
139-
// let result = try ganache.txPool.getInspect()
140-
//
141-
// print(result)
142-
// }
143-
//
144-
// func testTxPoolContent() throws {
145-
// let allAddresses = try ganache.eth.getAccounts()
146-
//
147-
// let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
148-
// let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
149-
//
150-
// let parameters = [] as [AnyObject]
151-
// let sendTx = contract.method("fallback", parameters: parameters)!
152-
//
153-
// let valueToSend = Web3.Utils.parseToBigUInt("1.0", units: .eth)
154-
// sendTx.transactionOptions.value = valueToSend
155-
// sendTx.transactionOptions.from = allAddresses[0]
156-
//
157-
// let _ = try sendTx.sendPromise().wait()
158-
//
159-
// let result = try ganache.txPool.getContent()
160-
//
161-
// print(result)
162-
// }
16397
}

Tests/web3swiftTests/localTests/EIP681Tests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ class EIP681Tests: LocalTestCase {
1515
// ERC20 transfer
1616
// ethereum:0x8932404A197D84Ec3Ea55971AADE11cdA1dddff1/transfer?address=0x6891dC3962e710f0ff711B9c6acc26133Fd35Cb4&uint256=1
1717

18-
func testEIP681Parsing() throws {
19-
let parsed = Web3.EIP681CodeParser.parse("ethereum:0x5ffc014343cd971b7eb70732021e26c35b744cc4?value=2.014e18")
18+
func testEIP681Parsing() async throws {
19+
let parsed = await Web3.EIP681CodeParser.parse("ethereum:0x5ffc014343cd971b7eb70732021e26c35b744cc4?value=2.014e18")
2020
XCTAssert(parsed != nil)
2121
}
2222

23-
func testEIP681Parsing2() throws {
24-
let parsed = Web3.EIP681CodeParser.parse("ethereum:0x8932404A197D84Ec3Ea55971AADE11cdA1dddff1/transfer?address=0x6891dC3962e710f0ff711B9c6acc26133Fd35Cb4&uint256=1")
23+
func testEIP681Parsing2() async throws {
24+
let parsed = await Web3.EIP681CodeParser.parse("ethereum:0x8932404A197D84Ec3Ea55971AADE11cdA1dddff1/transfer?address=0x6891dC3962e710f0ff711B9c6acc26133Fd35Cb4&uint256=1")
2525
XCTAssert(parsed != nil)
2626
}
2727

28-
func testEIP681ENSParsing() throws {
29-
let parsed = Web3.EIP681CodeParser.parse("ethereum:somename.eth/transfer?address=somename.eth&uint256=1")
28+
func testEIP681ENSParsing() async throws {
29+
let parsed = await Web3.EIP681CodeParser.parse("ethereum:somename.eth/transfer?address=somename.eth&uint256=1")
3030
XCTAssert(parsed != nil)
3131
}
3232
}

Tests/web3swiftTests/localTests/ERC20ClassTests.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,32 @@
66

77
import XCTest
88
import BigInt
9+
import Core
910

1011
@testable import web3swift
1112

12-
class ERC20ClassTests: LocalTestCase {
13+
class ERC20ClassTests: XCTestCase {
1314

14-
func testERC20TokenCreation() throws {
15-
let (_, receipt, _) = try TestHelpers.localDeployERC20(ganache)
16-
let erc20token = ERC20.init(web3: ganache, provider: ganache.provider, address: receipt.contractAddress!)
17-
erc20token.readProperties()
18-
XCTAssert(erc20token.symbol == "w3s")
19-
XCTAssert(erc20token.name == "web3swift")
20-
XCTAssert(erc20token.decimals == 18)
15+
func testERC20TokenCreation() async throws {
16+
let (web3, _, receipt, _) = try await TestHelpers.localDeployERC20()
17+
let erc20token = ERC20.init(web3: web3, provider: web3.provider, address: receipt.contractAddress!)
18+
try await erc20token.readProperties()
19+
let symbol = try await erc20token.symbol()
20+
let name = try await erc20token.name()
21+
let decimals = try await erc20token.decimals()
22+
XCTAssert(symbol == "w3s")
23+
XCTAssert(name == "web3swift")
24+
XCTAssert(decimals == 18)
2125
}
2226

23-
func testERC20tokenBalanceAndAllowance() throws {
24-
let (_, receipt, _) = try TestHelpers.localDeployERC20(ganache)
25-
let erc20token = ERC20.init(web3: ganache, provider: ganache.provider, address: receipt.contractAddress!)
27+
func testERC20tokenBalanceAndAllowance() async throws {
28+
let (web3, _, receipt, _) = try await TestHelpers.localDeployERC20()
29+
let erc20token = ERC20.init(web3: web3, provider: web3.provider, address: receipt.contractAddress!)
2630

2731
let userAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
2832

29-
let balance = try erc20token.getBalance(account: userAddress)
30-
let allowance = try erc20token.getAllowance(originalOwner: userAddress, delegate: userAddress)
33+
let balance = try await erc20token.getBalance(account: userAddress)
34+
let allowance = try await erc20token.getAllowance(originalOwner: userAddress, delegate: userAddress)
3135
XCTAssert(String(balance) == "1024")
3236
XCTAssert(allowance == 0)
3337
}

Tests/web3swiftTests/localTests/ERC20Tests.swift

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,43 @@
33
// Created by Alex Vlasov.
44
// Copyright © 2018 Alex Vlasov. All rights reserved.
55
//
6-
76
import XCTest
8-
import BigInt
7+
import Core
98

109
@testable import web3swift
11-
12-
class ERC20Tests: LocalTestCase {
13-
14-
func testERC20name() throws {
15-
let (_, receipt, _) = try TestHelpers.localDeployERC20(ganache)
16-
17-
let parameters = [] as [AnyObject]
18-
let contract = ganache.contract(Web3.Utils.erc20ABI, at: receipt.contractAddress!)!
19-
let readTX = contract.read("name", parameters: parameters)!
20-
readTX.transactionOptions.from = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")
21-
let response = try readTX.callPromise().wait()
22-
let name = response["0"] as? String
23-
XCTAssert(name == "web3swift", "Failed to create ERC20 name transaction")
10+
//TODO: refactor me
11+
class web3swiftEventloopTests: XCTestCase {
12+
13+
func testBasicEventLoop() async throws {
14+
var ticksToWait = 5
15+
let expectation = self.expectation(description: "Waiting")
16+
func getBlockNumber(_ web3: web3) async {
17+
do {
18+
let blockNumber = try await web3.eth.blockNumber()
19+
print("Block number = " + String(blockNumber))
20+
ticksToWait = ticksToWait - 1
21+
if ticksToWait == 0 {
22+
expectation.fulfill()
23+
}
24+
} catch {
25+
print(error)
26+
}
27+
}
28+
29+
let web3main = try await Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
30+
let functionToCall: web3.Eventloop.EventLoopCall = getBlockNumber
31+
let monitoredProperty = web3.Eventloop.MonitoredProperty.init(name: "onNewBlock", calledFunction: functionToCall)
32+
web3main.eventLoop.monitoredProperties.append(monitoredProperty)
33+
web3main.eventLoop.start(5)
34+
35+
await waitForExpectations(timeout: 60, handler: nil)
2436
}
2537

26-
func testERC20tokenBalance() throws {
27-
let (_, receipt, _) = try TestHelpers.localDeployERC20(ganache)
28-
29-
let addressOfUser = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
30-
let contract = ganache.contract(Web3.Utils.erc20ABI, at: receipt.contractAddress!, abiVersion: 2)!
31-
guard let readTX = contract.read("balanceOf", parameters: [addressOfUser] as [AnyObject]) else { return XCTFail() }
32-
readTX.transactionOptions.from = addressOfUser
33-
let tokenBalance = try readTX.callPromise().wait()
34-
guard let bal = tokenBalance["0"] as? BigUInt else { return XCTFail() }
35-
print(String(bal))
38+
func getKeystoreData() -> Data? {
39+
let bundle = Bundle(for: type(of: self))
40+
guard let path = bundle.path(forResource: "key", ofType: "json") else {return nil}
41+
guard let data = NSData(contentsOfFile: path) else {return nil}
42+
return data as Data
3643
}
3744

3845
}

0 commit comments

Comments
 (0)