3
3
// Created by Alex Vlasov.
4
4
// Copyright © 2018 Alex Vlasov. All rights reserved.
5
5
//
6
-
6
+ // TODO: Replace `XCTAssert` with more explicite `XCTAssertEqual`, where Applicable
7
7
import XCTest
8
8
import CryptoSwift
9
9
import BigInt
10
+ import Core
10
11
11
12
@testable import web3swift
12
13
13
- class BasicLocalNodeTests : LocalTestCase {
14
+ class BasicLocalNodeTests : XCTestCase {
14
15
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 ( )
17
19
18
20
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 \" }] "
19
21
let bytecode = Data . fromHex ( " 6060604052341561000f57600080fd5b6103358061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063a16e94bf14610051578063a46b5b6b146100df575b600080fd5b341561005c57600080fd5b61006461013c565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a4578082015181840152602081019050610089565b50505050905090810190601f1680156100d15780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156100ea57600080fd5b61013a600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190505061020d565b005b610144610250565b6000808073ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102035780601f106101d857610100808354040283529160200191610203565b820191906000526020600020905b8154815290600101906020018083116101e657829003601f168201915b5050505050905090565b806000808073ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600001908051906020019061024c929190610264565b5050565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102a557805160ff19168380011785556102d3565b828001600101855582156102d3579182015b828111156102d25782518255916020019190600101906102b7565b5b5090506102e091906102e4565b5090565b61030691905b808211156103025760008160009055506001016102ea565b5090565b905600a165627a7a7230582017359d063cd7fdf56f19ca186a54863ce855c8f070acece905d8538fbbc4d1bf0029 " ) !
20
22
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) !
23
27
deployTx. transactionOptions. from = allAddresses [ 0 ]
24
28
deployTx. transactionOptions. gasLimit = . manual( 3000000 )
25
29
26
- let result = try deployTx. sendPromise ( ) . wait ( )
30
+ let result = try await deployTx. send ( )
27
31
let txHash = result. hash
28
32
print ( " Transaction with hash " + txHash)
29
33
30
34
Thread . sleep ( forTimeInterval: 1.0 )
31
35
32
- let receipt = try ganache . eth. getTransactionReceipt ( txHash)
36
+ let receipt = try await web3 . eth. transactionReceipt ( txHash)
33
37
print ( receipt)
34
38
35
39
switch receipt. status {
@@ -39,34 +43,35 @@ class BasicLocalNodeTests: LocalTestCase {
39
43
break
40
44
}
41
45
42
- let details = try ganache . eth. getTransactionDetails ( txHash)
46
+ let details = try await web3 . eth. transactionDetails ( txHash)
43
47
print ( details)
44
48
}
45
49
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 ( )
48
53
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 ) !
50
55
51
56
let parameters = [ ] as [ AnyObject ]
52
57
let sendTx = contract. method ( " fallback " , parameters: parameters) !
53
58
54
- let valueToSend = Web3 . Utils . parseToBigUInt ( " 1.0 " , units: . eth) !
59
+ let valueToSend = Utilities . parseToBigUInt ( " 1.0 " , units: . eth)
55
60
sendTx. transactionOptions. value = valueToSend
56
61
sendTx. transactionOptions. from = allAddresses [ 0 ]
57
62
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 ] )
60
65
print ( " Balance before to: " + balanceBeforeTo. description)
61
66
print ( " Balance before from: " + balanceBeforeFrom. description)
62
67
63
- let result = try sendTx. sendPromise ( ) . wait ( )
68
+ let result = try await sendTx. send ( )
64
69
let txHash = result. hash
65
70
print ( " Transaction with hash " + txHash)
66
71
67
72
Thread . sleep ( forTimeInterval: 1.0 )
68
73
69
- let receipt = try ganache . eth. getTransactionReceipt ( txHash)
74
+ let receipt = try await web3 . eth. transactionReceipt ( txHash)
70
75
print ( receipt)
71
76
72
77
switch receipt. status {
@@ -76,88 +81,17 @@ class BasicLocalNodeTests: LocalTestCase {
76
81
break
77
82
}
78
83
79
- let details = try ganache . eth. getTransactionDetails ( txHash)
84
+ let details = try await web3 . eth. transactionDetails ( txHash)
80
85
print ( details)
81
86
82
87
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 ] )
85
90
print ( " Balance after to: " + balanceAfterTo. description)
86
91
print ( " Balance after from: " + balanceAfterFrom. description)
87
92
88
- XCTAssertEqual ( balanceAfterTo - balanceBeforeTo, valueToSend)
93
+ XCTAssert ( balanceAfterTo - balanceBeforeTo == valueToSend)
89
94
let txnGasPrice = details. transaction. parameters. gasPrice ?? 0
90
- XCTAssertEqual ( balanceBeforeFrom - ( balanceAfterFrom + receipt. gasUsed * txnGasPrice) , valueToSend)
95
+ XCTAssert ( balanceBeforeFrom - ( balanceAfterFrom + receipt. gasUsed * txnGasPrice) == valueToSend)
91
96
}
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
- // }
163
97
}
0 commit comments