4
4
from abc import ABC , abstractmethod
5
5
from http .cookies import SimpleCookie
6
6
from typing import Callable , Optional , Tuple
7
+ from warnings import warn
8
+
9
+ import grpc
10
+ from grpc import ChannelCredentials
7
11
8
12
9
13
class CookieAssistant (ABC ):
@@ -181,8 +185,20 @@ def __init__(
181
185
fee_denom : str ,
182
186
env : str ,
183
187
cookie_assistant : CookieAssistant ,
184
- use_secure_connection : bool = False ,
188
+ use_secure_connection : Optional [bool ] = None ,
189
+ grpc_channel_credentials : Optional [ChannelCredentials ] = None ,
190
+ grpc_exchange_channel_credentials : Optional [ChannelCredentials ] = None ,
191
+ grpc_explorer_channel_credentials : Optional [ChannelCredentials ] = None ,
192
+ chain_stream_channel_credentials : Optional [ChannelCredentials ] = None ,
185
193
):
194
+ # the `use_secure_connection` parameter is ignored and will be deprecated soon.
195
+ if use_secure_connection is not None :
196
+ warn (
197
+ "use_secure_connection parameter in Network is no longer used and will be deprecated" ,
198
+ DeprecationWarning ,
199
+ stacklevel = 2 ,
200
+ )
201
+
186
202
self .lcd_endpoint = lcd_endpoint
187
203
self .tm_websocket_endpoint = tm_websocket_endpoint
188
204
self .grpc_endpoint = grpc_endpoint
@@ -193,7 +209,10 @@ def __init__(
193
209
self .fee_denom = fee_denom
194
210
self .env = env
195
211
self .cookie_assistant = cookie_assistant
196
- self .use_secure_connection = use_secure_connection
212
+ self .grpc_channel_credentials = grpc_channel_credentials
213
+ self .grpc_exchange_channel_credentials = grpc_exchange_channel_credentials
214
+ self .grpc_explorer_channel_credentials = grpc_explorer_channel_credentials
215
+ self .chain_stream_channel_credentials = chain_stream_channel_credentials
197
216
198
217
@classmethod
199
218
def devnet (cls ):
@@ -219,6 +238,11 @@ def testnet(cls, node="lb"):
219
238
if node not in nodes :
220
239
raise ValueError ("Must be one of {}" .format (nodes ))
221
240
241
+ grpc_channel_credentials = grpc .ssl_channel_credentials ()
242
+ grpc_exchange_channel_credentials = grpc .ssl_channel_credentials ()
243
+ grpc_explorer_channel_credentials = grpc .ssl_channel_credentials ()
244
+ chain_stream_channel_credentials = grpc .ssl_channel_credentials ()
245
+
222
246
if node == "lb" :
223
247
lcd_endpoint = "https://testnet.sentry.lcd.injective.network:443"
224
248
tm_websocket_endpoint = "wss://testnet.sentry.tm.injective.network:443/websocket"
@@ -227,7 +251,6 @@ def testnet(cls, node="lb"):
227
251
grpc_explorer_endpoint = "testnet.sentry.explorer.grpc.injective.network:443"
228
252
chain_stream_endpoint = "testnet.sentry.chain.stream.injective.network:443"
229
253
cookie_assistant = BareMetalLoadBalancedCookieAssistant ()
230
- use_secure_connection = True
231
254
else :
232
255
lcd_endpoint = "https://testnet.lcd.injective.network:443"
233
256
tm_websocket_endpoint = "wss://testnet.tm.injective.network:443/websocket"
@@ -236,7 +259,6 @@ def testnet(cls, node="lb"):
236
259
grpc_explorer_endpoint = "testnet.explorer.grpc.injective.network:443"
237
260
chain_stream_endpoint = "testnet.chain.stream.injective.network:443"
238
261
cookie_assistant = DisabledCookieAssistant ()
239
- use_secure_connection = True
240
262
241
263
return cls (
242
264
lcd_endpoint = lcd_endpoint ,
@@ -249,7 +271,10 @@ def testnet(cls, node="lb"):
249
271
fee_denom = "inj" ,
250
272
env = "testnet" ,
251
273
cookie_assistant = cookie_assistant ,
252
- use_secure_connection = use_secure_connection ,
274
+ grpc_channel_credentials = grpc_channel_credentials ,
275
+ grpc_exchange_channel_credentials = grpc_exchange_channel_credentials ,
276
+ grpc_explorer_channel_credentials = grpc_explorer_channel_credentials ,
277
+ chain_stream_channel_credentials = chain_stream_channel_credentials ,
253
278
)
254
279
255
280
@classmethod
@@ -267,7 +292,10 @@ def mainnet(cls, node="lb"):
267
292
grpc_explorer_endpoint = "sentry.explorer.grpc.injective.network:443"
268
293
chain_stream_endpoint = "sentry.chain.stream.injective.network:443"
269
294
cookie_assistant = BareMetalLoadBalancedCookieAssistant ()
270
- use_secure_connection = True
295
+ grpc_channel_credentials = grpc .ssl_channel_credentials ()
296
+ grpc_exchange_channel_credentials = grpc .ssl_channel_credentials ()
297
+ grpc_explorer_channel_credentials = grpc .ssl_channel_credentials ()
298
+ chain_stream_channel_credentials = grpc .ssl_channel_credentials ()
271
299
272
300
return cls (
273
301
lcd_endpoint = lcd_endpoint ,
@@ -280,7 +308,10 @@ def mainnet(cls, node="lb"):
280
308
fee_denom = "inj" ,
281
309
env = "mainnet" ,
282
310
cookie_assistant = cookie_assistant ,
283
- use_secure_connection = use_secure_connection ,
311
+ grpc_channel_credentials = grpc_channel_credentials ,
312
+ grpc_exchange_channel_credentials = grpc_exchange_channel_credentials ,
313
+ grpc_explorer_channel_credentials = grpc_explorer_channel_credentials ,
314
+ chain_stream_channel_credentials = chain_stream_channel_credentials ,
284
315
)
285
316
286
317
@classmethod
@@ -296,7 +327,6 @@ def local(cls):
296
327
fee_denom = "inj" ,
297
328
env = "local" ,
298
329
cookie_assistant = DisabledCookieAssistant (),
299
- use_secure_connection = False ,
300
330
)
301
331
302
332
@classmethod
@@ -311,8 +341,20 @@ def custom(
311
341
chain_id ,
312
342
env ,
313
343
cookie_assistant : Optional [CookieAssistant ] = None ,
314
- use_secure_connection : bool = False ,
344
+ use_secure_connection : Optional [bool ] = None ,
345
+ grpc_channel_credentials : Optional [ChannelCredentials ] = None ,
346
+ grpc_exchange_channel_credentials : Optional [ChannelCredentials ] = None ,
347
+ grpc_explorer_channel_credentials : Optional [ChannelCredentials ] = None ,
348
+ chain_stream_channel_credentials : Optional [ChannelCredentials ] = None ,
315
349
):
350
+ # the `use_secure_connection` parameter is ignored and will be deprecated soon.
351
+ if use_secure_connection is not None :
352
+ warn (
353
+ "use_secure_connection parameter in Network is no longer used and will be deprecated" ,
354
+ DeprecationWarning ,
355
+ stacklevel = 2 ,
356
+ )
357
+
316
358
assistant = cookie_assistant or DisabledCookieAssistant ()
317
359
return cls (
318
360
lcd_endpoint = lcd_endpoint ,
@@ -325,7 +367,37 @@ def custom(
325
367
fee_denom = "inj" ,
326
368
env = env ,
327
369
cookie_assistant = assistant ,
328
- use_secure_connection = use_secure_connection ,
370
+ grpc_channel_credentials = grpc_channel_credentials ,
371
+ grpc_exchange_channel_credentials = grpc_exchange_channel_credentials ,
372
+ grpc_explorer_channel_credentials = grpc_explorer_channel_credentials ,
373
+ chain_stream_channel_credentials = chain_stream_channel_credentials ,
374
+ )
375
+
376
+ @classmethod
377
+ def custom_chain_and_public_indexer_mainnet (
378
+ cls ,
379
+ lcd_endpoint ,
380
+ tm_websocket_endpoint ,
381
+ grpc_endpoint ,
382
+ chain_stream_endpoint ,
383
+ cookie_assistant : Optional [CookieAssistant ] = None ,
384
+ ):
385
+ mainnet_network = cls .mainnet ()
386
+
387
+ return cls .custom (
388
+ lcd_endpoint = lcd_endpoint ,
389
+ tm_websocket_endpoint = tm_websocket_endpoint ,
390
+ grpc_endpoint = grpc_endpoint ,
391
+ grpc_exchange_endpoint = mainnet_network .grpc_exchange_endpoint ,
392
+ grpc_explorer_endpoint = mainnet_network .grpc_explorer_endpoint ,
393
+ chain_stream_endpoint = chain_stream_endpoint ,
394
+ chain_id = "injective-1" ,
395
+ env = "mainnet" ,
396
+ cookie_assistant = cookie_assistant ,
397
+ grpc_channel_credentials = None ,
398
+ grpc_exchange_channel_credentials = mainnet_network .grpc_exchange_channel_credentials ,
399
+ grpc_explorer_channel_credentials = mainnet_network .grpc_explorer_channel_credentials ,
400
+ chain_stream_channel_credentials = None ,
329
401
)
330
402
331
403
def string (self ):
@@ -336,3 +408,22 @@ async def chain_metadata(self, metadata_query_provider: Callable) -> Tuple[Tuple
336
408
337
409
async def exchange_metadata (self , metadata_query_provider : Callable ) -> Tuple [Tuple [str , str ]]:
338
410
return await self .cookie_assistant .exchange_metadata (metadata_query_provider = metadata_query_provider )
411
+
412
+ def create_chain_grpc_channel (self ) -> grpc .Channel :
413
+ return self ._create_grpc_channel (self .grpc_endpoint , self .grpc_channel_credentials )
414
+
415
+ def create_exchange_grpc_channel (self ) -> grpc .Channel :
416
+ return self ._create_grpc_channel (self .grpc_exchange_endpoint , self .grpc_exchange_channel_credentials )
417
+
418
+ def create_explorer_grpc_channel (self ) -> grpc .Channel :
419
+ return self ._create_grpc_channel (self .grpc_explorer_endpoint , self .grpc_explorer_channel_credentials )
420
+
421
+ def create_chain_stream_grpc_channel (self ) -> grpc .Channel :
422
+ return self ._create_grpc_channel (self .chain_stream_endpoint , self .chain_stream_channel_credentials )
423
+
424
+ def _create_grpc_channel (self , endpoint : str , credentials : Optional [ChannelCredentials ]) -> grpc .Channel :
425
+ if credentials is None :
426
+ channel = grpc .aio .insecure_channel (endpoint )
427
+ else :
428
+ channel = grpc .aio .secure_channel (endpoint , credentials )
429
+ return channel
0 commit comments