Skip to content

Commit 8992c75

Browse files
committed
[TIDY] Async/await APIs available without version requirement. Use only async/await for tests
1 parent f1e2f10 commit 8992c75

File tree

15 files changed

+104
-597
lines changed

15 files changed

+104
-597
lines changed

web3.swift.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Pod::Spec.new do |s|
88
s.source = { :git => 'https://github.com/argentlabs/web3.swift.git', :tag => s.version.to_s }
99
s.module_name = 'web3'
1010

11-
s.swift_version = '5.0'
12-
s.ios.deployment_target = '11.0'
11+
s.swift_version = '5.5'
12+
s.ios.deployment_target = '13.0'
1313

1414
s.source_files = 'web3swift/src/**/*.swift', 'web3swift/lib/**/*.{c,h,swift}'
1515
s.pod_target_xcconfig = {

web3sTests/Client/EthereumClientTests.swift

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ struct TransferMatchingSignatureEvent: ABIEvent {
3535
class EthereumClientTests: XCTestCase {
3636
var client: EthereumClient?
3737
var account: EthereumAccount?
38-
let timeout = 10.0
3938

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

47-
func testEthGetTransactionCount() {
48-
let expectation = XCTestExpectation(description: "get transaction receipt")
49-
50-
client?.eth_getTransactionCount(address: account!.address, block: .Latest, completion: { (error, count) in
51-
XCTAssertNotNil(count, "Transaction count not available: \(error?.localizedDescription ?? "no error")")
52-
expectation.fulfill()
53-
})
54-
55-
wait(for: [expectation], timeout: timeout)
46+
func testEthGetTransactionCount() async {
47+
do {
48+
let count = try await client?.eth_getTransactionCount(address: account!.address, block: .Latest)
49+
XCTAssertNotEqual(count, 0)
50+
} catch {
51+
XCTFail("Expected count but failed \(error).")
52+
}
5653
}
5754

58-
func testEthGetTransactionCountPending() {
59-
let expectation = XCTestExpectation(description: "get transaction receipt")
60-
61-
client?.eth_getTransactionCount(address: account!.address, block: .Pending, completion: { (error, count) in
62-
XCTAssertNotNil(count, "Transaction count not available: \(error?.localizedDescription ?? "no error")")
63-
expectation.fulfill()
64-
})
65-
66-
wait(for: [expectation], timeout: timeout)
55+
func testEthGetTransactionCountPending() async {
56+
do {
57+
let count = try await client?.eth_getTransactionCount(address: account!.address, block: .Pending)
58+
XCTAssertNotEqual(count, 0)
59+
} catch {
60+
XCTFail("Expected count but failed \(error).")
61+
}
6762
}
68-
}
69-
70-
#if compiler(>=5.5) && canImport(_Concurrency)
7163

72-
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
73-
extension EthereumClientTests {
74-
func testEthGetBalance_Async() async throws {
64+
func testEthGetBalance() async throws {
7565
do {
7666
let balance = try await client?.eth_getBalance(address: account?.address ?? .zero, block: .Latest)
7767
XCTAssertNotNil(balance, "Balance not available")
@@ -80,7 +70,7 @@ extension EthereumClientTests {
8070
}
8171
}
8272

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

92-
func testNetVersion_Async() async {
82+
func testNetVersion() async {
9383
do {
9484
let network = try await client?.net_version()
9585
XCTAssertEqual(network, EthereumNetwork.Ropsten, "Network incorrect")
@@ -98,7 +88,7 @@ extension EthereumClientTests {
9888
}
9989
}
10090

101-
func testEthGasPrice_Async() async {
91+
func testEthGasPrice() async {
10292
do {
10393
let gas = try await client?.eth_gasPrice()
10494
XCTAssertNotNil(gas, "Gas not available")
@@ -107,7 +97,7 @@ extension EthereumClientTests {
10797
}
10898
}
10999

110-
func testEthBlockNumber_Async() async {
100+
func testEthBlockNumber() async {
111101
do {
112102
let block = try await client?.eth_blockNumber()
113103
XCTAssertNotNil(block, "Block not available")
@@ -116,7 +106,7 @@ extension EthereumClientTests {
116106
}
117107
}
118108

119-
func testEthGetCode_Async() async {
109+
func testEthGetCode() async {
120110
do {
121111
let code = try await client?.eth_getCode(address: EthereumAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010"))
122112
XCTAssertNotNil(code, "Contract code not available")
@@ -125,7 +115,7 @@ extension EthereumClientTests {
125115
}
126116
}
127117

128-
func testEthSendRawTransaction_Async() async {
118+
func testEthSendRawTransaction() async {
129119
do {
130120
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)
131121

@@ -136,7 +126,7 @@ extension EthereumClientTests {
136126
}
137127
}
138128

139-
func testEthGetTransactionReceipt_Async() async {
129+
func testEthGetTransactionReceipt() async {
140130
do {
141131
let txHash = "0xc51002441dc669ad03697fd500a7096c054b1eb2ce094821e68831a3666fc878"
142132
let receipt = try await client?.eth_getTransactionReceipt(txHash: txHash)
@@ -146,7 +136,7 @@ extension EthereumClientTests {
146136
}
147137
}
148138

149-
func testEthCall_Async() async {
139+
func testEthCall() async {
150140
do {
151141
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)
152142
let txHash = try await client?.eth_call(tx, block: .Latest)
@@ -156,7 +146,7 @@ extension EthereumClientTests {
156146
}
157147
}
158148

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

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

178-
func testGivenGenesisBlock_ThenReturnsByNumber_Async() async {
168+
func testGivenGenesisBlock_ThenReturnsByNumber() async {
179169
do {
180170
let block = try await client?.eth_getBlockByNumber(.Number(0))
181171
XCTAssertEqual(block?.timestamp.timeIntervalSince1970, 0)
@@ -186,7 +176,7 @@ extension EthereumClientTests {
186176
}
187177
}
188178

189-
func testGivenLatestBlock_ThenReturnsByNumber_Async() async {
179+
func testGivenLatestBlock_ThenReturnsByNumber() async {
190180
do {
191181
let block = try await client?.eth_getBlockByNumber(.Latest)
192182
XCTAssertNotNil(block?.number.intValue)
@@ -195,7 +185,7 @@ extension EthereumClientTests {
195185
}
196186
}
197187

198-
func testGivenExistingBlock_ThenGetsBlockByNumber_Async() async {
188+
func testGivenExistingBlock_ThenGetsBlockByNumber() async {
199189
do {
200190
let block = try await client?.eth_getBlockByNumber(.Number(3415757))
201191
XCTAssertEqual(block?.number, .Number(3415757))
@@ -207,7 +197,7 @@ extension EthereumClientTests {
207197
}
208198
}
209199

210-
func testGivenUnexistingBlockNumber_ThenGetBlockByNumberReturnsError_Async() async {
200+
func testGivenUnexistingBlockNumber_ThenGetBlockByNumberReturnsError() async {
211201
do {
212202
let _ = try await client?.eth_getBlockByNumber(.Number(Int.max))
213203
XCTFail("Expected to throw while awaiting, but succeeded")
@@ -216,7 +206,7 @@ extension EthereumClientTests {
216206
}
217207
}
218208

219-
func testGivenMinedTransactionHash_ThenGetsTransactionByHash_Async() async {
209+
func testGivenMinedTransactionHash_ThenGetsTransactionByHash() async {
220210
do {
221211
let transaction = try await client?.eth_getTransaction(byHash: "0x014726c783ab2fd6828a9ca556850bccfc66f70926f411274eaf886385c704af")
222212
XCTAssertEqual(transaction?.from?.value, "0xbbf5029fd710d227630c8b7d338051b8e76d50b3")
@@ -232,7 +222,7 @@ extension EthereumClientTests {
232222
}
233223
}
234224

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

244-
func testGivenNoFilters_WhenMatchingSingleTransferEvents_AllEventsReturned_Async() async {
234+
func testGivenNoFilters_WhenMatchingSingleTransferEvents_AllEventsReturned() async {
245235
do {
246236
let to = try! ABIEncoder.encode(EthereumAddress("0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854"))
247237

@@ -257,7 +247,7 @@ extension EthereumClientTests {
257247
}
258248
}
259249

260-
func testGivenNoFilters_WhenMatchingMultipleTransferEvents_BothEventsReturned_Async() async {
250+
func testGivenNoFilters_WhenMatchingMultipleTransferEvents_BothEventsReturned() async {
261251
do {
262252
let to = try! ABIEncoder.encode(EthereumAddress("0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854"))
263253

@@ -273,7 +263,7 @@ extension EthereumClientTests {
273263
}
274264
}
275265

276-
func testGivenContractFilter_WhenMatchingSingleTransferEvents_OnlyMatchingSourceEventReturned_Async() async {
266+
func testGivenContractFilter_WhenMatchingSingleTransferEvents_OnlyMatchingSourceEventReturned() async {
277267
do {
278268
let to = try! ABIEncoder.encodeRaw("0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854", forType: ABIRawType.FixedAddress)
279269
let filters = [
@@ -292,7 +282,7 @@ extension EthereumClientTests {
292282
}
293283
}
294284

295-
func testGivenContractFilter_WhenMatchingMultipleTransferEvents_OnlyMatchingSourceEventsReturned_Async() async {
285+
func testGivenContractFilter_WhenMatchingMultipleTransferEvents_OnlyMatchingSourceEventsReturned() async {
296286
do {
297287
let to = try! ABIEncoder.encode(EthereumAddress("0x3C1Bd6B420448Cf16A389C8b0115CCB3660bB854"))
298288
let filters = [
@@ -312,7 +302,7 @@ extension EthereumClientTests {
312302
}
313303
}
314304

315-
func test_GivenDynamicArrayResponse_ThenCallReceivesData_Async() async {
305+
func test_GivenDynamicArrayResponse_ThenCallReceivesData() async {
316306
do {
317307
let function = GetGuardians(wallet: EthereumAddress("0x2A6295C34b4136F2C3c1445c6A0338D784fe0ddd"))
318308

@@ -323,7 +313,7 @@ extension EthereumClientTests {
323313
}
324314
}
325315

326-
func test_GivenUnimplementedMethod_WhenCallingContract_ThenFailsWithExecutionError_Async() async {
316+
func test_GivenUnimplementedMethod_WhenCallingContract_ThenFailsWithExecutionError() async {
327317
do {
328318
let function = InvalidMethodA(param: .zero)
329319
let _ = try await function.call(
@@ -340,7 +330,7 @@ extension EthereumClientTests {
340330
}
341331
}
342332

343-
func test_GivenValidTransaction_ThenEstimatesGas_Async() async {
333+
func test_GivenValidTransaction_ThenEstimatesGas() async {
344334
do {
345335
let function = TransferToken(wallet: EthereumAddress("0xD18dE36e6FB4a5A069f673723Fab71cc00C6CE5F"),
346336
token: EthereumAddress("0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"),
@@ -358,8 +348,6 @@ extension EthereumClientTests {
358348
}
359349
}
360350

361-
#endif
362-
363351
struct GetGuardians: ABIFunction {
364352
static let name = "getGuardians"
365353
let contract = EthereumAddress("0x25BD64224b7534f7B9e3E16dd10b6dED1A412b90")

web3sTests/Contract/ABIEventTests.swift

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,7 @@ class ABIEventTests: XCTestCase {
1717
self.client = EthereumClient(url: URL(string: TestConfig.clientUrl)!)
1818
}
1919

20-
func test_givenEventWithData4_ItParsesCorrectly() {
21-
let expect = expectation(description: "Request")
22-
23-
let encodedAddress = (try? ABIEncoder.encode(EthereumAddress("0x3B6Def16666a23905DD29071d13E7a9db08240E2")).bytes) ?? []
24-
25-
client.getEvents(addresses: nil,
26-
topics: [try? EnabledStaticCall.signature(), String(hexFromBytes: encodedAddress), nil],
27-
fromBlock: .Number(8386245),
28-
toBlock: .Number(8386245),
29-
eventTypes: [EnabledStaticCall.self]) { (error, events, logs) in
30-
XCTAssertNil(error)
31-
let event = events.first as? EnabledStaticCall
32-
XCTAssertEqual(event?.module, EthereumAddress("0x3b6def16666a23905dd29071d13e7a9db08240e2"))
33-
XCTAssertEqual(event?.method, Data(hex: "0x20c13b0b")!)
34-
35-
let event1 = events.last as? EnabledStaticCall
36-
XCTAssertEqual(event1?.module, EthereumAddress("0x3b6def16666a23905dd29071d13e7a9db08240e2"))
37-
XCTAssertEqual(event1?.method, Data(hex: "0x1626ba7e")!)
38-
expect.fulfill()
39-
}
40-
41-
waitForExpectations(timeout: 10)
42-
}
43-
44-
func test_givenEventWithData32_ItParsesCorrectly() {
45-
let expect = expectation(description: "Request")
46-
47-
client.getEvents(addresses: nil,
48-
topics: [try? UpgraderRegistered.signature()],
49-
fromBlock: .Number(
50-
8110676 ),
51-
toBlock: .Number(
52-
8110676 ),
53-
eventTypes: [UpgraderRegistered.self]) { (error, events, logs) in
54-
XCTAssertNil(error)
55-
XCTAssertEqual(events.count, 1)
56-
let event = events.first as? UpgraderRegistered
57-
XCTAssertEqual(event?.upgrader, EthereumAddress("0x17b11d842ae09eddedf5592f8271a7d07f6931e7"))
58-
XCTAssertEqual(event?.name, Data(hex: "0x307864323664616666635f307833373731376663310000000000000000000000")!)
59-
expect.fulfill()
60-
}
61-
62-
waitForExpectations(timeout: 10)
63-
}
64-
}
65-
66-
#if compiler(>=5.5) && canImport(_Concurrency)
67-
68-
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
69-
extension ABIEventTests {
70-
func test_givenEventWithData4_ItParsesCorrectly_Async() async {
20+
func test_givenEventWithData4_ItParsesCorrectly() async {
7121
do {
7222
let encodedAddress = (try? ABIEncoder.encode(EthereumAddress("0x3B6Def16666a23905DD29071d13E7a9db08240E2")).bytes) ?? []
7323

@@ -89,7 +39,7 @@ extension ABIEventTests {
8939
}
9040
}
9141

92-
func test_givenEventWithData32_ItParsesCorrectly_Async() async {
42+
func test_givenEventWithData32_ItParsesCorrectly() async {
9343
do {
9444
let eventsResult = try await client.getEvents(addresses: nil,
9545
topics: [try? UpgraderRegistered.signature()],
@@ -109,8 +59,6 @@ extension ABIEventTests {
10959
}
11060
}
11161

112-
#endif
113-
11462
struct EnabledStaticCall: ABIEvent {
11563
static let name = "EnabledStaticCall"
11664
static let types: [ABIType.Type] = [EthereumAddress.self,Data4.self]

0 commit comments

Comments
 (0)