44from abc import ABC , abstractmethod
55from http .cookies import SimpleCookie
66from typing import Callable , Optional , Tuple
7+ from warnings import warn
8+
9+ import grpc
10+ from grpc import ChannelCredentials
711
812
913class CookieAssistant (ABC ):
@@ -181,8 +185,20 @@ def __init__(
181185 fee_denom : str ,
182186 env : str ,
183187 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 ,
185193 ):
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+
186202 self .lcd_endpoint = lcd_endpoint
187203 self .tm_websocket_endpoint = tm_websocket_endpoint
188204 self .grpc_endpoint = grpc_endpoint
@@ -193,7 +209,10 @@ def __init__(
193209 self .fee_denom = fee_denom
194210 self .env = env
195211 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
197216
198217 @classmethod
199218 def devnet (cls ):
@@ -219,6 +238,11 @@ def testnet(cls, node="lb"):
219238 if node not in nodes :
220239 raise ValueError ("Must be one of {}" .format (nodes ))
221240
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+
222246 if node == "lb" :
223247 lcd_endpoint = "https://testnet.sentry.lcd.injective.network:443"
224248 tm_websocket_endpoint = "wss://testnet.sentry.tm.injective.network:443/websocket"
@@ -227,7 +251,6 @@ def testnet(cls, node="lb"):
227251 grpc_explorer_endpoint = "testnet.sentry.explorer.grpc.injective.network:443"
228252 chain_stream_endpoint = "testnet.sentry.chain.stream.injective.network:443"
229253 cookie_assistant = BareMetalLoadBalancedCookieAssistant ()
230- use_secure_connection = True
231254 else :
232255 lcd_endpoint = "https://testnet.lcd.injective.network:443"
233256 tm_websocket_endpoint = "wss://testnet.tm.injective.network:443/websocket"
@@ -236,7 +259,6 @@ def testnet(cls, node="lb"):
236259 grpc_explorer_endpoint = "testnet.explorer.grpc.injective.network:443"
237260 chain_stream_endpoint = "testnet.chain.stream.injective.network:443"
238261 cookie_assistant = DisabledCookieAssistant ()
239- use_secure_connection = True
240262
241263 return cls (
242264 lcd_endpoint = lcd_endpoint ,
@@ -249,7 +271,10 @@ def testnet(cls, node="lb"):
249271 fee_denom = "inj" ,
250272 env = "testnet" ,
251273 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 ,
253278 )
254279
255280 @classmethod
@@ -267,7 +292,10 @@ def mainnet(cls, node="lb"):
267292 grpc_explorer_endpoint = "sentry.explorer.grpc.injective.network:443"
268293 chain_stream_endpoint = "sentry.chain.stream.injective.network:443"
269294 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 ()
271299
272300 return cls (
273301 lcd_endpoint = lcd_endpoint ,
@@ -280,7 +308,10 @@ def mainnet(cls, node="lb"):
280308 fee_denom = "inj" ,
281309 env = "mainnet" ,
282310 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 ,
284315 )
285316
286317 @classmethod
@@ -296,7 +327,6 @@ def local(cls):
296327 fee_denom = "inj" ,
297328 env = "local" ,
298329 cookie_assistant = DisabledCookieAssistant (),
299- use_secure_connection = False ,
300330 )
301331
302332 @classmethod
@@ -311,8 +341,20 @@ def custom(
311341 chain_id ,
312342 env ,
313343 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 ,
315349 ):
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+
316358 assistant = cookie_assistant or DisabledCookieAssistant ()
317359 return cls (
318360 lcd_endpoint = lcd_endpoint ,
@@ -325,7 +367,37 @@ def custom(
325367 fee_denom = "inj" ,
326368 env = env ,
327369 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 ,
329401 )
330402
331403 def string (self ):
@@ -336,3 +408,22 @@ async def chain_metadata(self, metadata_query_provider: Callable) -> Tuple[Tuple
336408
337409 async def exchange_metadata (self , metadata_query_provider : Callable ) -> Tuple [Tuple [str , str ]]:
338410 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