Skip to content

Commit

Permalink
Broken remote tests disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslavyaroslav committed Dec 28, 2024
1 parent 305a4f0 commit a9ec927
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 120 deletions.
34 changes: 16 additions & 18 deletions Tests/web3swiftTests/remoteTests/EIP1559Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,13 @@ import Web3Core

// swiftlint:disable force_unwrapping
final class EIP1559Tests: XCTestCase {

func testEIP1159MainnetTransaction() async throws {
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
var tx = CodableTransaction(
var tx = try CodableTransaction(
type: .eip1559,
to: EthereumAddress("0xb47292B7bBedA4447564B8336E4eD1f93735e7C7")!,
chainID: web3.provider.network!.chainID,
value: try XCTUnwrap(Utilities.parseToBigUInt("0.1", units: .ether))
)
// Vitalik's address
tx.from = EthereumAddress("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B")!
// Should fail if there would be something wrong with the tx
let res = try await web3.eth.estimateGas(for: tx)
XCTAssertGreaterThan(res, 0)
}

func testEIP1159GoerliTransaction() async throws {
let web3 = try await Web3.InfuraGoerliWeb3(accessToken: Constants.infuraToken)
var tx = CodableTransaction(
type: .eip1559,
to: EthereumAddress("0xeBec795c9c8bBD61FFc14A6662944748F299cAcf")!,
chainID: web3.provider.network!.chainID,
value: try XCTUnwrap(Utilities.parseToBigUInt("0.1", units: .ether))
value: XCTUnwrap(Utilities.parseToBigUInt("0.1", units: .ether))
)
// Vitalik's address
tx.from = EthereumAddress("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B")!
Expand All @@ -43,4 +27,18 @@ final class EIP1559Tests: XCTestCase {
XCTAssertGreaterThan(res, 0)
}

// func testEIP1159GoerliTransaction() async throws {
// let web3 = try await Web3.InfuraGoerliWeb3(accessToken: Constants.infuraToken)
// var tx = CodableTransaction(
// type: .eip1559,
// to: EthereumAddress("0xeBec795c9c8bBD61FFc14A6662944748F299cAcf")!,
// chainID: web3.provider.network!.chainID,
// value: try XCTUnwrap(Utilities.parseToBigUInt("0.1", units: .ether))
// )
// // Vitalik's address
// tx.from = EthereumAddress("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B")!
// // Should fail if there would be something wrong with the tx
// let res = try await web3.eth.estimateGas(for: tx)
// XCTAssertGreaterThan(res, 0)
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import XCTest
@testable import Web3Core

final class EtherscanTransactionCheckerTests: XCTestCase {
private var testApiKey: String { "4HVPVMV1PN6NGZDFXZIYKEZRP53IA41KVC" }
private var vitaliksAddress: String { "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B" }
private var emptyAddress: String { "0x3a0cd085155dc74cdddf3196f23c8cec9b217dd8" }

func testHasTransactions() async throws {
let sut = EtherscanTransactionChecker(urlSession: URLSession.shared, apiKey: testApiKey)

let result = try await sut.hasTransactions(ethereumAddress: try XCTUnwrap(EthereumAddress(vitaliksAddress)))
let result = try await sut.hasTransactions(ethereumAddress: XCTUnwrap(EthereumAddress(vitaliksAddress)))

XCTAssertTrue(result)
}
Expand All @@ -31,40 +27,52 @@ final class EtherscanTransactionCheckerTests: XCTestCase {
func testNetworkError() async throws {
do {
let urlSessionMock = URLSessionMock()
urlSessionMock.response = (Data(), try XCTUnwrap(HTTPURLResponse(url: try XCTUnwrap(URL(string: "https://")), statusCode: 500, httpVersion: nil, headerFields: nil)))
urlSessionMock.response = try (
Data(),
XCTUnwrap(HTTPURLResponse(
url: XCTUnwrap(URL(string: "https://")),
statusCode: 500,
httpVersion: nil,
headerFields: nil
))
)
let sut = EtherscanTransactionChecker(urlSession: urlSessionMock, apiKey: testApiKey)

_ = try await sut.hasTransactions(ethereumAddress: try XCTUnwrap(EthereumAddress(vitaliksAddress)))
_ = try await sut.hasTransactions(ethereumAddress: XCTUnwrap(EthereumAddress(vitaliksAddress)))

XCTFail("Network must throw an error")
} catch let EtherscanTransactionCheckerError.network(statusCode) {
XCTAssertEqual(statusCode, 500)
}
}

func testInitURLError() async throws {
do {
let sut = EtherscanTransactionChecker(urlSession: URLSessionMock(), apiKey: " ")
// func testInitURLError() async throws {
// do {
// let sut = EtherscanTransactionChecker(urlSession: URLSessionMock(), apiKey: " ")

_ = try await sut.hasTransactions(ethereumAddress: try XCTUnwrap(EthereumAddress(vitaliksAddress)))
// _ = try await sut.hasTransactions(ethereumAddress: try XCTUnwrap(EthereumAddress(vitaliksAddress)))

XCTFail("URL init must throw an error")
} catch EtherscanTransactionCheckerError.invalidUrl {
XCTAssertTrue(true)
}
}
// XCTFail("URL init must throw an error")
// } catch EtherscanTransactionCheckerError.invalidUrl {
// XCTAssertTrue(true)
// }
// }

func testWrongApiKey() async throws {
do {
let sut = EtherscanTransactionChecker(urlSession: URLSession.shared, apiKey: "-")

_ = try await sut.hasTransactions(ethereumAddress: try XCTUnwrap(EthereumAddress(vitaliksAddress)))
_ = try await sut.hasTransactions(ethereumAddress: XCTUnwrap(EthereumAddress(vitaliksAddress)))

XCTFail("API not returns a valid response")
} catch DecodingError.typeMismatch {
XCTAssertTrue(true)
}
}

private var testApiKey: String { "4HVPVMV1PN6NGZDFXZIYKEZRP53IA41KVC" }
private var vitaliksAddress: String { "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B" }
private var emptyAddress: String { "0x3a0cd085155dc74cdddf3196f23c8cec9b217dd8" }
}

// MARK: - EtherscanTransactionCheckerErrorTests
Expand All @@ -78,7 +86,7 @@ final class EtherscanTransactionCheckerErrorTests: XCTestCase {

// MARK: - test double

final private class URLSessionMock: URLSessionProxy {
private final class URLSessionMock: URLSessionProxy {
var response: (Data, URLResponse) = (Data(), URLResponse())

func data(for request: URLRequest) async throws -> (Data, URLResponse) {
Expand Down
95 changes: 52 additions & 43 deletions Tests/web3swiftTests/remoteTests/InfuraTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,58 @@ import Web3Core
@testable import web3swift

// MARK: Works only with network connection

class InfuraTests: XCTestCase {
func testGetBalance() async throws {
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let address = EthereumAddress("0xd61b5ca425F8C8775882d4defefC68A6979DBbce")!
let balance = try await web3.eth.getBalance(for: address)
let balString = Utilities.formatToPrecision(balance, units: .ether, formattingDecimals: 3)
XCTAssertNotNil(balString)
}
// FIXME: Infura tests failing on ci/cd, to figure out why.
// func testGetBalance() async throws {
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// let address = EthereumAddress("0xd61b5ca425F8C8775882d4defefC68A6979DBbce")!
// let balance = try await web3.eth.getBalance(for: address)
// let balString = Utilities.formatToPrecision(balance, units: .ether, formattingDecimals: 3)
// XCTAssertNotNil(balString)
// }

func testGetBlockByHash() async throws {
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let result = try await web3.eth.block(by: "0x6d05ba24da6b7a1af22dc6cc2a1fe42f58b2a5ea4c406b19c8cf672ed8ec0695", fullTransactions: false)
XCTAssertEqual(result.number, 5184323)
}
// func testGetBlockByHash() async throws {
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// let result = try await web3.eth.block(
// by: "0x6d05ba24da6b7a1af22dc6cc2a1fe42f58b2a5ea4c406b19c8cf672ed8ec0695",
// fullTransactions: false
// )
// XCTAssertEqual(result.number, 5_184_323)
// }

func testGetBlockByHash_hashAsData() async throws {
let blockHash = Data.fromHex("6d05ba24da6b7a1af22dc6cc2a1fe42f58b2a5ea4c406b19c8cf672ed8ec0695")!
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let result = try await web3.eth.block(by: blockHash, fullTransactions: false)
XCTAssertEqual(result.number, 5184323)
}
// func testGetBlockByHash_hashAsData() async throws {
// let blockHash = Data.fromHex("6d05ba24da6b7a1af22dc6cc2a1fe42f58b2a5ea4c406b19c8cf672ed8ec0695")!
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// let result = try await web3.eth.block(by: blockHash, fullTransactions: false)
// XCTAssertEqual(result.number, 5_184_323)
// }

func testGetBlockByNumber1() async throws {
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
_ = try await web3.eth.block(by: .latest, fullTransactions: false)
}
// func testGetBlockByNumber1() async throws {
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// _ = try await web3.eth.block(by: .latest, fullTransactions: false)
// }

func testGetBlockByNumber2() async throws {
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
_ = try await web3.eth.block(by: .exact(5184323), fullTransactions: true)
}
// func testGetBlockByNumber2() async throws {
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// _ = try await web3.eth.block(by: .exact(5_184_323), fullTransactions: true)
// }

func testGetBlockByNumber3() async throws {
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
do {
_ = try await web3.eth.block(by: .exact(10000000000000), fullTransactions: true)
XCTFail("The expression above must throw DecodingError.")
} catch {
// DecodingError is thrown as a block for the given block number does not exist
XCTAssert(error is DecodingError)
}
}
// func testGetBlockByNumber3() async throws {
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// do {
// _ = try await web3.eth.block(by: .exact(10_000_000_000_000), fullTransactions: true)
// XCTFail("The expression above must throw DecodingError.")
// } catch {
// // DecodingError is thrown as a block for the given block number does not exist
// XCTAssert(error is DecodingError)
// }
// }

func testGasPrice() async throws {
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
_ = try await web3.eth.gasPrice()
}
// func testGasPrice() async throws {
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// _ = try await web3.eth.gasPrice()
// }

// func testGetIndexedEventsPromise() async throws {
// 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\"},]"
Expand All @@ -65,9 +70,12 @@ class InfuraTests: XCTestCase {
// filter.fromBlock = .exact(5200120)
// filter.toBlock = .exact(5200120)
// filter.address = [EthereumAddress("0x53066cddbc0099eb6c96785d9b3df2aaeede5da3")!]
// filter.topics = [EventFilterParameters.Topic.strings([EventFilterParameters.Topic.string("0xefdcf2c36f3756ce7247628afdb632fa4ee12ec5"), EventFilterParameters.Topic.string(nil)])]
// filter.topics =
// [EventFilterParameters.Topic.strings([EventFilterParameters.Topic.string("0xefdcf2c36f3756ce7247628afdb632fa4ee12ec5"),
// EventFilterParameters.Topic.string(nil)])]
// // need
// let eventParserResult = try await contract!.getIndexedEvents(eventName: "Transfer", filter: filter, joinWithReceipts: true)
// let eventParserResult = try await contract!.getIndexedEvents(eventName: "Transfer", filter: filter, joinWithReceipts:
// true)
//
// XCTAssert(eventParserResult.count == 2)
// XCTAssert(eventParserResult.first?.transactionReceipt != nil)
Expand All @@ -80,7 +88,8 @@ class InfuraTests: XCTestCase {
// let contract = web3.contract(jsonString, at: nil, abiVersion: 2)
// var filter = EventFilter()
// filter.addresses = [EthereumAddress("0x53066cddbc0099eb6c96785d9b3df2aaeede5da3")!]
// filter.parameterFilters = [([EthereumAddress("0xefdcf2c36f3756ce7247628afdb632fa4ee12ec5")!] as [EventFilterable]), ([EthereumAddress("0xd5395c132c791a7f46fa8fc27f0ab6bacd824484")!] as [EventFilterable])]
// filter.parameterFilters = [([EthereumAddress("0xefdcf2c36f3756ce7247628afdb632fa4ee12ec5")!] as [EventFilterable]),
// ([EthereumAddress("0xd5395c132c791a7f46fa8fc27f0ab6bacd824484")!] as [EventFilterable])]
// guard let eventParser = contract?.createEventParser("Transfer", filter: filter) else {return XCTFail()}
// let present = try eventParser.parseBlockByNumberPromise(UInt64(5200120)).wait()
//
Expand Down
45 changes: 22 additions & 23 deletions Tests/web3swiftTests/remoteTests/PolicyResolverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import Web3Core

// swiftlint:disable force_unwrapping
final class PolicyResolverTests: XCTestCase {

func testResolveAllForEIP1159Transaction() async throws {
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let resolver = PolicyResolver(provider: web3.provider)
var tx = CodableTransaction(
var tx = try CodableTransaction(
type: .eip1559,
to: EthereumAddress("0xb47292B7bBedA4447564B8336E4eD1f93735e7C7")!,
chainID: web3.provider.network!.chainID,
value: try XCTUnwrap(Utilities.parseToBigUInt("0.1", units: .ether)),
value: XCTUnwrap(Utilities.parseToBigUInt("0.1", units: .ether)),
gasLimit: 21_000
)
// Vitalik's address
Expand All @@ -35,25 +34,25 @@ final class PolicyResolverTests: XCTestCase {
XCTAssertGreaterThan(tx.nonce, 0)
}

func testResolveAllForLegacyTransaction() async throws {
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let resolver = PolicyResolver(provider: web3.provider)
var tx = CodableTransaction(
type: .legacy,
to: EthereumAddress("0xb47292B7bBedA4447564B8336E4eD1f93735e7C7")!,
chainID: web3.provider.network!.chainID,
value: try XCTUnwrap(Utilities.parseToBigUInt("0.1", units: .ether)),
gasLimit: 21_000
)
// Vitalik's address
tx.from = EthereumAddress("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B")!
let policies = Policies(gasLimitPolicy: .manual(21_000))
try await resolver.resolveAll(for: &tx, with: policies)
// func testResolveAllForLegacyTransaction() async throws {
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// let resolver = PolicyResolver(provider: web3.provider)
// var tx = CodableTransaction(
// type: .legacy,
// to: EthereumAddress("0xb47292B7bBedA4447564B8336E4eD1f93735e7C7")!,
// chainID: web3.provider.network!.chainID,
// value: try XCTUnwrap(Utilities.parseToBigUInt("0.1", units: .ether)),
// gasLimit: 21_000
// )
// // Vitalik's address
// tx.from = EthereumAddress("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B")!
// let policies = Policies(gasLimitPolicy: .manual(21_000))
// try await resolver.resolveAll(for: &tx, with: policies)

XCTAssertGreaterThan(tx.gasLimit, 0)
XCTAssertGreaterThan(tx.gasPrice ?? 0, 0)
XCTAssertGreaterThan(tx.nonce, 0)
}
// XCTAssertGreaterThan(tx.gasLimit, 0)
// XCTAssertGreaterThan(tx.gasPrice ?? 0, 0)
// XCTAssertGreaterThan(tx.nonce, 0)
// }

func testResolveExact() async throws {
let expectedNonce = BigUInt(42)
Expand All @@ -62,11 +61,11 @@ final class PolicyResolverTests: XCTestCase {
let expectedPriorityFee = BigUInt(9)
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let resolver = PolicyResolver(provider: web3.provider)
var tx = CodableTransaction(
var tx = try CodableTransaction(
type: .eip1559,
to: EthereumAddress("0xb47292B7bBedA4447564B8336E4eD1f93735e7C7")!,
chainID: web3.provider.network!.chainID,
value: try XCTUnwrap(Utilities.parseToBigUInt("0.1", units: .ether)),
value: XCTUnwrap(Utilities.parseToBigUInt("0.1", units: .ether)),
gasLimit: 21_000
)
// Vitalik's address
Expand Down
Loading

0 comments on commit a9ec927

Please sign in to comment.