Skip to content

Commit 89bce8c

Browse files
committed
change to helper
1 parent 3d654c5 commit 89bce8c

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@
5555
Preprocessed,
5656
)
5757
from async_substrate_interface.utils import hex_to_bytes, json
58-
from async_substrate_interface.utils.decoding import _determine_if_old_runtime_call
58+
from async_substrate_interface.utils.decoding import (
59+
_determine_if_old_runtime_call,
60+
_bt_decode_to_dict_or_list,
61+
)
5962
from async_substrate_interface.utils.storage import StorageKey
6063
from async_substrate_interface.type_registry import _TYPE_REGISTRY
6164

@@ -2576,21 +2579,10 @@ async def _do_runtime_call_old(
25762579
result_vec_u8_bytes = hex_to_bytes(result_data["result"])
25772580
result_bytes = await self.decode_scale("Vec<u8>", result_vec_u8_bytes)
25782581

2579-
def _as_dict(obj):
2580-
as_dict = {}
2581-
for key in dir(obj):
2582-
if not key.startswith("_"):
2583-
val = getattr(obj, key)
2584-
if isinstance(val, (OldAxonInfo, OldPrometheusInfo)):
2585-
as_dict[key] = _as_dict(val)
2586-
else:
2587-
as_dict[key] = val
2588-
return as_dict
2589-
25902582
# Decode result
25912583
# Get correct type
25922584
result_decoded = runtime_call_def["decoder"](bytes(result_bytes))
2593-
as_dict = _as_dict(result_decoded)
2585+
as_dict = _bt_decode_to_dict_or_list(result_decoded)
25942586
logging.debug("Decoded old runtime call result: ", as_dict)
25952587
result_obj = ScaleObj(as_dict)
25962588

async_substrate_interface/sync_substrate.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
ScaleObj,
3737
)
3838
from async_substrate_interface.utils import hex_to_bytes, json
39-
from async_substrate_interface.utils.decoding import _determine_if_old_runtime_call
39+
from async_substrate_interface.utils.decoding import (
40+
_determine_if_old_runtime_call,
41+
_bt_decode_to_dict_or_list,
42+
)
4043
from async_substrate_interface.utils.storage import StorageKey
4144
from async_substrate_interface.type_registry import _TYPE_REGISTRY
4245

@@ -2289,21 +2292,10 @@ def _do_runtime_call_old(
22892292
result_vec_u8_bytes = hex_to_bytes(result_data["result"])
22902293
result_bytes = self.decode_scale("Vec<u8>", result_vec_u8_bytes)
22912294

2292-
def _as_dict(obj):
2293-
as_dict = {}
2294-
for key in dir(obj):
2295-
if not key.startswith("_"):
2296-
val = getattr(obj, key)
2297-
if isinstance(val, (OldAxonInfo, OldPrometheusInfo)):
2298-
as_dict[key] = _as_dict(val)
2299-
else:
2300-
as_dict[key] = val
2301-
return as_dict
2302-
23032295
# Decode result
23042296
# Get correct type
23052297
result_decoded = runtime_call_def["decoder"](bytes(result_bytes))
2306-
as_dict = _as_dict(result_decoded)
2298+
as_dict = _bt_decode_to_dict_or_list(result_decoded)
23072299
logging.debug("Decoded old runtime call result: ", as_dict)
23082300
result_obj = ScaleObj(as_dict)
23092301

async_substrate_interface/utils/decoding.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from bt_metadata import AxonInfo, PrometheusInfo
2+
3+
14
def _determine_if_old_runtime_call(runtime_call_def, metadata_v15_value) -> bool:
25
# Check if the output type is a Vec<u8>
36
# If so, call the API using the old method
@@ -24,3 +27,18 @@ def _determine_if_old_runtime_call(runtime_call_def, metadata_v15_value) -> bool
2427
):
2528
return True
2629
return False
30+
31+
32+
def _bt_decode_to_dict_or_list(obj) -> dict | list[dict]:
33+
if isinstance(obj, list):
34+
return [_bt_decode_to_dict_or_list(item) for item in obj]
35+
36+
as_dict = {}
37+
for key in dir(obj):
38+
if not key.startswith("_"):
39+
val = getattr(obj, key)
40+
if isinstance(val, (AxonInfo, PrometheusInfo)):
41+
as_dict[key] = _bt_decode_to_dict_or_list(val)
42+
else:
43+
as_dict[key] = val
44+
return as_dict

0 commit comments

Comments
 (0)