Skip to content

Commit dee0b58

Browse files
fix: returned back incorrectly merged ERC20Tests
1 parent b795104 commit dee0b58

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

Tests/web3swiftTests/localTests/ERC20Tests.swift

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,36 @@
44
// Copyright © 2018 Alex Vlasov. All rights reserved.
55
//
66
import XCTest
7+
import BigInt
78
import Core
89

910
@testable import web3swift
10-
//TODO: refactor me
11-
class web3swiftEventloopTests: XCTestCase {
1211

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 {
2814

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()
3417

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")
3625
}
3726

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))
4337
}
4438

4539
}

0 commit comments

Comments
 (0)