Skip to content

Commit 763f307

Browse files
Merge pull request #774 from JeneaVranceanu/fix/http-provider-init-alternative
fix: web3 infura and http provider alternative throwing initalizers
2 parents 92db962 + efe14a3 commit 763f307

File tree

10 files changed

+66
-151
lines changed

10 files changed

+66
-151
lines changed

Sources/web3swift/Web3/Web3+HttpProvider.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@ public class Web3HttpProvider: Web3Provider {
1818
let urlSession = URLSession(configuration: config)
1919
return urlSession
2020
}()
21-
public init?(_ httpProviderURL: URL, network net: Networks?, keystoreManager manager: KeystoreManager? = nil) async {
22-
guard httpProviderURL.scheme == "http" || httpProviderURL.scheme == "https" else { return nil }
23-
url = httpProviderURL
21+
22+
@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.")
23+
public convenience init?(_ httpProviderURL: URL, network net: Networks?, keystoreManager manager: KeystoreManager? = nil) async {
24+
try? await self.init(url: httpProviderURL, network: net, keystoreManager: manager)
25+
}
26+
27+
public init(url: URL, network net: Networks?, keystoreManager manager: KeystoreManager? = nil) async throws {
28+
guard url.scheme == "http" || url.scheme == "https" else {
29+
throw Web3Error.inputError(desc: "Web3HttpProvider endpoint must have scheme http or https. Given scheme \(url.scheme ?? "none"). \(url.absoluteString)")
30+
}
31+
32+
self.url = url
2433
if let net = net {
2534
network = net
2635
} else {
@@ -29,13 +38,8 @@ public class Web3HttpProvider: Web3Provider {
2938
urlRequest.setValue("application/json", forHTTPHeaderField: "Accept")
3039
urlRequest.httpMethod = APIRequest.getNetwork.call
3140
urlRequest.httpBody = APIRequest.getNetwork.encodedBody
32-
do {
33-
let response: APIResponse<UInt> = try await APIRequest.send(uRLRequest: urlRequest, with: session)
34-
let network = Networks.fromInt(response.result)
35-
self.network = network
36-
} catch {
37-
return nil
38-
}
41+
let response: APIResponse<UInt> = try await APIRequest.send(uRLRequest: urlRequest, with: session)
42+
self.network = Networks.fromInt(response.result)
3943
}
4044
attachedKeystoreManager = manager
4145
}

Sources/web3swift/Web3/Web3+InfuraProviders.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ import Web3Core
88

99
/// Custom Web3 HTTP provider of Infura nodes.
1010
public final class InfuraProvider: Web3HttpProvider {
11-
public init?(_ net: Networks, accessToken token: String? = nil, keystoreManager manager: KeystoreManager? = nil) async {
11+
12+
@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`.")
13+
public convenience init?(_ net: Networks, accessToken token: String? = nil, keystoreManager manager: KeystoreManager? = nil) async {
14+
try? await self.init(net: net, accessToken: token, keystoreManager: manager)
15+
}
16+
17+
public init(net: Networks, accessToken token: String? = nil, keystoreManager manager: KeystoreManager? = nil) async throws {
1218
var requestURLstring = "https://" + net.name + Constants.infuraHttpScheme
1319
requestURLstring += token ?? Constants.infuraToken
1420
guard let providerURL = URL(string: requestURLstring) else {
15-
return nil
21+
throw Web3Error.inputError(desc: "URL created with token \(token ?? "Default token - \(Constants.infuraToken)") is not a valid URL: \(requestURLstring)")
1622
}
17-
await super.init(providerURL, network: net, keystoreManager: manager)
23+
try await super.init(url: providerURL, network: net, keystoreManager: manager)
1824
}
1925
}

Sources/web3swift/Web3/Web3.swift

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,40 @@ extension Web3 {
1313
/// Initialized provider-bound Web3 instance using a provider's URL. Under the hood it performs a synchronous call to get
1414
/// the Network ID for EIP155 purposes
1515
public static func new(_ providerURL: URL, network: Networks = .Mainnet) async throws -> Web3 {
16-
// FIXME: Change this hardcoded value to dynamically fethed from a Node
17-
guard let provider = await Web3HttpProvider(providerURL, network: network) else {
18-
throw Web3Error.inputError(desc: "Wrong provider - should be Web3HttpProvider with endpoint scheme http or https")
19-
}
16+
let provider = try await Web3HttpProvider(url: providerURL, network: network)
2017
return Web3(provider: provider)
2118
}
2219

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

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

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

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

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

Tests/web3swiftTests/localTests/ST20AndSecurityTokenTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ST20AndSecurityTokenTests: XCTestCase {
1818
var securityToken: SecurityToken!
1919

2020
override func setUp() async throws {
21-
web3 = await Web3.InfuraGoerliWeb3(accessToken: Constants.infuraToken)
21+
web3 = try await Web3.InfuraGoerliWeb3(accessToken: Constants.infuraToken)
2222
ethMock = Web3EthMock(provider: web3.provider)
2323
web3.ethInstance = ethMock
2424
st20token = ST20.init(web3: web3, provider: web3.provider, address: .contractDeploymentAddress())

Tests/web3swiftTests/remoteTests/EIP1559Tests.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ import Web3Core
1414
final class EIP1559Tests: XCTestCase {
1515

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

3632
func testEIP1159GoerliTransaction() async throws {
37-
guard let web3 = await Web3.InfuraGoerliWeb3(accessToken: Constants.infuraToken)
38-
else {
39-
XCTFail("Failed to connect to InfuraGoerli using token \(Constants.infuraToken)")
40-
return
41-
}
33+
let web3 = try await Web3.InfuraGoerliWeb3(accessToken: Constants.infuraToken)
4234
var tx = CodableTransaction(
4335
type: .eip1559,
4436
to: EthereumAddress("0xeBec795c9c8bBD61FFc14A6662944748F299cAcf")!,

Tests/web3swiftTests/remoteTests/ENSTests.swift

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ class ENSTests: XCTestCase {
2323
}
2424

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

3834
func testResolver() async throws {
39-
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
40-
else {
41-
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
42-
return
43-
}
35+
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
4436
let ens = ENS(web3: web3)
4537
let domain = "somename.eth"
4638
let address = try await ens?.getAddress(forNode: domain)
4739
XCTAssertEqual(address?.address.lowercased(), "0xc1ccfb5fc589b83b9e849c6f9b26efc71419898d")
4840
}
4941

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

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

8872
func testOwner() async throws {
89-
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
90-
else {
91-
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
92-
return
93-
}
73+
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
9474
let ens = ENS(web3: web3)
9575
let domain = "somename.eth"
9676
let owner = try await ens?.registry.getOwner(node: domain)
9777
XCTAssertEqual("0xc1ccfb5fc589b83b9e849c6f9b26efc71419898d", owner?.address.lowercased())
9878
}
9979

10080
func testTTL() async throws {
101-
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
102-
else {
103-
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
104-
return
105-
}
81+
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
10682
let ens = try XCTUnwrap(ENS(web3: web3))
10783
let domain = "somename.eth"
10884
let ttl = try await ens.registry.getTTL(node: domain)
10985
XCTAssertGreaterThanOrEqual(ttl, 0)
11086
}
11187

11288
func testGetAddress() async throws {
113-
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
114-
else {
115-
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
116-
return
117-
}
89+
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
11890
let ens = ENS(web3: web3)
11991
let domain = "somename.eth"
12092
let resolver = try await ens?.registry.getResolver(forDomain: domain)
@@ -123,11 +95,7 @@ class ENSTests: XCTestCase {
12395
}
12496

12597
func testGetPubkey() async throws {
126-
guard let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
127-
else {
128-
XCTFail("Failed to connect to InfuraMainnet using token \(Constants.infuraToken)")
129-
return
130-
}
98+
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
13199
let ens = ENS(web3: web3)
132100
let domain = "somename.eth"
133101
let resolver = try await ens?.registry.getResolver(forDomain: domain)

Tests/web3swiftTests/remoteTests/GasOracleTests.swift

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ class GasOracleTests: XCTestCase {
1616
let blockNumber: BigUInt = 14571792
1717

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

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

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

8169
// func testPredictLegacyGasPrice() async throws {
82-
// let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
70+
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
8371
// lazy var oracle: Web3.Oracle = .init(web3, block: .exact(blockNumber), blockCount: 20, percentiles: [10, 40, 60, 90])
8472
// let etalonPercentiles: [BigUInt] = [
8573
// 93253857566, // 10 percentile
@@ -93,7 +81,7 @@ class GasOracleTests: XCTestCase {
9381
// }
9482
//
9583
// func testAllTransactionInBlockDecodesWell() async throws {
96-
// let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
84+
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
9785
// lazy var oracle: Web3.Oracle = .init(web3, block: .exact(blockNumber), blockCount: 20, percentiles: [10, 40, 60, 90])
9886
// let blockWithTransaction = try await web3.eth.getBlockByNumber(blockNumber, fullTransactions: true)
9987
//
@@ -107,13 +95,13 @@ class GasOracleTests: XCTestCase {
10795

10896
// FIXME: Move it to external test suit.
10997
// func testBlockNumber() async throws {
110-
// let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
98+
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
11199
// let latestBlockNumber = try await web3.eth.getBlockNumber()
112100
//
113101
// }
114102
//
115103
// func testgetAccounts() async throws {
116-
// let web3 = await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
104+
// let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
117105
// let accounts = try await web3.eth.getAccounts()
118106
//
119107
// }

0 commit comments

Comments
 (0)