|
1 |
| -//// web3swift |
2 |
| -//// |
3 |
| -//// Created by Alex Vlasov. |
4 |
| -//// Copyright © 2018 Alex Vlasov. All rights reserved. |
5 |
| -//// |
| 1 | +// web3swift |
6 | 2 | //
|
7 |
| -//import XCTest |
| 3 | +// Created by Alex Vlasov. |
| 4 | +// Copyright © 2018 Alex Vlasov. All rights reserved. |
8 | 5 | //
|
9 |
| -//@testable import web3swift |
10 |
| -// |
11 |
| -//class web3swiftEventloopTests: XCTestCase { |
12 |
| -// |
13 |
| -// func testBasicEventLoop() async throws { |
14 |
| -// let expectation = self.expectation(description: "Waiting") |
15 |
| -// @Sendable func getBlockNumber(_ web3: web3) async { |
16 |
| -// do { |
17 |
| -// let blockNumber = try await web3.eth.getBlockNumber() |
18 |
| -// print("Block number = " + String(blockNumber)) |
19 |
| -// expectation.fulfill() |
20 |
| -// } catch { |
21 |
| -// print(error) |
22 |
| -// } |
23 |
| -// } |
24 |
| -// |
25 |
| -// let web3main = try await Web3.new(URL.init(string: "http://127.0.0.1:8545")!) |
26 |
| -// async let functionToCall: web3.Eventloop.EventLoopCall = getBlockNumber |
27 |
| -// let monitoredProperty = await web3.Eventloop.MonitoredProperty.init(name: "onNewBlock", calledFunction: functionToCall) |
28 |
| -// web3main.eventLoop.monitoredProperties.append(monitoredProperty) |
29 |
| -// web3main.eventLoop.start(5) |
30 |
| -// |
31 |
| -// await waitForExpectations(timeout: 60, handler: nil) |
32 |
| -// } |
33 |
| -// |
34 |
| -// func getKeystoreData() -> Data? { |
35 |
| -// let bundle = Bundle(for: type(of: self)) |
36 |
| -// guard let path = bundle.path(forResource: "key", ofType: "json") else {return nil} |
37 |
| -// guard let data = NSData(contentsOfFile: path) else {return nil} |
38 |
| -// return data as Data |
39 |
| -// } |
40 |
| -// |
41 |
| -//} |
| 6 | + |
| 7 | +import XCTest |
| 8 | + |
| 9 | +@testable import web3swift |
| 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.getBlockNumber() |
| 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) |
| 36 | + } |
| 37 | + |
| 38 | + // func testNonceMiddleware() throws { |
| 39 | + // let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) |
| 40 | + // let middleware = Web3.Utils.NonceMiddleware() |
| 41 | + // middleware.attach(web3) |
| 42 | + |
| 43 | + // let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! |
| 44 | + // let ksData = getKeystoreData() |
| 45 | + // FIXME: Ganache crash here |
| 46 | + // let tempKeystore = EthereumKeystoreV3(ksData!) |
| 47 | + // let keystoreManager = KeystoreManager([tempKeystore!]) |
| 48 | + // web3.addKeystoreManager(keystoreManager) |
| 49 | + |
| 50 | + // var tx = web3.eth.sendETH(to: sendToAddress, amount: 1000) |
| 51 | + // tx!.transactionOptions.from = tempKeystore!.addresses!.first! |
| 52 | + // var result = try! tx!.send(password: "web3swift") |
| 53 | + // let newNonce = result.transaction.nonce |
| 54 | + // sleep(1) |
| 55 | + // let hookNewNonce = middleware.nonceLookups[tempKeystore!.addresses!.first!]! |
| 56 | + // XCTAssertEqual(newNonce, hookNewNonce) |
| 57 | + |
| 58 | + // tx = web3.eth.sendETH(to: sendToAddress, amount: 1000) |
| 59 | + // tx!.transactionOptions.from = tempKeystore!.addresses!.first! |
| 60 | + // result = try! tx!.send(password: "web3swift") |
| 61 | + // sleep(1) |
| 62 | + // let newNonce2 = result.transaction.nonce |
| 63 | + // let hookNewNonce2 = middleware.nonceLookups[tempKeystore!.addresses!.first!]! |
| 64 | + // XCTAssert(newNonce2 == hookNewNonce2) |
| 65 | + // } |
| 66 | + |
| 67 | + func getKeystoreData() -> Data? { |
| 68 | + let bundle = Bundle(for: type(of: self)) |
| 69 | + guard let path = bundle.path(forResource: "key", ofType: "json") else {return nil} |
| 70 | + guard let data = NSData(contentsOfFile: path) else {return nil} |
| 71 | + return data as Data |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments