@@ -8,13 +8,28 @@ import XCTest
8
8
9
9
@testable import web3swift
10
10
11
+ public final class GanacheProvider : Web3HttpProvider {
12
+ public init ? ( _ net: Networks = Networks . Mainnet, keystoreManager manager: KeystoreManager ? = nil ) {
13
+ let providerURL = URL ( string: " http://127.0.0.1:8545/ " ) !
14
+ super. init ( providerURL, network: net, keystoreManager: manager)
15
+ }
16
+ }
17
+
18
+ extension Web3 {
19
+ /// Initialized Web3 instance bound to Infura's mainnet provider.
20
+ public static func GanacheWeb3( network: Networks = Networks . Mainnet) -> web3 {
21
+ let ganache = GanacheProvider ( network) !
22
+ return web3 ( provider: ganache)
23
+ }
24
+ }
25
+
11
26
// MARK: Works only with network connection
12
27
class web3swiftInfuraTests : XCTestCase {
13
28
14
29
func testGetBalance( ) throws {
15
30
do {
16
- let web3 = Web3 . InfuraMainnetWeb3 ( )
17
- let address = EthereumAddress ( " 0xe22b8979739D724343bd002F9f432F5990879901 " ) !
31
+ let web3 = Web3 . GanacheWeb3 ( )
32
+ let address = EthereumAddress ( " 0xd61b5ca425F8C8775882d4defefC68A6979DBbce " ) !
18
33
let balance = try web3. eth. getBalance ( address: address)
19
34
let balString = Web3 . Utils. formatToEthereumUnits ( balance, toUnits: . eth, decimals: 3 )
20
35
print ( balString!)
@@ -25,7 +40,7 @@ class web3swiftInfuraTests: XCTestCase {
25
40
26
41
func testGetBlockByHash( ) throws {
27
42
do {
28
- let web3 = Web3 . InfuraMainnetWeb3 ( )
43
+ let web3 = Web3 . GanacheWeb3 ( )
29
44
let result = try web3. eth. getBlockByHash ( " 0x6d05ba24da6b7a1af22dc6cc2a1fe42f58b2a5ea4c406b19c8cf672ed8ec0695 " , fullTransactions: true )
30
45
print ( result)
31
46
} catch {
@@ -34,13 +49,13 @@ class web3swiftInfuraTests: XCTestCase {
34
49
}
35
50
36
51
func testGetBlockByNumber1( ) throws {
37
- let web3 = Web3 . InfuraMainnetWeb3 ( )
52
+ let web3 = Web3 . GanacheWeb3 ( )
38
53
let result = try web3. eth. getBlockByNumber ( " latest " , fullTransactions: true )
39
54
print ( result)
40
55
}
41
56
42
57
func testGetBlockByNumber2( ) throws {
43
- let web3 = Web3 . InfuraMainnetWeb3 ( )
58
+ let web3 = Web3 . GanacheWeb3 ( )
44
59
let result = try web3. eth. getBlockByNumber ( UInt64 ( 5184323 ) , fullTransactions: true )
45
60
print ( result)
46
61
let transactions = result. transactions
@@ -56,7 +71,7 @@ class web3swiftInfuraTests: XCTestCase {
56
71
57
72
func testGetBlockByNumber3( ) throws {
58
73
do {
59
- let web3 = Web3 . InfuraMainnetWeb3 ( )
74
+ let web3 = Web3 . GanacheWeb3 ( )
60
75
let _ = try web3. eth. getBlockByNumber ( UInt64 ( 1000000000 ) , fullTransactions: true )
61
76
XCTFail ( )
62
77
} catch {
@@ -65,14 +80,14 @@ class web3swiftInfuraTests: XCTestCase {
65
80
}
66
81
67
82
func testGasPrice( ) throws {
68
- let web3 = Web3 . InfuraMainnetWeb3 ( )
83
+ let web3 = Web3 . GanacheWeb3 ( )
69
84
let response = try web3. eth. getGasPrice ( )
70
85
print ( response)
71
86
}
72
87
73
88
func testGetIndexedEventsPromise( ) throws {
74
89
let jsonString = " [{ \" constant \" :true, \" inputs \" :[], \" name \" : \" name \" , \" outputs \" :[{ \" name \" : \" \" , \" type \" : \" string \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :false, \" inputs \" :[{ \" name \" : \" _spender \" , \" type \" : \" address \" },{ \" name \" : \" _value \" , \" type \" : \" uint256 \" }], \" name \" : \" approve \" , \" outputs \" :[{ \" name \" : \" success \" , \" type \" : \" bool \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[], \" name \" : \" totalSupply \" , \" outputs \" :[{ \" name \" : \" \" , \" type \" : \" uint256 \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :false, \" inputs \" :[{ \" name \" : \" _from \" , \" type \" : \" address \" },{ \" name \" : \" _to \" , \" type \" : \" address \" },{ \" name \" : \" _value \" , \" type \" : \" uint256 \" }], \" name \" : \" transferFrom \" , \" outputs \" :[{ \" name \" : \" success \" , \" type \" : \" bool \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[], \" name \" : \" decimals \" , \" outputs \" :[{ \" name \" : \" \" , \" type \" : \" uint8 \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[], \" name \" : \" version \" , \" outputs \" :[{ \" name \" : \" \" , \" type \" : \" string \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[{ \" name \" : \" _owner \" , \" type \" : \" address \" }], \" name \" : \" balanceOf \" , \" outputs \" :[{ \" name \" : \" balance \" , \" type \" : \" uint256 \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[], \" name \" : \" symbol \" , \" outputs \" :[{ \" name \" : \" \" , \" type \" : \" string \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :false, \" inputs \" :[{ \" name \" : \" _to \" , \" type \" : \" address \" },{ \" name \" : \" _value \" , \" type \" : \" uint256 \" }], \" name \" : \" transfer \" , \" outputs \" :[{ \" name \" : \" success \" , \" type \" : \" bool \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :false, \" inputs \" :[{ \" name \" : \" _spender \" , \" type \" : \" address \" },{ \" name \" : \" _value \" , \" type \" : \" uint256 \" },{ \" name \" : \" _extraData \" , \" type \" : \" bytes \" }], \" name \" : \" approveAndCall \" , \" outputs \" :[{ \" name \" : \" success \" , \" type \" : \" bool \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[{ \" name \" : \" _owner \" , \" type \" : \" address \" },{ \" name \" : \" _spender \" , \" type \" : \" address \" }], \" name \" : \" allowance \" , \" outputs \" :[{ \" name \" : \" remaining \" , \" type \" : \" uint256 \" }], \" payable \" :false, \" type \" : \" function \" },{ \" inputs \" :[{ \" name \" : \" _initialAmount \" , \" type \" : \" uint256 \" },{ \" name \" : \" _tokenName \" , \" type \" : \" string \" },{ \" name \" : \" _decimalUnits \" , \" type \" : \" uint8 \" },{ \" name \" : \" _tokenSymbol \" , \" type \" : \" string \" }], \" type \" : \" constructor \" },{ \" payable \" :false, \" type \" : \" fallback \" },{ \" anonymous \" :false, \" inputs \" :[{ \" indexed \" :true, \" name \" : \" _from \" , \" type \" : \" address \" },{ \" indexed \" :true, \" name \" : \" _to \" , \" type \" : \" address \" },{ \" indexed \" :false, \" name \" : \" _value \" , \" type \" : \" uint256 \" }], \" name \" : \" Transfer \" , \" type \" : \" event \" },{ \" anonymous \" :false, \" inputs \" :[{ \" indexed \" :true, \" name \" : \" _owner \" , \" type \" : \" address \" },{ \" indexed \" :true, \" name \" : \" _spender \" , \" type \" : \" address \" },{ \" indexed \" :false, \" name \" : \" _value \" , \" type \" : \" uint256 \" }], \" name \" : \" Approval \" , \" type \" : \" event \" },] "
75
- let web3 = Web3 . InfuraMainnetWeb3 ( )
90
+ let web3 = Web3 . GanacheWeb3 ( )
76
91
let contract = web3. contract ( jsonString, at: nil , abiVersion: 2 )
77
92
var filter = EventFilter ( )
78
93
filter. fromBlock = . blockNumber( UInt64 ( 5200120 ) )
@@ -82,13 +97,13 @@ class web3swiftInfuraTests: XCTestCase {
82
97
let eventParserResult = try contract!. getIndexedEventsPromise ( eventName: " Transfer " , filter: filter, joinWithReceipts: true ) . wait ( )
83
98
print ( eventParserResult)
84
99
XCTAssert ( eventParserResult. count == 2 )
85
- XCTAssert ( eventParserResult [ 0 ] . transactionReceipt != nil )
86
- XCTAssert ( eventParserResult [ 0 ] . eventLog != nil )
100
+ XCTAssert ( eventParserResult. first ? . transactionReceipt != nil )
101
+ XCTAssert ( eventParserResult. first ? . eventLog != nil )
87
102
}
88
103
89
104
func testEventParsingBlockByNumberPromise( ) throws {
90
105
let jsonString = " [{ \" constant \" :true, \" inputs \" :[], \" name \" : \" name \" , \" outputs \" :[{ \" name \" : \" \" , \" type \" : \" string \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :false, \" inputs \" :[{ \" name \" : \" _spender \" , \" type \" : \" address \" },{ \" name \" : \" _value \" , \" type \" : \" uint256 \" }], \" name \" : \" approve \" , \" outputs \" :[{ \" name \" : \" success \" , \" type \" : \" bool \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[], \" name \" : \" totalSupply \" , \" outputs \" :[{ \" name \" : \" \" , \" type \" : \" uint256 \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :false, \" inputs \" :[{ \" name \" : \" _from \" , \" type \" : \" address \" },{ \" name \" : \" _to \" , \" type \" : \" address \" },{ \" name \" : \" _value \" , \" type \" : \" uint256 \" }], \" name \" : \" transferFrom \" , \" outputs \" :[{ \" name \" : \" success \" , \" type \" : \" bool \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[], \" name \" : \" decimals \" , \" outputs \" :[{ \" name \" : \" \" , \" type \" : \" uint8 \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[], \" name \" : \" version \" , \" outputs \" :[{ \" name \" : \" \" , \" type \" : \" string \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[{ \" name \" : \" _owner \" , \" type \" : \" address \" }], \" name \" : \" balanceOf \" , \" outputs \" :[{ \" name \" : \" balance \" , \" type \" : \" uint256 \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[], \" name \" : \" symbol \" , \" outputs \" :[{ \" name \" : \" \" , \" type \" : \" string \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :false, \" inputs \" :[{ \" name \" : \" _to \" , \" type \" : \" address \" },{ \" name \" : \" _value \" , \" type \" : \" uint256 \" }], \" name \" : \" transfer \" , \" outputs \" :[{ \" name \" : \" success \" , \" type \" : \" bool \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :false, \" inputs \" :[{ \" name \" : \" _spender \" , \" type \" : \" address \" },{ \" name \" : \" _value \" , \" type \" : \" uint256 \" },{ \" name \" : \" _extraData \" , \" type \" : \" bytes \" }], \" name \" : \" approveAndCall \" , \" outputs \" :[{ \" name \" : \" success \" , \" type \" : \" bool \" }], \" payable \" :false, \" type \" : \" function \" },{ \" constant \" :true, \" inputs \" :[{ \" name \" : \" _owner \" , \" type \" : \" address \" },{ \" name \" : \" _spender \" , \" type \" : \" address \" }], \" name \" : \" allowance \" , \" outputs \" :[{ \" name \" : \" remaining \" , \" type \" : \" uint256 \" }], \" payable \" :false, \" type \" : \" function \" },{ \" inputs \" :[{ \" name \" : \" _initialAmount \" , \" type \" : \" uint256 \" },{ \" name \" : \" _tokenName \" , \" type \" : \" string \" },{ \" name \" : \" _decimalUnits \" , \" type \" : \" uint8 \" },{ \" name \" : \" _tokenSymbol \" , \" type \" : \" string \" }], \" type \" : \" constructor \" },{ \" payable \" :false, \" type \" : \" fallback \" },{ \" anonymous \" :false, \" inputs \" :[{ \" indexed \" :true, \" name \" : \" _from \" , \" type \" : \" address \" },{ \" indexed \" :true, \" name \" : \" _to \" , \" type \" : \" address \" },{ \" indexed \" :false, \" name \" : \" _value \" , \" type \" : \" uint256 \" }], \" name \" : \" Transfer \" , \" type \" : \" event \" },{ \" anonymous \" :false, \" inputs \" :[{ \" indexed \" :true, \" name \" : \" _owner \" , \" type \" : \" address \" },{ \" indexed \" :true, \" name \" : \" _spender \" , \" type \" : \" address \" },{ \" indexed \" :false, \" name \" : \" _value \" , \" type \" : \" uint256 \" }], \" name \" : \" Approval \" , \" type \" : \" event \" },] "
91
- let web3 = Web3 . InfuraMainnetWeb3 ( )
106
+ let web3 = Web3 . GanacheWeb3 ( )
92
107
let contract = web3. contract ( jsonString, at: nil , abiVersion: 2 )
93
108
var filter = EventFilter ( )
94
109
filter. addresses = [ EthereumAddress ( " 0x53066cddbc0099eb6c96785d9b3df2aaeede5da3 " ) !]
@@ -102,7 +117,7 @@ class web3swiftInfuraTests: XCTestCase {
102
117
func testUserCaseEventParsing( ) throws {
103
118
let contractAddress = EthereumAddress ( " 0x7ff546aaccd379d2d1f241e1d29cdd61d4d50778 " )
104
119
let jsonString = " [{ \" constant \" :false, \" inputs \" :[{ \" name \" : \" _id \" , \" type \" : \" string \" }], \" name \" : \" deposit \" , \" outputs \" :[], \" payable \" :true, \" stateMutability \" : \" payable \" , \" type \" : \" function \" },{ \" anonymous \" :false, \" inputs \" :[{ \" indexed \" :true, \" name \" : \" _from \" , \" type \" : \" address \" },{ \" indexed \" :false, \" name \" : \" _id \" , \" type \" : \" string \" },{ \" indexed \" :true, \" name \" : \" _value \" , \" type \" : \" uint256 \" }], \" name \" : \" Deposit \" , \" type \" : \" event \" }] "
105
- let web3 = Web3 . InfuraRinkebyWeb3 ( )
120
+ let web3 = Web3 . GanacheWeb3 ( network : . Rinkeby )
106
121
let contract = web3. contract ( jsonString, at: contractAddress, abiVersion: 2 )
107
122
guard let eventParser = contract? . createEventParser ( " Deposit " , filter: nil ) else { return XCTFail ( ) }
108
123
let pres = try eventParser. parseBlockByNumber ( UInt64 ( 2138657 ) )
0 commit comments