Skip to content

Commit

Permalink
add get_network_name() to python client
Browse files Browse the repository at this point in the history
  • Loading branch information
sslivkoff committed Sep 17, 2024
1 parent 5d8b4f7 commit 1922aeb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/mesc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
find_endpoints,
get_global_metadata,
)
from .network_utils import (
get_network_name,
)

import typing

Expand All @@ -26,6 +29,7 @@
'get_endpoint_by_query',
'find_endpoints',
'get_global_metadata',
'get_network_name',
)

else:
Expand All @@ -37,6 +41,7 @@
'get_endpoint_by_query',
'find_endpoints',
'get_global_metadata',
'get_network_name',
)

__version__ = '0.2.0'
30 changes: 30 additions & 0 deletions python/mesc/network_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,33 @@ def get_by_chain_id(mapping: typing.Mapping[str, T], chain_id: str) -> T | None:

def chain_ids_equal(lhs: str, rhs: str) -> bool:
return chain_id_to_standard_hex(lhs) == chain_id_to_standard_hex(rhs)


def get_network_name(network: int | str) -> str | None:
if isinstance(network, str):
# 1. check if a known name
if network in network_names.network_names.values():
for k, v in network_names.network_names.items():
if v == network:
return v

# 2. check if a hex number
try:
if network.startswith('0x'):
as_int = int(network, 16)
else:
as_int = int(network)
as_int_str = str(as_int)
if as_int_str in network_names.network_names:
return network_names.network_names[as_int_str]
except ValueError:
pass

# 3. return as a name
return network

elif isinstance(network, int):
return network_names.network_names.get(str(network))

else:
raise Exception('invalid format for network')

0 comments on commit 1922aeb

Please sign in to comment.