Skip to content

Commit

Permalink
[TIDY] Async/await APIs available without version requirement. Use on…
Browse files Browse the repository at this point in the history
…ly async/await for tests
  • Loading branch information
DarthMike committed May 20, 2022
1 parent f1e2f10 commit 8992c75
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 597 deletions.
4 changes: 2 additions & 2 deletions web3.swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Pod::Spec.new do |s|
s.source = { :git => 'https://github.com/argentlabs/web3.swift.git', :tag => s.version.to_s }
s.module_name = 'web3'

s.swift_version = '5.0'
s.ios.deployment_target = '11.0'
s.swift_version = '5.5'
s.ios.deployment_target = '13.0'

s.source_files = 'web3swift/src/**/*.swift', 'web3swift/lib/**/*.{c,h,swift}'
s.pod_target_xcconfig = {
Expand Down
88 changes: 38 additions & 50 deletions web3sTests/Client/EthereumClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ struct TransferMatchingSignatureEvent: ABIEvent {
class EthereumClientTests: XCTestCase {
var client: EthereumClient?
var account: EthereumAccount?
let timeout = 10.0

override func setUp() {
super.setUp()
Expand All @@ -44,34 +43,25 @@ class EthereumClientTests: XCTestCase {
print("Public address: \(self.account?.address.value ?? "NONE")")
}

func testEthGetTransactionCount() {
let expectation = XCTestExpectation(description: "get transaction receipt")

client?.eth_getTransactionCount(address: account!.address, block: .Latest, completion: { (error, count) in
XCTAssertNotNil(count, "Transaction count not available: \(error?.localizedDescription ?? "no error")")
expectation.fulfill()
})

wait(for: [expectation], timeout: timeout)
func testEthGetTransactionCount() async {
do {
let count = try await client?.eth_getTransactionCount(address: account!.address, block: .Latest)
XCTAssertNotEqual(count, 0)
} catch {
XCTFail("Expected count but failed \(error).")
}
}

func testEthGetTransactionCountPending() {
let expectation = XCTestExpectation(description: "get transaction receipt")

client?.eth_getTransactionCount(address: account!.address, block: .Pending, completion: { (error, count) in
XCTAssertNotNil(count, "Transaction count not available: \(error?.localizedDescription ?? "no error")")
expectation.fulfill()
})

wait(for: [expectation], timeout: timeout)
func testEthGetTransactionCountPending() async {
do {
let count = try await client?.eth_getTransactionCount(address: account!.address, block: .Pending)
XCTAssertNotEqual(count, 0)
} catch {
XCTFail("Expected count but failed \(error).")
}
}
}

#if compiler(>=5.5) && canImport(_Concurrency)

@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
extension EthereumClientTests {
func testEthGetBalance_Async() async throws {
func testEthGetBalance() async throws {
do {
let balance = try await client?.eth_getBalance(address: account?.address ?? .zero, block: .Latest)
XCTAssertNotNil(balance, "Balance not available")
Expand All @@ -80,7 +70,7 @@ extension EthereumClientTests {
}
}

func testEthGetBalanceIncorrectAddress_Async() async {
func testEthGetBalanceIncorrectAddress() async {
do {
_ = try await client?.eth_getBalance(address: EthereumAddress("0xnig42niog2"), block: .Latest)
XCTFail("Expected to throw while awaiting, but succeeded")
Expand All @@ -89,7 +79,7 @@ extension EthereumClientTests {
}
}

func testNetVersion_Async() async {
func testNetVersion() async {
do {
let network = try await client?.net_version()
XCTAssertEqual(network, EthereumNetwork.Ropsten, "Network incorrect")
Expand All @@ -98,7 +88,7 @@ extension EthereumClientTests {
}
}

func testEthGasPrice_Async() async {
func testEthGasPrice() async {
do {
let gas = try await client?.eth_gasPrice()
XCTAssertNotNil(gas, "Gas not available")
Expand All @@ -107,7 +97,7 @@ extension EthereumClientTests {
}
}

func testEthBlockNumber_Async() async {
func testEthBlockNumber() async {
do {
let block = try await client?.eth_blockNumber()
XCTAssertNotNil(block, "Block not available")
Expand All @@ -116,7 +106,7 @@ extension EthereumClientTests {
}
}

func testEthGetCode_Async() async {
func testEthGetCode() async {
do {
let code = try await client?.eth_getCode(address: EthereumAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010"))
XCTAssertNotNil(code, "Contract code not available")
Expand All @@ -125,7 +115,7 @@ extension EthereumClientTests {
}
}

func testEthSendRawTransaction_Async() async {
func testEthSendRawTransaction() async {
do {
let tx = EthereumTransaction(from: nil, to: EthereumAddress("0x3c1bd6b420448cf16a389c8b0115ccb3660bb854"), value: BigUInt(1600000), data: nil, nonce: 2, gasPrice: BigUInt(4000000), gasLimit: BigUInt(500000), chainId: EthereumNetwork.Ropsten.intValue)

Expand All @@ -136,7 +126,7 @@ extension EthereumClientTests {
}
}

func testEthGetTransactionReceipt_Async() async {
func testEthGetTransactionReceipt() async {
do {
let txHash = "0xc51002441dc669ad03697fd500a7096c054b1eb2ce094821e68831a3666fc878"
let receipt = try await client?.eth_getTransactionReceipt(txHash: txHash)
Expand All @@ -146,7 +136,7 @@ extension EthereumClientTests {
}
}

func testEthCall_Async() async {
func testEthCall() async {
do {
let tx = EthereumTransaction(from: nil, to: EthereumAddress("0x3c1bd6b420448cf16a389c8b0115ccb3660bb854"), value: BigUInt(1800000), data: nil, nonce: 2, gasPrice: BigUInt(400000), gasLimit: BigUInt(50000), chainId: EthereumNetwork.Ropsten.intValue)
let txHash = try await client?.eth_call(tx, block: .Latest)
Expand All @@ -156,7 +146,7 @@ extension EthereumClientTests {
}
}

func testSimpleEthGetLogs_Async() async {
func testSimpleEthGetLogs() async {
do {
let logs = try await client?.eth_getLogs(addresses: [EthereumAddress("0x23d0a442580c01e420270fba6ca836a8b2353acb")], topics: nil, fromBlock: .Earliest, toBlock: .Latest)
XCTAssertNotNil(logs, "Logs not available")
Expand All @@ -165,7 +155,7 @@ extension EthereumClientTests {
}
}

func testOrTopicsEthGetLogs_Async() async {
func testOrTopicsEthGetLogs() async {
do {
let logs = try await client?.eth_getLogs(addresses: nil, orTopics: [["0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c", "0x7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65"], ["0x000000000000000000000000655ef694b98e55977a93259cb3b708560869a8f3"]], fromBlock: .Number(6540313), toBlock: .Number(6540397))
XCTAssertEqual(logs?.count, 2)
Expand All @@ -175,7 +165,7 @@ extension EthereumClientTests {
}
}

func testGivenGenesisBlock_ThenReturnsByNumber_Async() async {
func testGivenGenesisBlock_ThenReturnsByNumber() async {
do {
let block = try await client?.eth_getBlockByNumber(.Number(0))
XCTAssertEqual(block?.timestamp.timeIntervalSince1970, 0)
Expand All @@ -186,7 +176,7 @@ extension EthereumClientTests {
}
}

func testGivenLatestBlock_ThenReturnsByNumber_Async() async {
func testGivenLatestBlock_ThenReturnsByNumber() async {
do {
let block = try await client?.eth_getBlockByNumber(.Latest)
XCTAssertNotNil(block?.number.intValue)
Expand All @@ -195,7 +185,7 @@ extension EthereumClientTests {
}
}

func testGivenExistingBlock_ThenGetsBlockByNumber_Async() async {
func testGivenExistingBlock_ThenGetsBlockByNumber() async {
do {
let block = try await client?.eth_getBlockByNumber(.Number(3415757))
XCTAssertEqual(block?.number, .Number(3415757))
Expand All @@ -207,7 +197,7 @@ extension EthereumClientTests {
}
}

func testGivenUnexistingBlockNumber_ThenGetBlockByNumberReturnsError_Async() async {
func testGivenUnexistingBlockNumber_ThenGetBlockByNumberReturnsError() async {
do {
let _ = try await client?.eth_getBlockByNumber(.Number(Int.max))
XCTFail("Expected to throw while awaiting, but succeeded")
Expand All @@ -216,7 +206,7 @@ extension EthereumClientTests {
}
}

func testGivenMinedTransactionHash_ThenGetsTransactionByHash_Async() async {
func testGivenMinedTransactionHash_ThenGetsTransactionByHash() async {
do {
let transaction = try await client?.eth_getTransaction(byHash: "0x014726c783ab2fd6828a9ca556850bccfc66f70926f411274eaf886385c704af")
XCTAssertEqual(transaction?.from?.value, "0xbbf5029fd710d227630c8b7d338051b8e76d50b3")
Expand All @@ -232,7 +222,7 @@ extension EthereumClientTests {
}
}

func testGivenUnexistingTransactionHash_ThenErrorsGetTransactionByHash_Async() async {
func testGivenUnexistingTransactionHash_ThenErrorsGetTransactionByHash() async {
do {
let _ = try await client?.eth_getTransaction(byHash: "0x01234")
XCTFail("Expected to throw while awaiting, but succeeded")
Expand All @@ -241,7 +231,7 @@ extension EthereumClientTests {
}
}

func testGivenNoFilters_WhenMatchingSingleTransferEvents_AllEventsReturned_Async() async {
func testGivenNoFilters_WhenMatchingSingleTransferEvents_AllEventsReturned() async {
do {
let to = try! ABIEncoder.encode(EthereumAddress("0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854"))

Expand All @@ -257,7 +247,7 @@ extension EthereumClientTests {
}
}

func testGivenNoFilters_WhenMatchingMultipleTransferEvents_BothEventsReturned_Async() async {
func testGivenNoFilters_WhenMatchingMultipleTransferEvents_BothEventsReturned() async {
do {
let to = try! ABIEncoder.encode(EthereumAddress("0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854"))

Expand All @@ -273,7 +263,7 @@ extension EthereumClientTests {
}
}

func testGivenContractFilter_WhenMatchingSingleTransferEvents_OnlyMatchingSourceEventReturned_Async() async {
func testGivenContractFilter_WhenMatchingSingleTransferEvents_OnlyMatchingSourceEventReturned() async {
do {
let to = try! ABIEncoder.encodeRaw("0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854", forType: ABIRawType.FixedAddress)
let filters = [
Expand All @@ -292,7 +282,7 @@ extension EthereumClientTests {
}
}

func testGivenContractFilter_WhenMatchingMultipleTransferEvents_OnlyMatchingSourceEventsReturned_Async() async {
func testGivenContractFilter_WhenMatchingMultipleTransferEvents_OnlyMatchingSourceEventsReturned() async {
do {
let to = try! ABIEncoder.encode(EthereumAddress("0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854"))
let filters = [
Expand All @@ -312,7 +302,7 @@ extension EthereumClientTests {
}
}

func test_GivenDynamicArrayResponse_ThenCallReceivesData_Async() async {
func test_GivenDynamicArrayResponse_ThenCallReceivesData() async {
do {
let function = GetGuardians(wallet: EthereumAddress("0x2A6295C34b4136F2C3c1445c6A0338D784fe0ddd"))

Expand All @@ -323,7 +313,7 @@ extension EthereumClientTests {
}
}

func test_GivenUnimplementedMethod_WhenCallingContract_ThenFailsWithExecutionError_Async() async {
func test_GivenUnimplementedMethod_WhenCallingContract_ThenFailsWithExecutionError() async {
do {
let function = InvalidMethodA(param: .zero)
let _ = try await function.call(
Expand All @@ -340,7 +330,7 @@ extension EthereumClientTests {
}
}

func test_GivenValidTransaction_ThenEstimatesGas_Async() async {
func test_GivenValidTransaction_ThenEstimatesGas() async {
do {
let function = TransferToken(wallet: EthereumAddress("0xD18dE36e6FB4a5A069f673723Fab71cc00C6CE5F"),
token: EthereumAddress("0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"),
Expand All @@ -358,8 +348,6 @@ extension EthereumClientTests {
}
}

#endif

struct GetGuardians: ABIFunction {
static let name = "getGuardians"
let contract = EthereumAddress("0x25BD64224b7534f7B9e3E16dd10b6dED1A412b90")
Expand Down
56 changes: 2 additions & 54 deletions web3sTests/Contract/ABIEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,7 @@ class ABIEventTests: XCTestCase {
self.client = EthereumClient(url: URL(string: TestConfig.clientUrl)!)
}

func test_givenEventWithData4_ItParsesCorrectly() {
let expect = expectation(description: "Request")

let encodedAddress = (try? ABIEncoder.encode(EthereumAddress("0x3B6Def16666a23905DD29071d13E7a9db08240E2")).bytes) ?? []

client.getEvents(addresses: nil,
topics: [try? EnabledStaticCall.signature(), String(hexFromBytes: encodedAddress), nil],
fromBlock: .Number(8386245),
toBlock: .Number(8386245),
eventTypes: [EnabledStaticCall.self]) { (error, events, logs) in
XCTAssertNil(error)
let event = events.first as? EnabledStaticCall
XCTAssertEqual(event?.module, EthereumAddress("0x3b6def16666a23905dd29071d13e7a9db08240e2"))
XCTAssertEqual(event?.method, Data(hex: "0x20c13b0b")!)

let event1 = events.last as? EnabledStaticCall
XCTAssertEqual(event1?.module, EthereumAddress("0x3b6def16666a23905dd29071d13e7a9db08240e2"))
XCTAssertEqual(event1?.method, Data(hex: "0x1626ba7e")!)
expect.fulfill()
}

waitForExpectations(timeout: 10)
}

func test_givenEventWithData32_ItParsesCorrectly() {
let expect = expectation(description: "Request")

client.getEvents(addresses: nil,
topics: [try? UpgraderRegistered.signature()],
fromBlock: .Number(
8110676 ),
toBlock: .Number(
8110676 ),
eventTypes: [UpgraderRegistered.self]) { (error, events, logs) in
XCTAssertNil(error)
XCTAssertEqual(events.count, 1)
let event = events.first as? UpgraderRegistered
XCTAssertEqual(event?.upgrader, EthereumAddress("0x17b11d842ae09eddedf5592f8271a7d07f6931e7"))
XCTAssertEqual(event?.name, Data(hex: "0x307864323664616666635f307833373731376663310000000000000000000000")!)
expect.fulfill()
}

waitForExpectations(timeout: 10)
}
}

#if compiler(>=5.5) && canImport(_Concurrency)

@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
extension ABIEventTests {
func test_givenEventWithData4_ItParsesCorrectly_Async() async {
func test_givenEventWithData4_ItParsesCorrectly() async {
do {
let encodedAddress = (try? ABIEncoder.encode(EthereumAddress("0x3B6Def16666a23905DD29071d13E7a9db08240E2")).bytes) ?? []

Expand All @@ -89,7 +39,7 @@ extension ABIEventTests {
}
}

func test_givenEventWithData32_ItParsesCorrectly_Async() async {
func test_givenEventWithData32_ItParsesCorrectly() async {
do {
let eventsResult = try await client.getEvents(addresses: nil,
topics: [try? UpgraderRegistered.signature()],
Expand All @@ -109,8 +59,6 @@ extension ABIEventTests {
}
}

#endif

struct EnabledStaticCall: ABIEvent {
static let name = "EnabledStaticCall"
static let types: [ABIType.Type] = [EthereumAddress.self,Data4.self]
Expand Down
Loading

0 comments on commit 8992c75

Please sign in to comment.