Skip to content

Commit d205015

Browse files
committed
Add in selective type conversion
1 parent d139a17 commit d205015

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ def __init__(
734734
_mock: bool = False,
735735
_log_raw_websockets: bool = False,
736736
ws_shutdown_timer: float = 5.0,
737+
decode_ss58: bool = False,
737738
):
738739
"""
739740
The asyncio-compatible version of the subtensor interface commands we use in bittensor. It is important to
@@ -753,10 +754,15 @@ def __init__(
753754
_mock: whether to use mock version of the subtensor interface
754755
_log_raw_websockets: whether to log raw websocket requests during RPC requests
755756
ws_shutdown_timer: how long after the last connection your websocket should close
757+
decode_ss58: Whether to decode AccountIds to SS58 or leave them in raw bytes tuples.
756758
757759
"""
758760
super().__init__(
759-
type_registry, type_registry_preset, use_remote_preset, ss58_format
761+
type_registry,
762+
type_registry_preset,
763+
use_remote_preset,
764+
ss58_format,
765+
decode_ss58,
760766
)
761767
self.max_retries = max_retries
762768
self.retry_timeout = retry_timeout
@@ -996,12 +1002,15 @@ async def decode_scale(
9961002
runtime = await self.init_runtime(block_hash=block_hash)
9971003
if runtime.metadata_v15 is not None or force_legacy is True:
9981004
obj = decode_by_type_string(type_string, runtime.registry, scale_bytes)
999-
try:
1000-
type_str_int = int(type_string.split("::")[1])
1001-
decoded_type_str = runtime.type_id_to_name[type_str_int]
1002-
obj = convert_account_ids(obj, decoded_type_str)
1003-
except (ValueError, KeyError):
1004-
pass
1005+
if self.decode_ss58:
1006+
try:
1007+
type_str_int = int(type_string.split("::")[1])
1008+
decoded_type_str = runtime.type_id_to_name[type_str_int]
1009+
obj = convert_account_ids(
1010+
obj, decoded_type_str, runtime.ss58_format
1011+
)
1012+
except (ValueError, KeyError):
1013+
pass
10051014
else:
10061015
obj = legacy_scale_decode(type_string, scale_bytes, runtime)
10071016
if return_scale_obj:
@@ -3480,6 +3489,7 @@ async def query_map(
34803489
value_type,
34813490
key_hashers,
34823491
ignore_decoding_errors,
3492+
self.decode_ss58,
34833493
)
34843494
return AsyncQueryMapResult(
34853495
records=result,

async_substrate_interface/sync_substrate.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
_bt_decode_to_dict_or_list,
4545
decode_query_map,
4646
legacy_scale_decode,
47+
convert_account_ids,
4748
)
4849
from async_substrate_interface.utils.storage import StorageKey
4950
from async_substrate_interface.type_registry import _TYPE_REGISTRY
@@ -486,6 +487,7 @@ def __init__(
486487
retry_timeout: float = 60.0,
487488
_mock: bool = False,
488489
_log_raw_websockets: bool = False,
490+
decode_ss58: bool = False,
489491
):
490492
"""
491493
The sync compatible version of the subtensor interface commands we use in bittensor. Use this instance only
@@ -503,10 +505,15 @@ def __init__(
503505
retry_timeout: how to long wait since the last ping to retry the RPC request
504506
_mock: whether to use mock version of the subtensor interface
505507
_log_raw_websockets: whether to log raw websocket requests during RPC requests
508+
decode_ss58: Whether to decode AccountIds to SS58 or leave them in raw bytes tuples.
506509
507510
"""
508511
super().__init__(
509-
type_registry, type_registry_preset, use_remote_preset, ss58_format
512+
type_registry,
513+
type_registry_preset,
514+
use_remote_preset,
515+
ss58_format,
516+
decode_ss58,
510517
)
511518
self.max_retries = max_retries
512519
self.retry_timeout = retry_timeout
@@ -560,6 +567,7 @@ def initialize(self):
560567
)
561568
if ss58_prefix_constant:
562569
self.ss58_format = ss58_prefix_constant.value
570+
self.runtime.ss58_format = ss58_prefix_constant.value
563571
self.initialized = True
564572

565573
def __exit__(self, exc_type, exc_val, exc_tb):
@@ -693,6 +701,15 @@ def decode_scale(
693701
obj = decode_by_type_string(
694702
type_string, self.runtime.registry, scale_bytes
695703
)
704+
if self.decode_ss58:
705+
try:
706+
type_str_int = int(type_string.split("::")[1])
707+
decoded_type_str = self.runtime.type_id_to_name[type_str_int]
708+
obj = convert_account_ids(
709+
obj, decoded_type_str, self.ss58_format
710+
)
711+
except (ValueError, KeyError):
712+
pass
696713
else:
697714
obj = legacy_scale_decode(type_string, scale_bytes, self.runtime)
698715
if return_scale_obj:
@@ -3010,6 +3027,7 @@ def query_map(
30103027
value_type,
30113028
key_hashers,
30123029
ignore_decoding_errors,
3030+
self.decode_ss58,
30133031
)
30143032
return QueryMapResult(
30153033
records=result,

async_substrate_interface/types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,10 @@ def __init__(
553553
type_registry_preset: Optional[str] = None,
554554
use_remote_preset: bool = False,
555555
ss58_format: Optional[int] = None,
556+
decode_ss58: bool = False,
556557
):
557558
# We load a very basic RuntimeConfigurationObject that is only used for the initial metadata decoding
559+
self.decode_ss58 = decode_ss58
558560
self.runtime_config = RuntimeConfigurationObject(ss58_format=ss58_format)
559561
self.ss58_format = ss58_format
560562
self.runtime_config.update_type_registry(load_type_registry_preset(name="core"))

async_substrate_interface/utils/decoding.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def decode_query_map(
8181
value_type,
8282
key_hashers,
8383
ignore_decoding_errors,
84+
decode_ss58: bool = False,
8485
):
8586
def concat_hash_len(key_hasher: str) -> int:
8687
"""
@@ -129,25 +130,29 @@ def concat_hash_len(key_hasher: str) -> int:
129130
# strip key_hashers to use as item key
130131
if len(param_types) - len(params) == 1:
131132
item_key = dk[1]
132-
if kts[kts.index(", ") + 2 : kts.index(")")] == "scale_info::0":
133-
item_key = ss58_encode(bytes(item_key[0]), runtime.ss58_format)
133+
if decode_ss58:
134+
if kts[kts.index(", ") + 2 : kts.index(")")] == "scale_info::0":
135+
item_key = ss58_encode(bytes(item_key[0]), runtime.ss58_format)
134136

135137
else:
136138
item_key = tuple(
137139
dk[key + 1] for key in range(len(params), len(param_types) + 1, 2)
138140
)
139-
# TODO handle decoding here, but first figure out what triggers this
140141

141142
except Exception as _:
142143
if not ignore_decoding_errors:
143144
raise
144145
item_key = None
145-
try:
146-
value_type_str_int = int(vts.split("::")[1])
147-
decoded_type_str = runtime.type_id_to_name[value_type_str_int]
148-
item_value = convert_account_ids(dv, decoded_type_str, runtime.ss58_format)
149-
except (ValueError, KeyError) as e:
150-
item_value = dv
146+
item_value = dv
147+
if decode_ss58:
148+
try:
149+
value_type_str_int = int(vts.split("::")[1])
150+
decoded_type_str = runtime.type_id_to_name[value_type_str_int]
151+
item_value = convert_account_ids(
152+
dv, decoded_type_str, runtime.ss58_format
153+
)
154+
except (ValueError, KeyError):
155+
pass
151156
result.append([item_key, ScaleObj(item_value)])
152157
return result
153158

0 commit comments

Comments
 (0)