Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions Sources/web3swift/Web3/Web3+HttpProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ public class Web3HttpProvider: Web3Provider {
let urlSession = URLSession(configuration: config)
return urlSession
}()
public init?(_ httpProviderURL: URL, network net: Networks?, keystoreManager manager: KeystoreManager? = nil) async {
guard httpProviderURL.scheme == "http" || httpProviderURL.scheme == "https" else { return nil }
url = httpProviderURL

@available(*, deprecated, message: "Will be removed in Web3Swift v4. Please use `init(url: URL, network: Networks?, keystoreManager: KeystoreManager?)` instead as it will throw an error instead of returning `nil` value.")
public convenience init?(_ httpProviderURL: URL, network net: Networks?, keystoreManager manager: KeystoreManager? = nil) async {
try? await self.init(url: httpProviderURL, network: net, keystoreManager: manager)
}

public init(url: URL, network net: Networks?, keystoreManager manager: KeystoreManager? = nil) async throws {
guard url.scheme == "http" || url.scheme == "https" else {
throw Web3Error.inputError(desc: "Web3HttpProvider endpoint must have scheme http or https. Given scheme \(url.scheme ?? "none"). \(url.absoluteString)")
}

self.url = url
if let net = net {
network = net
} else {
Expand All @@ -29,13 +38,8 @@ public class Web3HttpProvider: Web3Provider {
urlRequest.setValue("application/json", forHTTPHeaderField: "Accept")
urlRequest.httpMethod = APIRequest.getNetwork.call
urlRequest.httpBody = APIRequest.getNetwork.encodedBody
do {
let response: APIResponse<UInt> = try await APIRequest.send(uRLRequest: urlRequest, with: session)
let network = Networks.fromInt(response.result)
self.network = network
} catch {
return nil
}
let response: APIResponse<UInt> = try await APIRequest.send(uRLRequest: urlRequest, with: session)
self.network = Networks.fromInt(response.result)
}
attachedKeystoreManager = manager
}
Expand Down
12 changes: 9 additions & 3 deletions Sources/web3swift/Web3/Web3+InfuraProviders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ import Web3Core

/// Custom Web3 HTTP provider of Infura nodes.
public final class InfuraProvider: Web3HttpProvider {
public init?(_ net: Networks, accessToken token: String? = nil, keystoreManager manager: KeystoreManager? = nil) async {

@available(*, deprecated, message: "Will be removed in Web3Swift v4. Please use `init(net: Networks, accessToken: String?, keystoreManager: KeystoreManager?)` instead as it will throw an error instead of returning `nil`.")
public convenience init?(_ net: Networks, accessToken token: String? = nil, keystoreManager manager: KeystoreManager? = nil) async {
try? await self.init(net: net, accessToken: token, keystoreManager: manager)
}

public init(net: Networks, accessToken token: String? = nil, keystoreManager manager: KeystoreManager? = nil) async throws {
var requestURLstring = "https://" + net.name + Constants.infuraHttpScheme
requestURLstring += token ?? Constants.infuraToken
guard let providerURL = URL(string: requestURLstring) else {
return nil
throw Web3Error.inputError(desc: "URL created with token \(token ?? "Default token - \(Constants.infuraToken)") is not a valid URL: \(requestURLstring)")
}
await super.init(providerURL, network: net, keystoreManager: manager)
try await super.init(url: providerURL, network: net, keystoreManager: manager)
}
}
25 changes: 11 additions & 14 deletions Sources/web3swift/Web3/Web3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,40 @@ extension Web3 {
/// Initialized provider-bound Web3 instance using a provider's URL. Under the hood it performs a synchronous call to get
/// the Network ID for EIP155 purposes
public static func new(_ providerURL: URL, network: Networks = .Mainnet) async throws -> Web3 {
// FIXME: Change this hardcoded value to dynamically fethed from a Node
guard let provider = await Web3HttpProvider(providerURL, network: network) else {
throw Web3Error.inputError(desc: "Wrong provider - should be Web3HttpProvider with endpoint scheme http or https")
}
let provider = try await Web3HttpProvider(url: providerURL, network: network)
return Web3(provider: provider)
}

/// Initialized Web3 instance bound to Infura's mainnet provider.
public static func InfuraMainnetWeb3(accessToken: String? = nil) async -> Web3? {
guard let infura = await InfuraProvider(Networks.Mainnet, accessToken: accessToken) else { return nil }
public static func InfuraMainnetWeb3(accessToken: String? = nil) async throws -> Web3 {
let infura = try await InfuraProvider(net: Networks.Mainnet, accessToken: accessToken)
return Web3(provider: infura)
}

/// Initialized Web3 instance bound to Infura's goerli provider.
public static func InfuraGoerliWeb3(accessToken: String? = nil) async -> Web3? {
guard let infura = await InfuraProvider(Networks.Goerli, accessToken: accessToken) else { return nil }
public static func InfuraGoerliWeb3(accessToken: String? = nil) async throws -> Web3 {
let infura = try await InfuraProvider(net: Networks.Goerli, accessToken: accessToken)
return Web3(provider: infura)
}

/// Initialized Web3 instance bound to Infura's rinkeby provider.
@available(*, deprecated, message: "This network support was deprecated by Infura")
public static func InfuraRinkebyWeb3(accessToken: String? = nil) async -> Web3? {
guard let infura = await InfuraProvider(Networks.Rinkeby, accessToken: accessToken) else { return nil }
public static func InfuraRinkebyWeb3(accessToken: String? = nil) async throws -> Web3 {
let infura = try await InfuraProvider(net: Networks.Rinkeby, accessToken: accessToken)
return Web3(provider: infura)
}

/// Initialized Web3 instance bound to Infura's ropsten provider.
@available(*, deprecated, message: "This network support was deprecated by Infura")
public static func InfuraRopstenWeb3(accessToken: String? = nil) async -> Web3? {
guard let infura = await InfuraProvider(Networks.Ropsten, accessToken: accessToken) else { return nil }
public static func InfuraRopstenWeb3(accessToken: String? = nil) async throws -> Web3 {
let infura = try await InfuraProvider(net: Networks.Ropsten, accessToken: accessToken)
return Web3(provider: infura)
}

/// Initialized Web3 instance bound to Infura's kovan provider.
@available(*, deprecated, message: "This network support was deprecated by Infura")
public static func InfuraKovanWeb3(accessToken: String? = nil) async -> Web3? {
guard let infura = await InfuraProvider(Networks.Kovan, accessToken: accessToken) else { return nil }
public static func InfuraKovanWeb3(accessToken: String? = nil) async throws -> Web3 {
let infura = try await InfuraProvider(net: Networks.Kovan, accessToken: accessToken)
return Web3(provider: infura)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ST20AndSecurityTokenTests: XCTestCase {
var securityToken: SecurityToken!

override func setUp() async throws {
web3 = await Web3.InfuraGoerliWeb3(accessToken: Constants.infuraToken)
web3 = try await Web3.InfuraGoerliWeb3(accessToken: Constants.infuraToken)
ethMock = Web3EthMock(provider: web3.provider)
web3.ethInstance = ethMock
st20token = ST20.init(web3: web3, provider: web3.provider, address: .contractDeploymentAddress())
Expand Down
12 changes: 2 additions & 10 deletions Tests/web3swiftTests/remoteTests/EIP1559Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import Web3Core
final class EIP1559Tests: XCTestCase {

func testEIP1159MainnetTransaction() async throws {
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
var tx = CodableTransaction(
type: .eip1559,
to: EthereumAddress("0xb47292B7bBedA4447564B8336E4eD1f93735e7C7")!,
Expand All @@ -34,11 +30,7 @@ final class EIP1559Tests: XCTestCase {
}

func testEIP1159GoerliTransaction() async throws {
guard let web3 = await Web3.InfuraGoerliWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraGoerli using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraGoerliWeb3(accessToken: Constants.infuraToken)
var tx = CodableTransaction(
type: .eip1559,
to: EthereumAddress("0xeBec795c9c8bBD61FFc14A6662944748F299cAcf")!,
Expand Down
48 changes: 8 additions & 40 deletions Tests/web3swiftTests/remoteTests/ENSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ class ENSTests: XCTestCase {
}

func testResolverAddress() async throws {
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let ens = ENS(web3: web3)
let domain = "somename.eth"
let address = try await ens?.registry.getResolver(forDomain: domain).resolverContractAddress
Expand All @@ -36,23 +32,15 @@ class ENSTests: XCTestCase {
}

func testResolver() async throws {
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let ens = ENS(web3: web3)
let domain = "somename.eth"
let address = try await ens?.getAddress(forNode: domain)
XCTAssertEqual(address?.address.lowercased(), "0xc1ccfb5fc589b83b9e849c6f9b26efc71419898d")
}

func testSupportsInterface() async throws {
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let ens = ENS(web3: web3)
let domain = "somename.eth"
let resolver = try await ens?.registry.getResolver(forDomain: domain)
Expand All @@ -67,11 +55,7 @@ class ENSTests: XCTestCase {
}

func testABI() async throws {
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let ens = ENS(web3: web3)
let domain = "somename.eth"
let resolver = try await ens?.registry.getResolver(forDomain: domain)
Expand All @@ -86,35 +70,23 @@ class ENSTests: XCTestCase {
}

func testOwner() async throws {
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let ens = ENS(web3: web3)
let domain = "somename.eth"
let owner = try await ens?.registry.getOwner(node: domain)
XCTAssertEqual("0xc1ccfb5fc589b83b9e849c6f9b26efc71419898d", owner?.address.lowercased())
}

func testTTL() async throws {
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let ens = try XCTUnwrap(ENS(web3: web3))
let domain = "somename.eth"
let ttl = try await ens.registry.getTTL(node: domain)
XCTAssertGreaterThanOrEqual(ttl, 0)
}

func testGetAddress() async throws {
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let ens = ENS(web3: web3)
let domain = "somename.eth"
let resolver = try await ens?.registry.getResolver(forDomain: domain)
Expand All @@ -123,11 +95,7 @@ class ENSTests: XCTestCase {
}

func testGetPubkey() async throws {
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
let ens = ENS(web3: web3)
let domain = "somename.eth"
let resolver = try await ens?.registry.getResolver(forDomain: domain)
Expand Down
26 changes: 7 additions & 19 deletions Tests/web3swiftTests/remoteTests/GasOracleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ class GasOracleTests: XCTestCase {
let blockNumber: BigUInt = 14571792

func testPretictBaseFee() async throws {
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
lazy var oracle: Oracle = .init(web3.provider, block: .exact(blockNumber), blockCount: 20, percentiles: [10, 40, 60, 90])
let etalonPercentiles: [BigUInt] = [
94217344703, // 10 percentile
Expand All @@ -34,11 +30,7 @@ class GasOracleTests: XCTestCase {
}

func testPredictTip() async throws {
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
lazy var oracle: Oracle = .init(web3.provider, block: .exact(blockNumber), blockCount: 20, percentiles: [10, 40, 60, 90])
let etalonPercentiles: [BigUInt] = [
1217066957, // 10 percentile
Expand All @@ -52,11 +44,7 @@ class GasOracleTests: XCTestCase {
}

func testPredictBothFee() async throws {
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
else {
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
return
}
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
lazy var oracle: Oracle = .init(web3.provider, block: .exact(blockNumber), blockCount: 20, percentiles: [10, 40, 60, 90])
let etalonPercentiles: ([BigUInt], [BigUInt]) = (
baseFee: [
Expand All @@ -79,7 +67,7 @@ class GasOracleTests: XCTestCase {
}

// func testPredictLegacyGasPrice() async throws {
// let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// lazy var oracle: Web3.Oracle = .init(web3, block: .exact(blockNumber), blockCount: 20, percentiles: [10, 40, 60, 90])
// let etalonPercentiles: [BigUInt] = [
// 93253857566, // 10 percentile
Expand All @@ -93,7 +81,7 @@ class GasOracleTests: XCTestCase {
// }
//
// func testAllTransactionInBlockDecodesWell() async throws {
// let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// lazy var oracle: Web3.Oracle = .init(web3, block: .exact(blockNumber), blockCount: 20, percentiles: [10, 40, 60, 90])
// let blockWithTransaction = try await web3.eth.getBlockByNumber(blockNumber, fullTransactions: true)
//
Expand All @@ -107,13 +95,13 @@ class GasOracleTests: XCTestCase {

// FIXME: Move it to external test suit.
// func testBlockNumber() async throws {
// let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// let latestBlockNumber = try await web3.eth.getBlockNumber()
//
// }
//
// func testgetAccounts() async throws {
// let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
// let accounts = try await web3.eth.getAccounts()
//
// }
Expand Down
Loading