@@ -8,27 +8,12 @@ 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
-
26
11
// MARK: Works only with network connection
27
- class web3swiftGanacheTests : XCTestCase {
12
+ class web3swiftInfuraTests : XCTestCase {
28
13
29
14
func testGetBalance( ) throws {
30
15
do {
31
- let web3 = Web3 . GanacheWeb3 ( )
16
+ let web3 = Web3 . InfuraMainnetWeb3 ( accessToken : Constants . infuraToken )
32
17
let address = EthereumAddress ( " 0xd61b5ca425F8C8775882d4defefC68A6979DBbce " ) !
33
18
let balance = try web3. eth. getBalance ( address: address)
34
19
let balString = Web3 . Utils. formatToEthereumUnits ( balance, toUnits: . eth, decimals: 3 )
@@ -39,23 +24,20 @@ class web3swiftGanacheTests: XCTestCase {
39
24
}
40
25
41
26
func testGetBlockByHash( ) throws {
42
- do {
43
- let web3 = Web3 . GanacheWeb3 ( )
44
- let result = try web3. eth. getBlockByHash ( " 0x6d05ba24da6b7a1af22dc6cc2a1fe42f58b2a5ea4c406b19c8cf672ed8ec0695 " , fullTransactions: true )
45
- print ( result)
46
- } catch {
47
- XCTFail ( )
48
- }
27
+ let web3 = Web3 . InfuraMainnetWeb3 ( accessToken: Constants . infuraToken)
28
+ let result = try web3. eth. getBlockByHash ( " 0x6d05ba24da6b7a1af22dc6cc2a1fe42f58b2a5ea4c406b19c8cf672ed8ec0695 " , fullTransactions: false )
29
+
30
+ print ( result)
49
31
}
50
32
51
33
func testGetBlockByNumber1( ) throws {
52
- let web3 = Web3 . GanacheWeb3 ( )
53
- let result = try web3. eth. getBlockByNumber ( " latest " , fullTransactions: true )
34
+ let web3 = Web3 . InfuraMainnetWeb3 ( accessToken : Constants . infuraToken )
35
+ let result = try web3. eth. getBlockByNumber ( " latest " , fullTransactions: false )
54
36
print ( result)
55
37
}
56
38
57
39
func testGetBlockByNumber2( ) throws {
58
- let web3 = Web3 . GanacheWeb3 ( )
40
+ let web3 = Web3 . InfuraMainnetWeb3 ( accessToken : Constants . infuraToken )
59
41
let result = try web3. eth. getBlockByNumber ( UInt64 ( 5184323 ) , fullTransactions: true )
60
42
print ( result)
61
43
let transactions = result. transactions
@@ -71,7 +53,7 @@ class web3swiftGanacheTests: XCTestCase {
71
53
72
54
func testGetBlockByNumber3( ) throws {
73
55
do {
74
- let web3 = Web3 . GanacheWeb3 ( )
56
+ let web3 = Web3 . InfuraMainnetWeb3 ( accessToken : Constants . infuraToken )
75
57
let _ = try web3. eth. getBlockByNumber ( UInt64 ( 1000000000 ) , fullTransactions: true )
76
58
XCTFail ( )
77
59
} catch {
@@ -80,14 +62,14 @@ class web3swiftGanacheTests: XCTestCase {
80
62
}
81
63
82
64
func testGasPrice( ) throws {
83
- let web3 = Web3 . GanacheWeb3 ( )
65
+ let web3 = Web3 . InfuraMainnetWeb3 ( accessToken : Constants . infuraToken )
84
66
let response = try web3. eth. getGasPrice ( )
85
67
print ( response)
86
68
}
87
69
88
70
func testGetIndexedEventsPromise( ) throws {
89
71
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 \" },] "
90
- let web3 = Web3 . GanacheWeb3 ( )
72
+ let web3 = Web3 . InfuraMainnetWeb3 ( accessToken : Constants . infuraToken )
91
73
let contract = web3. contract ( jsonString, at: nil , abiVersion: 2 )
92
74
var filter = EventFilter ( )
93
75
filter. fromBlock = . blockNumber( UInt64 ( 5200120 ) )
@@ -103,7 +85,7 @@ class web3swiftGanacheTests: XCTestCase {
103
85
104
86
func testEventParsingBlockByNumberPromise( ) throws {
105
87
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 \" },] "
106
- let web3 = Web3 . GanacheWeb3 ( )
88
+ let web3 = Web3 . InfuraMainnetWeb3 ( accessToken : Constants . infuraToken )
107
89
let contract = web3. contract ( jsonString, at: nil , abiVersion: 2 )
108
90
var filter = EventFilter ( )
109
91
filter. addresses = [ EthereumAddress ( " 0x53066cddbc0099eb6c96785d9b3df2aaeede5da3 " ) !]
@@ -117,7 +99,7 @@ class web3swiftGanacheTests: XCTestCase {
117
99
func testUserCaseEventParsing( ) throws {
118
100
let contractAddress = EthereumAddress ( " 0x7ff546aaccd379d2d1f241e1d29cdd61d4d50778 " )
119
101
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 \" }] "
120
- let web3 = Web3 . GanacheWeb3 ( network : . Rinkeby )
102
+ let web3 = Web3 . InfuraRinkebyWeb3 ( accessToken : Constants . infuraToken )
121
103
let contract = web3. contract ( jsonString, at: contractAddress, abiVersion: 2 )
122
104
guard let eventParser = contract? . createEventParser ( " Deposit " , filter: nil ) else { return XCTFail ( ) }
123
105
let pres = try eventParser. parseBlockByNumber ( UInt64 ( 2138657 ) )
0 commit comments