|
4 | 4 | // Copyright © 2018 Alex Vlasov. All rights reserved.
|
5 | 5 | //
|
6 | 6 | import XCTest
|
| 7 | +import BigInt |
7 | 8 | import Core
|
8 | 9 |
|
9 | 10 | @testable import web3swift
|
10 |
| -//TODO: refactor me |
11 |
| -class web3swiftEventloopTests: XCTestCase { |
12 | 11 |
|
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 |
| - } |
| 12 | +//TODO: refactor me |
| 13 | +class ERC20Tests: LocalTestCase { |
28 | 14 |
|
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) |
| 15 | + func testERC20name() async throws { |
| 16 | + let (web3, _, receipt, _) = try await TestHelpers.localDeployERC20() |
34 | 17 |
|
35 |
| - await waitForExpectations(timeout: 60, handler: nil) |
| 18 | + let parameters = [] as [AnyObject] |
| 19 | + let contract = web3.contract(Web3.Utils.erc20ABI, at: receipt.contractAddress!)! |
| 20 | + let readTX = contract.read("name", parameters:parameters)! |
| 21 | + readTX.transactionOptions.from = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901") |
| 22 | + let response = try await readTX.decodedData() |
| 23 | + let name = response["0"] as? String |
| 24 | + XCTAssert(name == "web3swift", "Failed to create ERC20 name transaction") |
36 | 25 | }
|
37 | 26 |
|
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 |
| 27 | + func testERC20tokenBalance() async throws { |
| 28 | + let (web3, _, receipt, _) = try await TestHelpers.localDeployERC20() |
| 29 | + |
| 30 | + let addressOfUser = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! |
| 31 | + let contract = web3.contract(Web3.Utils.erc20ABI, at: receipt.contractAddress!, abiVersion: 2)! |
| 32 | + guard let readTX = contract.read("balanceOf", parameters: [addressOfUser] as [AnyObject]) else {return XCTFail()} |
| 33 | + readTX.transactionOptions.from = addressOfUser |
| 34 | + let tokenBalance = try await readTX.decodedData() |
| 35 | + guard let bal = tokenBalance["0"] as? BigUInt else {return XCTFail()} |
| 36 | + print(String(bal)) |
43 | 37 | }
|
44 | 38 |
|
45 | 39 | }
|
0 commit comments