|
| 1 | +// web3swift |
| 2 | +// |
| 3 | +// Created by Alex Vlasov. |
| 4 | +// Copyright © 2018 Alex Vlasov. All rights reserved. |
| 5 | +// |
| 6 | + |
| 7 | +import XCTest |
| 8 | + |
| 9 | +@testable import web3swift |
| 10 | + |
| 11 | +// MARK: Works only with network connection |
| 12 | +class web3swift_infura_Tests: XCTestCase { |
| 13 | + |
| 14 | + func testGetBalance() throws { |
| 15 | + do { |
| 16 | + let web3 = Web3.InfuraMainnetWeb3() |
| 17 | + let address = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! |
| 18 | + let balance = try web3.eth.getBalance(address: address) |
| 19 | + let balString = Web3.Utils.formatToEthereumUnits(balance, toUnits: .eth, decimals: 3) |
| 20 | + print(balString!) |
| 21 | + } catch { |
| 22 | + XCTFail() |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + func testGetBlockByHash() throws { |
| 27 | + do { |
| 28 | + let web3 = Web3.InfuraMainnetWeb3() |
| 29 | + let result = try web3.eth.getBlockByHash("0x6d05ba24da6b7a1af22dc6cc2a1fe42f58b2a5ea4c406b19c8cf672ed8ec0695", fullTransactions: true) |
| 30 | + print(result) |
| 31 | + } catch { |
| 32 | + XCTFail() |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + func testGetBlockByNumber1() throws { |
| 37 | + let web3 = Web3.InfuraMainnetWeb3() |
| 38 | + let result = try web3.eth.getBlockByNumber("latest", fullTransactions: true) |
| 39 | + print(result) |
| 40 | + } |
| 41 | + |
| 42 | + func testGetBlockByNumber2() throws { |
| 43 | + let web3 = Web3.InfuraMainnetWeb3() |
| 44 | + let result = try web3.eth.getBlockByNumber(UInt64(5184323), fullTransactions: true) |
| 45 | + print(result) |
| 46 | + let transactions = result.transactions |
| 47 | + for transaction in transactions { |
| 48 | + switch transaction { |
| 49 | + case .transaction(let tx): |
| 50 | + print(String(describing: tx)) |
| 51 | + default: |
| 52 | + break |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + func testGetBlockByNumber3() throws { |
| 58 | + do { |
| 59 | + let web3 = Web3.InfuraMainnetWeb3() |
| 60 | + let _ = try web3.eth.getBlockByNumber(UInt64(1000000000), fullTransactions: true) |
| 61 | + XCTFail() |
| 62 | + } catch { |
| 63 | + |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + func testGasPrice() throws { |
| 68 | + let web3 = Web3.InfuraMainnetWeb3() |
| 69 | + let response = try web3.eth.getGasPrice() |
| 70 | + print(response) |
| 71 | + } |
| 72 | + |
| 73 | + func testGetIndexedEventsPromise() throws { |
| 74 | + 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() |
| 76 | + let contract = web3.contract(jsonString, at: nil, abiVersion: 2) |
| 77 | + var filter = EventFilter() |
| 78 | + filter.fromBlock = .blockNumber(UInt64(5200120)) |
| 79 | + filter.toBlock = .blockNumber(UInt64(5200120)) |
| 80 | + filter.addresses = [EthereumAddress("0x53066cddbc0099eb6c96785d9b3df2aaeede5da3")!] |
| 81 | + filter.parameterFilters = [([EthereumAddress("0xefdcf2c36f3756ce7247628afdb632fa4ee12ec5")!] as [EventFilterable]), (nil as [EventFilterable]?)] |
| 82 | + let eventParserResult = try contract!.getIndexedEventsPromise(eventName: "Transfer", filter: filter, joinWithReceipts: true).wait() |
| 83 | + print(eventParserResult) |
| 84 | + XCTAssert(eventParserResult.count == 2) |
| 85 | + XCTAssert(eventParserResult[0].transactionReceipt != nil) |
| 86 | + XCTAssert(eventParserResult[0].eventLog != nil) |
| 87 | + } |
| 88 | + |
| 89 | + func testEventParsingBlockByNumberPromise() throws { |
| 90 | + 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() |
| 92 | + let contract = web3.contract(jsonString, at: nil, abiVersion: 2) |
| 93 | + var filter = EventFilter() |
| 94 | + filter.addresses = [EthereumAddress("0x53066cddbc0099eb6c96785d9b3df2aaeede5da3")!] |
| 95 | + filter.parameterFilters = [([EthereumAddress("0xefdcf2c36f3756ce7247628afdb632fa4ee12ec5")!] as [EventFilterable]), ([EthereumAddress("0xd5395c132c791a7f46fa8fc27f0ab6bacd824484")!] as [EventFilterable])] |
| 96 | + guard let eventParser = contract?.createEventParser("Transfer", filter: filter) else {return XCTFail()} |
| 97 | + let present = try eventParser.parseBlockByNumberPromise(UInt64(5200120)).wait() |
| 98 | + print(present) |
| 99 | + XCTAssert(present.count == 1) |
| 100 | + } |
| 101 | + |
| 102 | + func testUserCaseEventParsing() throws { |
| 103 | + let contractAddress = EthereumAddress("0x7ff546aaccd379d2d1f241e1d29cdd61d4d50778") |
| 104 | + 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() |
| 106 | + let contract = web3.contract(jsonString, at: contractAddress, abiVersion: 2) |
| 107 | + guard let eventParser = contract?.createEventParser("Deposit", filter: nil) else {return XCTFail()} |
| 108 | + let pres = try eventParser.parseBlockByNumber(UInt64(2138657)) |
| 109 | + XCTAssert(pres.count == 1) |
| 110 | + } |
| 111 | +} |
0 commit comments