Skip to content

Commit f1811cb

Browse files
separated code to files
1 parent 57db4c3 commit f1811cb

File tree

5 files changed

+212
-194
lines changed

5 files changed

+212
-194
lines changed

web3swift.xcodeproj/project.pbxproj

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
3AE8910E2251F49D00D08E99 /* Web3+WebsocketProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE8910D2251F49D00D08E99 /* Web3+WebsocketProvider.swift */; };
3636
3AE8910F2251F4A100D08E99 /* Web3+WebsocketProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE8910D2251F49D00D08E99 /* Web3+WebsocketProvider.swift */; };
3737
3AE8911422522B4200D08E99 /* web3swift_websocket_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE8911322522B4200D08E99 /* web3swift_websocket_Tests.swift */; };
38+
3AE891162255216B00D08E99 /* Web3+Eth+Websocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE891152255216B00D08E99 /* Web3+Eth+Websocket.swift */; };
39+
3AE891172255216B00D08E99 /* Web3+Eth+Websocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE891152255216B00D08E99 /* Web3+Eth+Websocket.swift */; };
3840
4194811B203630530065A83B /* Web3+HttpProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 817EBB142004FE4200E02EAA /* Web3+HttpProvider.swift */; };
3941
4194811E203630530065A83B /* Web3.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81DDECCE1FDF004E0063684A /* Web3.swift */; };
4042
4194811F203630530065A83B /* Web3+Instance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 818ABD5A1FE95F8F002657BB /* Web3+Instance.swift */; };
@@ -183,8 +185,8 @@
183185
81D7D97820A61E3800A193EC /* EventFiltering.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81D7D97720A61E3800A193EC /* EventFiltering.swift */; };
184186
81D7D97920A61E3800A193EC /* EventFiltering.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81D7D97720A61E3800A193EC /* EventFiltering.swift */; };
185187
81DDECCF1FDF004E0063684A /* Web3.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81DDECCE1FDF004E0063684A /* Web3.swift */; };
186-
81DFB3FF210775320011DC85 /* Web3+Infura.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81DFB3FE210775320011DC85 /* Web3+Infura.swift */; };
187-
81DFB400210775320011DC85 /* Web3+Infura.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81DFB3FE210775320011DC85 /* Web3+Infura.swift */; };
188+
81DFB3FF210775320011DC85 /* Web3+InfuraProviders.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81DFB3FE210775320011DC85 /* Web3+InfuraProviders.swift */; };
189+
81DFB400210775320011DC85 /* Web3+InfuraProviders.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81DFB3FE210775320011DC85 /* Web3+InfuraProviders.swift */; };
188190
81EB1E4B208173D7003BD47F /* Web3+Personal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81EB1E4A208173D7003BD47F /* Web3+Personal.swift */; };
189191
81EB1E4C208173D7003BD47F /* Web3+Personal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81EB1E4A208173D7003BD47F /* Web3+Personal.swift */; };
190192
81FA43F62044097100EE14D5 /* web3swift_macOS_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81FA43F52044097100EE14D5 /* web3swift_macOS_Tests.swift */; };
@@ -258,6 +260,7 @@
258260
3AC3BD7F222EC6C900656EC7 /* Web3+SecurityToken.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Web3+SecurityToken.swift"; sourceTree = "<group>"; };
259261
3AE8910D2251F49D00D08E99 /* Web3+WebsocketProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Web3+WebsocketProvider.swift"; sourceTree = "<group>"; };
260262
3AE8911322522B4200D08E99 /* web3swift_websocket_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = web3swift_websocket_Tests.swift; sourceTree = "<group>"; };
263+
3AE891152255216B00D08E99 /* Web3+Eth+Websocket.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Web3+Eth+Websocket.swift"; sourceTree = "<group>"; };
261264
417715D420362916005C3E16 /* web3swift_macOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = web3swift_macOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
262265
4EFFCB6D208552F2008165FE /* web3swift_local_node_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = web3swift_local_node_Tests.swift; sourceTree = "<group>"; };
263266
4EFFCB6F208554EB008165FE /* web3swift_remoteParsing_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = web3swift_remoteParsing_Tests.swift; sourceTree = "<group>"; };
@@ -338,7 +341,7 @@
338341
81D7D97420A3240900A193EC /* EthereumFilterEncodingExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EthereumFilterEncodingExtensions.swift; sourceTree = "<group>"; };
339342
81D7D97720A61E3800A193EC /* EventFiltering.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventFiltering.swift; sourceTree = "<group>"; };
340343
81DDECCE1FDF004E0063684A /* Web3.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Web3.swift; sourceTree = "<group>"; };
341-
81DFB3FE210775320011DC85 /* Web3+Infura.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Web3+Infura.swift"; sourceTree = "<group>"; };
344+
81DFB3FE210775320011DC85 /* Web3+InfuraProviders.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Web3+InfuraProviders.swift"; sourceTree = "<group>"; };
342345
81EB1E4A208173D7003BD47F /* Web3+Personal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Web3+Personal.swift"; sourceTree = "<group>"; };
343346
81ED4EA72190D922003E932E /* NonceMiddleware.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NonceMiddleware.swift; sourceTree = "<group>"; };
344347
81FA43F32044097000EE14D5 /* web3swift-macOS_Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "web3swift-macOS_Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -706,9 +709,10 @@
706709
81DDECCE1FDF004E0063684A /* Web3.swift */,
707710
817EBB142004FE4200E02EAA /* Web3+HttpProvider.swift */,
708711
3AE8910D2251F49D00D08E99 /* Web3+WebsocketProvider.swift */,
712+
3AE891152255216B00D08E99 /* Web3+Eth+Websocket.swift */,
709713
818ABD5A1FE95F8F002657BB /* Web3+Instance.swift */,
710714
818ABD5C1FE95FC9002657BB /* Web3+Contract.swift */,
711-
81DFB3FE210775320011DC85 /* Web3+Infura.swift */,
715+
81DFB3FE210775320011DC85 /* Web3+InfuraProviders.swift */,
712716
8104E2271FE82BDC00F952CB /* Web3+Utils.swift */,
713717
810B0F991FEC446B00CF0DA2 /* Web3+JSONRPC.swift */,
714718
81C146F61FF274B200AA943E /* Web3+Structures.swift */,
@@ -1223,6 +1227,7 @@
12231227
81A1821A20D5A6F70016741F /* Promise+HttpProvider.swift in Sources */,
12241228
E2F8083621CB142000B6BF15 /* Web3+ERC1376.swift in Sources */,
12251229
E279C9F821C47CD00081695F /* Web3+ERC820.swift in Sources */,
1230+
3AE891162255216B00D08E99 /* Web3+Eth+Websocket.swift in Sources */,
12261231
81A1823720D6E2BB0016741F /* Promise+Web3+Eth+GetBlockByHash.swift in Sources */,
12271232
81909D51218DAEC0007D2AE5 /* Promise+Web3+Personal+CreateAccount.swift in Sources */,
12281233
E279C9FB21C4860F0081695F /* Web3+ERC1400.swift in Sources */,
@@ -1264,7 +1269,7 @@
12641269
3AC3BD7D222EA70500656EC7 /* Web3+ST20.swift in Sources */,
12651270
817EBB162004FE4200E02EAA /* Web3+HttpProvider.swift in Sources */,
12661271
8103BBCC2077B84400499769 /* PlainKeystore.swift in Sources */,
1267-
81DFB3FF210775320011DC85 /* Web3+Infura.swift in Sources */,
1272+
81DFB3FF210775320011DC85 /* Web3+InfuraProviders.swift in Sources */,
12681273
81909D4A218864A8007D2AE5 /* EIP681.swift in Sources */,
12691274
81C0FCF220440EB500D82FAF /* Web3+Protocols.swift in Sources */,
12701275
81C0FCF420440F0900D82FAF /* Web3+Options.swift in Sources */,
@@ -1348,10 +1353,11 @@
13481353
81EB1E4C208173D7003BD47F /* Web3+Personal.swift in Sources */,
13491354
81D7D97620A3240900A193EC /* EthereumFilterEncodingExtensions.swift in Sources */,
13501355
41948126203630530065A83B /* Web3+Eth.swift in Sources */,
1356+
3AE891172255216B00D08E99 /* Web3+Eth+Websocket.swift in Sources */,
13511357
81A1822320D678060016741F /* Promise+Web3+Eth+GetBlockNumber.swift in Sources */,
13521358
81909D2F21885B2C007D2AE5 /* Web3+ERC721.swift in Sources */,
13531359
4194812C203630530065A83B /* KeystoreManager.swift in Sources */,
1354-
81DFB400210775320011DC85 /* Web3+Infura.swift in Sources */,
1360+
81DFB400210775320011DC85 /* Web3+InfuraProviders.swift in Sources */,
13551361
81D7D97320A31FB700A193EC /* ComparisonExtensions.swift in Sources */,
13561362
81A1823E20D79C2C0016741F /* Promise+Web3+Eth+SendRawTransaction.swift in Sources */,
13571363
81A1823D20D79C290016741F /* Promise+Web3+Eth+Call.swift in Sources */,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Web3+Eth+Websocket.swift
3+
// web3swift
4+
//
5+
// Created by Anton on 03/04/2019.
6+
// Copyright © 2019 The Matter Inc. All rights reserved.
7+
//
8+
9+
import Starscream
10+
import PromiseKit
11+
import BigInt
12+
import Foundation
13+
14+
extension web3.Eth {
15+
16+
public func getWebsocketProvider(forDelegate delegate: Web3SocketDelegate) throws -> InfuraWebsocketProvider {
17+
var infuraWSProvider: InfuraWebsocketProvider
18+
if !(provider is InfuraWebsocketProvider) {
19+
guard let infuraNetwork = provider.network else {
20+
throw Web3Error.processingError(desc: "Wrong network")
21+
}
22+
guard let infuraProvider = InfuraWebsocketProvider(infuraNetwork, delegate: delegate, keystoreManager: provider.attachedKeystoreManager) else {
23+
throw Web3Error.processingError(desc: "Wrong network")
24+
}
25+
infuraWSProvider = infuraProvider
26+
} else {
27+
infuraWSProvider = provider as! InfuraWebsocketProvider
28+
}
29+
infuraWSProvider.connectSocket()
30+
return infuraWSProvider
31+
}
32+
33+
public func getLatestPendingTransactions(forDelegate delegate: Web3SocketDelegate) throws {
34+
let provider = try getWebsocketProvider(forDelegate: delegate)
35+
try provider.filter(method: .newPendingTransactionFilter)
36+
}
37+
38+
public func subscribeOnPendingTransactions(forDelegate delegate: Web3SocketDelegate) throws {
39+
let provider = try getWebsocketProvider(forDelegate: delegate)
40+
try provider.subscribeOnNewPendingTransactions()
41+
}
42+
}

web3swift/Web3/Classes/Web3+Infura.swift

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// web3swift
2+
//
3+
// Created by Alex Vlasov.
4+
// Copyright © 2018 Alex Vlasov. All rights reserved.
5+
//
6+
7+
import Foundation
8+
import BigInt
9+
import Starscream
10+
11+
/// Custom Web3 HTTP provider of Infura nodes.
12+
public final class InfuraProvider: Web3HttpProvider {
13+
public init?(_ net:Networks, accessToken token: String? = nil, keystoreManager manager: KeystoreManager? = nil) {
14+
var requestURLstring = "https://" + net.name + ".infura.io/"
15+
if token != nil {
16+
requestURLstring = requestURLstring + token!
17+
}
18+
let providerURL = URL(string: requestURLstring)
19+
super.init(providerURL!, network: net, keystoreManager: manager)
20+
}
21+
}
22+
23+
/// Custom Websocket provider of Infura nodes.
24+
public final class InfuraWebsocketProvider: WebsocketProvider {
25+
public var filterID: String?
26+
public var subscriptionIDs = Set<String>()
27+
private var subscriptionIDforUnsubscribing: String? = nil
28+
private var filterTimer: Timer?
29+
30+
public init?(_ network: Networks,
31+
delegate: Web3SocketDelegate,
32+
keystoreManager manager: KeystoreManager? = nil) {
33+
guard network == Networks.Kovan
34+
|| network == Networks.Rinkeby
35+
|| network == Networks.Ropsten
36+
|| network == Networks.Mainnet else {return nil}
37+
let networkName = network.name
38+
let urlString = "wss://\(networkName).infura.io/ws"
39+
guard let socketURL = URL(string: urlString) else {return nil}
40+
super.init(endpoint: socketURL,
41+
delegate: delegate,
42+
keystoreManager: manager)
43+
}
44+
45+
public static func connectToSocket(_ network: Networks,
46+
delegate: Web3SocketDelegate,
47+
keystoreManager manager: KeystoreManager? = nil) -> InfuraWebsocketProvider? {
48+
guard let socketProvider = InfuraWebsocketProvider(network,
49+
delegate: delegate,
50+
keystoreManager: manager) else {return nil}
51+
socketProvider.connectSocket()
52+
return socketProvider
53+
}
54+
55+
public func writeMessage(method: InfuraWebsocketMethod, params: [Encodable]) throws {
56+
let request = JSONRPCRequestFabric.prepareRequest(method, parameters: params)
57+
let encoder = JSONEncoder()
58+
let requestData = try encoder.encode(request)
59+
writeMessage(data: requestData)
60+
}
61+
62+
public func filter(method: InfuraWebsocketMethod, params: [Encodable]? = nil) throws {
63+
filterTimer?.invalidate()
64+
filterID = nil
65+
let params = params ?? []
66+
let paramsCount = params.count
67+
guard method.requiredNumOfParameters == paramsCount || method.requiredNumOfParameters == nil else {
68+
throw Web3Error.inputError(desc: "Wrong number of params: need - \(method.requiredNumOfParameters!), got - \(paramsCount)")
69+
}
70+
try writeMessage(method: method, params: params)
71+
filterTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(getFilterChanges), userInfo: nil, repeats: true)
72+
}
73+
74+
@objc public func getFilterChanges() throws {
75+
if let id = self.filterID {
76+
filterTimer?.invalidate()
77+
let method = InfuraWebsocketMethod.getFilterChanges
78+
try writeMessage(method: method, params: [id])
79+
}
80+
}
81+
82+
public func getFilterLogs() throws {
83+
if let id = self.filterID {
84+
let method = InfuraWebsocketMethod.getFilterLogs
85+
try writeMessage(method: method, params: [id])
86+
}
87+
}
88+
89+
public func unistallFilter() throws {
90+
if let id = self.filterID {
91+
let method = InfuraWebsocketMethod.uninstallFilter
92+
try writeMessage(method: method, params: [id])
93+
}
94+
}
95+
96+
public func subscribe(params: [Encodable]) throws {
97+
let method = InfuraWebsocketMethod.subscribe
98+
try writeMessage(method: method, params: params)
99+
}
100+
101+
public func unsubscribe(subscriptionID: String) throws {
102+
let method = InfuraWebsocketMethod.unsubscribe
103+
subscriptionIDforUnsubscribing = subscriptionID
104+
try writeMessage(method: method, params: [subscriptionID])
105+
}
106+
107+
public func subscribeOnNewHeads() throws {
108+
let method = InfuraWebsocketMethod.subscribe
109+
let params = ["newHeads"]
110+
try writeMessage(method: method, params: params)
111+
}
112+
113+
public func subscribeOnNewPendingTransactions() throws {
114+
let method = InfuraWebsocketMethod.subscribe
115+
let params = ["newPendingTransactions"]
116+
try writeMessage(method: method, params: params)
117+
}
118+
119+
public func subscribeOnSyncing() throws {
120+
guard network != Networks.Kovan else {
121+
throw Web3Error.inputError(desc: "Can't sync on Kovan")
122+
}
123+
let method = InfuraWebsocketMethod.subscribe
124+
let params = ["syncing"]
125+
try writeMessage(method: method, params: params)
126+
}
127+
128+
override public func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
129+
if let data = text.data(using: String.Encoding.utf8),
130+
let dictionary = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
131+
if filterID == nil,
132+
let result = dictionary["result"] as? String {
133+
// setting filter id
134+
filterID = result
135+
} else if let params = dictionary["params"] as? [String: Any],
136+
let subscription = params["subscription"] as? String,
137+
let result = params["result"] {
138+
// subscription result
139+
subscriptionIDs.insert(subscription)
140+
delegate.received(message: result)
141+
} else if let unsubscribed = dictionary["result"] as? Bool {
142+
// unsubsribe result
143+
if unsubscribed == true, let id = subscriptionIDforUnsubscribing {
144+
subscriptionIDs.remove(id)
145+
} else if let id = subscriptionIDforUnsubscribing {
146+
delegate.gotError(error: Web3Error.processingError(desc: "Can\'t unsubscribe \(id)"))
147+
} else {
148+
delegate.received(message: unsubscribed)
149+
}
150+
} else if let message = dictionary["result"] {
151+
// filter result
152+
delegate.received(message: message)
153+
} else {
154+
delegate.gotError(error: Web3Error.processingError(desc: "Can\'t get known result. Message is: \(text)"))
155+
}
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)