Skip to content

Commit b778f1e

Browse files
Switch Eth provider from Infura to Ganache
1 parent 5731de2 commit b778f1e

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

Tests/web3swiftTests/infuraTests/web3swiftInfuraTests.swift

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,28 @@ import XCTest
88

99
@testable import web3swift
1010

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+
1126
// MARK: Works only with network connection
1227
class web3swiftInfuraTests: XCTestCase {
1328

1429
func testGetBalance() throws {
1530
do {
16-
let web3 = Web3.InfuraMainnetWeb3()
17-
let address = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
31+
let web3 = Web3.GanacheWeb3()
32+
let address = EthereumAddress("0xd61b5ca425F8C8775882d4defefC68A6979DBbce")!
1833
let balance = try web3.eth.getBalance(address: address)
1934
let balString = Web3.Utils.formatToEthereumUnits(balance, toUnits: .eth, decimals: 3)
2035
print(balString!)
@@ -25,7 +40,7 @@ class web3swiftInfuraTests: XCTestCase {
2540

2641
func testGetBlockByHash() throws {
2742
do {
28-
let web3 = Web3.InfuraMainnetWeb3()
43+
let web3 = Web3.GanacheWeb3()
2944
let result = try web3.eth.getBlockByHash("0x6d05ba24da6b7a1af22dc6cc2a1fe42f58b2a5ea4c406b19c8cf672ed8ec0695", fullTransactions: true)
3045
print(result)
3146
} catch {
@@ -34,13 +49,13 @@ class web3swiftInfuraTests: XCTestCase {
3449
}
3550

3651
func testGetBlockByNumber1() throws {
37-
let web3 = Web3.InfuraMainnetWeb3()
52+
let web3 = Web3.GanacheWeb3()
3853
let result = try web3.eth.getBlockByNumber("latest", fullTransactions: true)
3954
print(result)
4055
}
4156

4257
func testGetBlockByNumber2() throws {
43-
let web3 = Web3.InfuraMainnetWeb3()
58+
let web3 = Web3.GanacheWeb3()
4459
let result = try web3.eth.getBlockByNumber(UInt64(5184323), fullTransactions: true)
4560
print(result)
4661
let transactions = result.transactions
@@ -56,7 +71,7 @@ class web3swiftInfuraTests: XCTestCase {
5671

5772
func testGetBlockByNumber3() throws {
5873
do {
59-
let web3 = Web3.InfuraMainnetWeb3()
74+
let web3 = Web3.GanacheWeb3()
6075
let _ = try web3.eth.getBlockByNumber(UInt64(1000000000), fullTransactions: true)
6176
XCTFail()
6277
} catch {
@@ -65,14 +80,14 @@ class web3swiftInfuraTests: XCTestCase {
6580
}
6681

6782
func testGasPrice() throws {
68-
let web3 = Web3.InfuraMainnetWeb3()
83+
let web3 = Web3.GanacheWeb3()
6984
let response = try web3.eth.getGasPrice()
7085
print(response)
7186
}
7287

7388
func testGetIndexedEventsPromise() throws {
7489
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()
7691
let contract = web3.contract(jsonString, at: nil, abiVersion: 2)
7792
var filter = EventFilter()
7893
filter.fromBlock = .blockNumber(UInt64(5200120))
@@ -82,13 +97,13 @@ class web3swiftInfuraTests: XCTestCase {
8297
let eventParserResult = try contract!.getIndexedEventsPromise(eventName: "Transfer", filter: filter, joinWithReceipts: true).wait()
8398
print(eventParserResult)
8499
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)
87102
}
88103

89104
func testEventParsingBlockByNumberPromise() throws {
90105
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()
92107
let contract = web3.contract(jsonString, at: nil, abiVersion: 2)
93108
var filter = EventFilter()
94109
filter.addresses = [EthereumAddress("0x53066cddbc0099eb6c96785d9b3df2aaeede5da3")!]
@@ -102,7 +117,7 @@ class web3swiftInfuraTests: XCTestCase {
102117
func testUserCaseEventParsing() throws {
103118
let contractAddress = EthereumAddress("0x7ff546aaccd379d2d1f241e1d29cdd61d4d50778")
104119
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)
106121
let contract = web3.contract(jsonString, at: contractAddress, abiVersion: 2)
107122
guard let eventParser = contract?.createEventParser("Deposit", filter: nil) else {return XCTFail()}
108123
let pres = try eventParser.parseBlockByNumber(UInt64(2138657))

0 commit comments

Comments
 (0)