@@ -9,6 +9,7 @@ public class ReadOnlyRPCProvider {
9
9
let infuraAPIKey : String
10
10
private let network : any Networking
11
11
12
+ let rpcUrls : [ String : String ]
12
13
let readonlyRPCMap : [ String : String ]
13
14
14
15
public convenience init ( infuraAPIKey: String ? = nil , readonlyRPCMap: [ String : String ] ? = nil ) {
@@ -18,26 +19,31 @@ public class ReadOnlyRPCProvider {
18
19
init ( infuraAPIKey: String ? = nil , readonlyRPCMap: [ String : String ] ? , network: any Networking ) {
19
20
self . infuraAPIKey = infuraAPIKey ?? " "
20
21
self . network = network
22
+ self . readonlyRPCMap = readonlyRPCMap ?? [ : ]
21
23
22
24
if let providedRPCMap = readonlyRPCMap {
23
25
if let apiKey = infuraAPIKey {
24
26
// Merge infuraReadonlyRPCMap with readonlyRPCMap, overriding infura's keys if they are present in readonlyRPCMap
25
27
var mergedMap = ReadOnlyRPCProvider . infuraReadonlyRPCMap ( apiKey)
26
28
providedRPCMap. forEach { mergedMap [ $0. key] = $0. value }
27
- self . readonlyRPCMap = mergedMap
29
+ self . rpcUrls = mergedMap
28
30
} else {
29
31
// Use only the provided readonlyRPCMap
30
- self . readonlyRPCMap = providedRPCMap
32
+ self . rpcUrls = providedRPCMap
31
33
}
32
34
} else if let apiKey = infuraAPIKey {
33
35
// Use infuraReadonlyRPCMap as default
34
- self . readonlyRPCMap = ReadOnlyRPCProvider . infuraReadonlyRPCMap ( apiKey)
36
+ self . rpcUrls = ReadOnlyRPCProvider . infuraReadonlyRPCMap ( apiKey)
35
37
} else {
36
38
// Default to an empty map if neither are provided
37
- self . readonlyRPCMap = [ : ]
39
+ self . rpcUrls = [ : ]
38
40
}
39
41
}
40
42
43
+ func supportsChain( _ chainId: String ) -> Bool {
44
+ return rpcUrls [ chainId] != nil && ( readonlyRPCMap [ chainId] != nil || !infuraAPIKey. isEmpty)
45
+ }
46
+
41
47
static func infuraReadonlyRPCMap( _ infuraAPIKey: String ) -> [ String : String ] {
42
48
[
43
49
// ###### Ethereum ######
@@ -96,17 +102,20 @@ public class ReadOnlyRPCProvider {
96
102
}
97
103
98
104
func endpoint( for chainId: String ) -> String ? {
99
- readonlyRPCMap [ chainId]
105
+ rpcUrls [ chainId]
100
106
}
101
107
102
- public func sendRequest( _ request: any RPCRequest , chainId: String , appMetadata: AppMetadata ) async -> Any ? {
108
+ public func sendRequest( _ request: any RPCRequest ,
109
+ params: Any = " " ,
110
+ chainId: String ,
111
+ appMetadata: AppMetadata ) async -> Any ? {
103
112
Logging . log ( " ReadOnlyRPCProvider:: Sending request \( request. method) on chain \( chainId) via Infura API " )
104
113
105
114
let params : [ String : Any ] = [
106
115
" method " : request. method,
107
116
" jsonrpc " : " 2.0 " ,
108
117
" id " : request. id,
109
- " params " : request . params
118
+ " params " : params
110
119
]
111
120
112
121
guard let endpoint = endpoint ( for: chainId) else {
@@ -132,10 +141,14 @@ public class ReadOnlyRPCProvider {
132
141
}
133
142
134
143
Logging . error ( " ReadOnlyRPCProvider:: could not get result from response \( json) " )
144
+ if let error = json [ " error " ] as? [ String : Any ] {
145
+ return RequestError ( from: error)
146
+ }
147
+
135
148
return nil
136
149
} catch {
137
- Logging . error ( " tracking error: \( error. localizedDescription) " )
138
- return nil
150
+ Logging . error ( " ReadOnlyRPCProvider:: error: \( error. localizedDescription) " )
151
+ return RequestError ( from : [ " code " : - 1 , " message " : error . localizedDescription ] )
139
152
}
140
153
}
141
154
}
0 commit comments