@@ -12,91 +12,83 @@ import BigInt
12
12
13
13
@testable import web3swift
14
14
15
- class web3swiftBasicLocalNodeTests : XCTestCase {
16
-
17
- // this is needed for any test suites that depend on the block-chain state from Ganache
18
- override class func setUp( ) {
19
- super. setUp ( )
20
- preloadGanache ( )
21
- }
15
+ class web3swiftBasicLocalNodeTests : LocalTestCase {
22
16
23
17
func testDeployWithRemoteSigning( ) throws {
24
- let web3 = try Web3 . new ( URL . init ( string: " http://127.0.0.1:8545 " ) !)
25
- let allAddresses = try web3. eth. getAccounts ( )
26
-
18
+ let allAddresses = try ganache. eth. getAccounts ( )
19
+
27
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 \" }] "
28
21
let bytecode = Data . fromHex ( " 6060604052341561000f57600080fd5b6103358061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063a16e94bf14610051578063a46b5b6b146100df575b600080fd5b341561005c57600080fd5b61006461013c565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a4578082015181840152602081019050610089565b50505050905090810190601f1680156100d15780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156100ea57600080fd5b61013a600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190505061020d565b005b610144610250565b6000808073ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102035780601f106101d857610100808354040283529160200191610203565b820191906000526020600020905b8154815290600101906020018083116101e657829003601f168201915b5050505050905090565b806000808073ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600001908051906020019061024c929190610264565b5050565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102a557805160ff19168380011785556102d3565b828001600101855582156102d3579182015b828111156102d25782518255916020019190600101906102b7565b5b5090506102e091906102e4565b5090565b61030691905b808211156103025760008160009055506001016102ea565b5090565b905600a165627a7a7230582017359d063cd7fdf56f19ca186a54863ce855c8f070acece905d8538fbbc4d1bf0029 " ) !
29
-
30
- let contract = web3 . contract ( abiString, at: nil , abiVersion: 2 ) !
31
-
22
+
23
+ let contract = ganache . contract ( abiString, at: nil , abiVersion: 2 ) !
24
+
32
25
let parameters = [ ] as [ AnyObject ]
33
26
let deployTx = contract. deploy ( bytecode: bytecode, parameters: parameters) !
34
27
deployTx. transactionOptions. from = allAddresses [ 0 ]
35
28
deployTx. transactionOptions. gasLimit = . manual( 3000000 )
36
-
29
+
37
30
let result = try deployTx. sendPromise ( ) . wait ( )
38
31
let txHash = result. hash
39
32
print ( " Transaction with hash " + txHash)
40
-
33
+
41
34
Thread . sleep ( forTimeInterval: 1.0 )
42
-
43
- let receipt = try web3 . eth. getTransactionReceipt ( txHash)
35
+
36
+ let receipt = try ganache . eth. getTransactionReceipt ( txHash)
44
37
print ( receipt)
45
-
38
+
46
39
switch receipt. status {
47
40
case . notYetProcessed:
48
41
return
49
42
default :
50
43
break
51
44
}
52
-
53
- let details = try web3 . eth. getTransactionDetails ( txHash)
45
+
46
+ let details = try ganache . eth. getTransactionDetails ( txHash)
54
47
print ( details)
55
48
}
56
49
57
50
func testEthSendExampleWithRemoteSigning( ) throws {
58
- let web3 = try Web3 . new ( URL . init ( string: " http://127.0.0.1:8545 " ) !)
59
- let allAddresses = try web3. eth. getAccounts ( )
51
+ let allAddresses = try ganache. eth. getAccounts ( )
60
52
let sendToAddress = EthereumAddress ( " 0xe22b8979739D724343bd002F9f432F5990879901 " ) !
61
- let contract = web3 . contract ( Web3 . Utils. coldWalletABI, at: sendToAddress, abiVersion: 2 ) !
62
-
53
+ let contract = ganache . contract ( Web3 . Utils. coldWalletABI, at: sendToAddress, abiVersion: 2 ) !
54
+
63
55
let parameters = [ ] as [ AnyObject ]
64
56
let sendTx = contract. method ( " fallback " , parameters: parameters) !
65
-
57
+
66
58
let valueToSend = Web3 . Utils. parseToBigUInt ( " 1.0 " , units: . eth)
67
59
sendTx. transactionOptions. value = valueToSend
68
60
sendTx. transactionOptions. from = allAddresses [ 0 ]
69
-
70
- let balanceBeforeTo = try web3 . eth. getBalancePromise ( address: sendToAddress) . wait ( )
71
- let balanceBeforeFrom = try web3 . eth. getBalancePromise ( address: allAddresses [ 0 ] ) . wait ( )
61
+
62
+ let balanceBeforeTo = try ganache . eth. getBalancePromise ( address: sendToAddress) . wait ( )
63
+ let balanceBeforeFrom = try ganache . eth. getBalancePromise ( address: allAddresses [ 0 ] ) . wait ( )
72
64
print ( " Balance before to: " + balanceBeforeTo. description)
73
65
print ( " Balance before from: " + balanceBeforeFrom. description)
74
-
66
+
75
67
let result = try sendTx. sendPromise ( ) . wait ( )
76
68
let txHash = result. hash
77
69
print ( " Transaction with hash " + txHash)
78
-
70
+
79
71
Thread . sleep ( forTimeInterval: 1.0 )
80
-
81
- let receipt = try web3 . eth. getTransactionReceipt ( txHash)
72
+
73
+ let receipt = try ganache . eth. getTransactionReceipt ( txHash)
82
74
print ( receipt)
83
-
75
+
84
76
switch receipt. status {
85
77
case . notYetProcessed:
86
78
return
87
79
default :
88
80
break
89
81
}
90
-
91
- let details = try web3 . eth. getTransactionDetails ( txHash)
82
+
83
+ let details = try ganache . eth. getTransactionDetails ( txHash)
92
84
print ( details)
93
-
94
-
95
- let balanceAfterTo = try web3 . eth. getBalancePromise ( address: sendToAddress) . wait ( )
96
- let balanceAfterFrom = try web3 . eth. getBalancePromise ( address: allAddresses [ 0 ] ) . wait ( )
85
+
86
+
87
+ let balanceAfterTo = try ganache . eth. getBalancePromise ( address: sendToAddress) . wait ( )
88
+ let balanceAfterFrom = try ganache . eth. getBalancePromise ( address: allAddresses [ 0 ] ) . wait ( )
97
89
print ( " Balance after to: " + balanceAfterTo. description)
98
90
print ( " Balance after from: " + balanceAfterFrom. description)
99
-
91
+
100
92
XCTAssert ( balanceAfterTo - balanceBeforeTo == valueToSend)
101
93
let txnGasPrice = details. transaction. parameters. gasPrice ?? 0
102
94
XCTAssert ( balanceBeforeFrom - ( balanceAfterFrom + receipt. gasUsed * txnGasPrice) == valueToSend)
@@ -105,21 +97,19 @@ class web3swiftBasicLocalNodeTests: XCTestCase {
105
97
// FIXME: Crashes on CI/CD
106
98
// FIXME: Fails on ganache
107
99
// func testSignPersonal() throws {
108
- // let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
109
- // let allAddresses = try web3.eth.getAccounts()
110
-
111
- // let response = try web3.personal.signPersonalMessage(message: "hello world".data(using: .utf8)!, from: allAddresses[0])
112
-
100
+ // let allAddresses = try ganache.eth.getAccounts()
101
+
102
+ // let response = try ganache.personal.signPersonalMessage(message: "hello world".data(using: .utf8)!, from: allAddresses[0])
103
+
113
104
// XCTAssert(response.toHexString() == "b686c8ddc854bd49de9eb62eb4e52af4c69a89802b40fe9a295e346b111406393c6e3f05114561ab845a47196ad22c33cec67592af9a9e42bfc067a20c7d4b6101")
114
105
// }
115
-
106
+
116
107
// MARK: Ganache doesn't support a mempool for now
117
108
// func testTxPoolStatus() throws {
118
- // let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
119
- // let allAddresses = try web3.eth.getAccounts()
109
+ // let allAddresses = try ganache.eth.getAccounts()
120
110
//
121
111
// let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
122
- // let contract = web3 .contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
112
+ // let contract = ganache .contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
123
113
//
124
114
// let parameters = [] as [AnyObject]
125
115
// let sendTx = contract.method("fallback", parameters: parameters)!
@@ -130,17 +120,16 @@ class web3swiftBasicLocalNodeTests: XCTestCase {
130
120
//
131
121
// let _ = try sendTx.sendPromise().wait()
132
122
//
133
- // let result = try web3 .txPool.getStatus()
123
+ // let result = try ganache .txPool.getStatus()
134
124
//
135
125
// print(result)
136
126
// }
137
127
//
138
128
// func testTxPoolInspect() throws {
139
- // let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
140
- // let allAddresses = try web3.eth.getAccounts()
129
+ // let allAddresses = try ganache.eth.getAccounts()
141
130
//
142
131
// let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
143
- // let contract = web3 .contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
132
+ // let contract = ganache .contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
144
133
//
145
134
// let parameters = [] as [AnyObject]
146
135
// let sendTx = contract.method("fallback", parameters: parameters)!
@@ -151,17 +140,16 @@ class web3swiftBasicLocalNodeTests: XCTestCase {
151
140
//
152
141
// let _ = try sendTx.sendPromise().wait()
153
142
//
154
- // let result = try web3 .txPool.getInspect()
143
+ // let result = try ganache .txPool.getInspect()
155
144
//
156
145
// print(result)
157
146
// }
158
147
//
159
148
// func testTxPoolContent() throws {
160
- // let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
161
- // let allAddresses = try web3.eth.getAccounts()
149
+ // let allAddresses = try ganache.eth.getAccounts()
162
150
//
163
151
// let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
164
- // let contract = web3 .contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
152
+ // let contract = ganache .contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
165
153
//
166
154
// let parameters = [] as [AnyObject]
167
155
// let sendTx = contract.method("fallback", parameters: parameters)!
@@ -172,7 +160,7 @@ class web3swiftBasicLocalNodeTests: XCTestCase {
172
160
//
173
161
// let _ = try sendTx.sendPromise().wait()
174
162
//
175
- // let result = try web3 .txPool.getContent()
163
+ // let result = try ganache .txPool.getContent()
176
164
//
177
165
// print(result)
178
166
// }
0 commit comments