33// Created by Alex Vlasov.
44// Copyright © 2018 Alex Vlasov. All rights reserved.
55//
6-
6+ // TODO: Replace `XCTAssert` with more explicite `XCTAssertEqual`, where Applicable
77import XCTest
88import CryptoSwift
99import BigInt
10+ import Core
1011
1112@testable import web3swift
1213
13- class BasicLocalNodeTests : LocalTestCase {
14+ class BasicLocalNodeTests : XCTestCase {
1415
15- func testDeployWithRemoteSigning( ) throws {
16- let allAddresses = try ganache. eth. getAccounts ( )
16+ func testDeployWithRemoteSigning( ) async throws {
17+ let web3 = try await Web3 . new ( URL . init ( string: " http://127.0.0.1:8545 " ) !)
18+ let allAddresses = try await web3. eth. ownedAccounts ( )
1719
1820 let abiString = " [{ \" constant \" :true, \" inputs \" :[], \" name \" : \" getFlagData \" , \" outputs \" :[{ \" name \" : \" data \" , \" type \" : \" string \" }], \" payable \" :false, \" stateMutability \" : \" view \" , \" type \" : \" function \" },{ \" constant \" :false, \" inputs \" :[{ \" name \" : \" data \" , \" type \" : \" string \" }], \" name \" : \" setFlagData \" , \" outputs \" :[], \" payable \" :false, \" stateMutability \" : \" nonpayable \" , \" type \" : \" function \" }] "
1921 let bytecode = Data . fromHex ( " 6060604052341561000f57600080fd5b6103358061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063a16e94bf14610051578063a46b5b6b146100df575b600080fd5b341561005c57600080fd5b61006461013c565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a4578082015181840152602081019050610089565b50505050905090810190601f1680156100d15780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156100ea57600080fd5b61013a600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190505061020d565b005b610144610250565b6000808073ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102035780601f106101d857610100808354040283529160200191610203565b820191906000526020600020905b8154815290600101906020018083116101e657829003601f168201915b5050505050905090565b806000808073ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600001908051906020019061024c929190610264565b5050565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102a557805160ff19168380011785556102d3565b828001600101855582156102d3579182015b828111156102d25782518255916020019190600101906102b7565b5b5090506102e091906102e4565b5090565b61030691905b808211156103025760008160009055506001016102ea565b5090565b905600a165627a7a7230582017359d063cd7fdf56f19ca186a54863ce855c8f070acece905d8538fbbc4d1bf0029 " ) !
2022
21- let contract = ganache. contract ( abiString, at: nil , abiVersion: 2 ) !
22- let deployTx = contract. deploy ( bytecode: bytecode) !
23+ let contract = web3. contract ( abiString, at: nil , abiVersion: 2 ) !
24+
25+ let parameters = [ ] as [ AnyObject ]
26+ let deployTx = contract. deploy ( bytecode: bytecode, parameters: parameters) !
2327 deployTx. transactionOptions. from = allAddresses [ 0 ]
2428 deployTx. transactionOptions. gasLimit = . manual( 3000000 )
2529
26- let result = try deployTx. sendPromise ( ) . wait ( )
30+ let result = try await deployTx. send ( )
2731 let txHash = result. hash
2832 print ( " Transaction with hash " + txHash)
2933
3034 Thread . sleep ( forTimeInterval: 1.0 )
3135
32- let receipt = try ganache . eth. getTransactionReceipt ( txHash)
36+ let receipt = try await web3 . eth. transactionReceipt ( txHash)
3337 print ( receipt)
3438
3539 switch receipt. status {
@@ -39,34 +43,35 @@ class BasicLocalNodeTests: LocalTestCase {
3943 break
4044 }
4145
42- let details = try ganache . eth. getTransactionDetails ( txHash)
46+ let details = try await web3 . eth. transactionDetails ( txHash)
4347 print ( details)
4448 }
4549
46- func testEthSendExampleWithRemoteSigning( ) throws {
47- let allAddresses = try ganache. eth. getAccounts ( )
50+ func testEthSendExampleWithRemoteSigning( ) async throws {
51+ let web3 = try await Web3 . new ( URL ( string: " http://127.0.0.1:8545 " ) !)
52+ let allAddresses = try await web3. eth. ownedAccounts ( )
4853 let sendToAddress = EthereumAddress ( " 0xe22b8979739D724343bd002F9f432F5990879901 " ) !
49- let contract = ganache . contract ( Web3 . Utils. coldWalletABI, at: sendToAddress, abiVersion: 2 ) !
54+ let contract = web3 . contract ( Web3 . Utils. coldWalletABI, at: sendToAddress, abiVersion: 2 ) !
5055
5156 let parameters = [ ] as [ AnyObject ]
5257 let sendTx = contract. method ( " fallback " , parameters: parameters) !
5358
54- let valueToSend = Web3 . Utils . parseToBigUInt ( " 1.0 " , units: . eth) !
59+ let valueToSend = Utilities . parseToBigUInt ( " 1.0 " , units: . eth)
5560 sendTx. transactionOptions. value = valueToSend
5661 sendTx. transactionOptions. from = allAddresses [ 0 ]
5762
58- let balanceBeforeTo = try ganache . eth. getBalancePromise ( address : sendToAddress) . wait ( )
59- let balanceBeforeFrom = try ganache . eth. getBalancePromise ( address : allAddresses [ 0 ] ) . wait ( )
63+ let balanceBeforeTo = try await web3 . eth. getBalance ( for : sendToAddress)
64+ let balanceBeforeFrom = try await web3 . eth. getBalance ( for : allAddresses [ 0 ] )
6065 print ( " Balance before to: " + balanceBeforeTo. description)
6166 print ( " Balance before from: " + balanceBeforeFrom. description)
6267
63- let result = try sendTx. sendPromise ( ) . wait ( )
68+ let result = try await sendTx. send ( )
6469 let txHash = result. hash
6570 print ( " Transaction with hash " + txHash)
6671
6772 Thread . sleep ( forTimeInterval: 1.0 )
6873
69- let receipt = try ganache . eth. getTransactionReceipt ( txHash)
74+ let receipt = try await web3 . eth. transactionReceipt ( txHash)
7075 print ( receipt)
7176
7277 switch receipt. status {
@@ -76,88 +81,17 @@ class BasicLocalNodeTests: LocalTestCase {
7681 break
7782 }
7883
79- let details = try ganache . eth. getTransactionDetails ( txHash)
84+ let details = try await web3 . eth. transactionDetails ( txHash)
8085 print ( details)
8186
8287
83- let balanceAfterTo = try ganache . eth. getBalancePromise ( address : sendToAddress) . wait ( )
84- let balanceAfterFrom = try ganache . eth. getBalancePromise ( address : allAddresses [ 0 ] ) . wait ( )
88+ let balanceAfterTo = try await web3 . eth. getBalance ( for : sendToAddress)
89+ let balanceAfterFrom = try await web3 . eth. getBalance ( for : allAddresses [ 0 ] )
8590 print ( " Balance after to: " + balanceAfterTo. description)
8691 print ( " Balance after from: " + balanceAfterFrom. description)
8792
88- XCTAssertEqual ( balanceAfterTo - balanceBeforeTo, valueToSend)
93+ XCTAssert ( balanceAfterTo - balanceBeforeTo == valueToSend)
8994 let txnGasPrice = details. transaction. parameters. gasPrice ?? 0
90- XCTAssertEqual ( balanceBeforeFrom - ( balanceAfterFrom + receipt. gasUsed * txnGasPrice) , valueToSend)
95+ XCTAssert ( balanceBeforeFrom - ( balanceAfterFrom + receipt. gasUsed * txnGasPrice) == valueToSend)
9196 }
92-
93- // FIXME: Crashes on CI/CD
94- // FIXME: Fails on ganache
95- // func testSignPersonal() throws {
96- // let allAddresses = try ganache.eth.getAccounts()
97-
98- // let response = try ganache.personal.signPersonalMessage(message: "hello world".data(using: .utf8)!, from: allAddresses[0])
99-
100- // XCTAssert(response.toHexString() == "b686c8ddc854bd49de9eb62eb4e52af4c69a89802b40fe9a295e346b111406393c6e3f05114561ab845a47196ad22c33cec67592af9a9e42bfc067a20c7d4b6101")
101- // }
102-
103- // MARK: Ganache doesn't support a mempool for now
104- // func testTxPoolStatus() throws {
105- // let allAddresses = try ganache.eth.getAccounts()
106- //
107- // let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
108- // let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
109- //
110- // let parameters = [] as [AnyObject]
111- // let sendTx = contract.method("fallback", parameters: parameters)!
112- //
113- // let valueToSend = Web3.Utils.parseToBigUInt("1.0", units: .eth)
114- // sendTx.transactionOptions.value = valueToSend
115- // sendTx.transactionOptions.from = allAddresses[0]
116- //
117- // let _ = try sendTx.sendPromise().wait()
118- //
119- // let result = try ganache.txPool.getStatus()
120- //
121- // print(result)
122- // }
123- //
124- // func testTxPoolInspect() throws {
125- // let allAddresses = try ganache.eth.getAccounts()
126- //
127- // let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
128- // let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
129- //
130- // let parameters = [] as [AnyObject]
131- // let sendTx = contract.method("fallback", parameters: parameters)!
132- //
133- // let valueToSend = Web3.Utils.parseToBigUInt("1.0", units: .eth)
134- // sendTx.transactionOptions.value = valueToSend
135- // sendTx.transactionOptions.from = allAddresses[0]
136- //
137- // let _ = try sendTx.sendPromise().wait()
138- //
139- // let result = try ganache.txPool.getInspect()
140- //
141- // print(result)
142- // }
143- //
144- // func testTxPoolContent() throws {
145- // let allAddresses = try ganache.eth.getAccounts()
146- //
147- // let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
148- // let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
149- //
150- // let parameters = [] as [AnyObject]
151- // let sendTx = contract.method("fallback", parameters: parameters)!
152- //
153- // let valueToSend = Web3.Utils.parseToBigUInt("1.0", units: .eth)
154- // sendTx.transactionOptions.value = valueToSend
155- // sendTx.transactionOptions.from = allAddresses[0]
156- //
157- // let _ = try sendTx.sendPromise().wait()
158- //
159- // let result = try ganache.txPool.getContent()
160- //
161- // print(result)
162- // }
16397}
0 commit comments