Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion async_substrate_interface/async_substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3055,7 +3055,9 @@ async def _do_runtime_call_old(
param_data = b""

if "encoder" in runtime_call_def:
param_data = runtime_call_def["encoder"](params)
if runtime is None:
runtime = await self.init_runtime(block_hash=block_hash)
param_data = runtime_call_def["encoder"](params, runtime.registry)
else:
for idx, param in enumerate(runtime_call_def["params"]):
param_type_string = f"{param['type']}"
Expand Down
3 changes: 2 additions & 1 deletion async_substrate_interface/sync_substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2541,7 +2541,8 @@ def _do_runtime_call_old(
param_data = b""

if "encoder" in runtime_call_def:
param_data = runtime_call_def["encoder"](params)
runtime = self.init_runtime(block_hash=block_hash)
param_data = runtime_call_def["encoder"](params, runtime.registry)
else:
for idx, param in enumerate(runtime_call_def["params"]):
param_type_string = f"{param['type']}"
Expand Down
77 changes: 71 additions & 6 deletions async_substrate_interface/type_registry.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union
from collections import namedtuple
from bt_decode import (
NeuronInfo,
NeuronInfoLite,
Expand All @@ -8,7 +10,54 @@
SubnetInfoV2,
encode,
)
from scalecodec import ss58_encode
from scalecodec import ss58_decode


def stake_info_decode_vec_legacy_compatibility(
item,
) -> list[dict[str, Union[str, int, bytes, bool]]]:
stake_infos: list[StakeInfo] = StakeInfo.decode_vec(item)
NewStakeInfo = namedtuple(
"NewStakeInfo",
[
"netuid",
"hotkey",
"coldkey",
"stake",
"locked",
"emission",
"drain",
"is_registered",
],
)
output = []
for stake_info in stake_infos:
output.append(
NewStakeInfo(
0,
stake_info.hotkey,
stake_info.coldkey,
stake_info.stake,
0,
0,
0,
False,
)
)
return output


def preprocess_get_stake_info_for_coldkeys(addrs):
output = []
if isinstance(addrs[0], list): # I think
for addr in addrs[0]:
output.append(list(bytes.fromhex(ss58_decode(addr))))
else:
if isinstance(addrs[0], dict):
for addr in addrs[0]["coldkey_accounts"]:
output.append(list(bytes.fromhex(ss58_decode(addr))))
return output


_TYPE_REGISTRY: dict[str, dict] = {
"types": {
Expand All @@ -24,7 +73,9 @@
"type": "Vec<u8>",
},
],
"encoder": lambda addr: encode(ss58_encode(addr), "Vec<u8>"),
"encoder": lambda addr, reg: encode(
"Vec<u8>", reg, list(bytes.fromhex(ss58_decode(addr)))
),
"type": "Vec<u8>",
"decoder": DelegateInfo.decode_delegated,
},
Expand Down Expand Up @@ -97,8 +148,20 @@
},
],
"type": "Vec<u8>",
"encoder": lambda addr: encode(ss58_encode(addr), "Vec<u8>"),
"decoder": StakeInfo.decode_vec,
"encoder": lambda addr, reg: encode(
"Vec<u8>",
reg,
list(
bytes.fromhex(
ss58_decode(
addr[0]
if isinstance(addr, list)
else addr["coldkey_account"]
)
)
),
),
"decoder": stake_info_decode_vec_legacy_compatibility,
},
"get_stake_info_for_coldkeys": {
"params": [
Expand All @@ -108,8 +171,10 @@
},
],
"type": "Vec<u8>",
"encoder": lambda addrs: encode(
[ss58_encode(addr) for addr in addrs], "Vec<Vec<u8>>"
"encoder": lambda addrs, reg: encode(
"Vec<Vec<u8>>",
reg,
preprocess_get_stake_info_for_coldkeys(addrs),
),
"decoder": StakeInfo.decode_vec_tuple_vec,
},
Expand Down
14 changes: 14 additions & 0 deletions tests/integration_tests/test_substrate_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,17 @@ def test_query_map_with_odd_number_of_params():
first_record = qm.records[0]
assert len(first_record) == 2
assert len(first_record[0]) == 4


def test_old_runtime_calls():
from bittensor import SubtensorApi

sub = SubtensorApi(network="archive", legacy_methods=True, async_subtensor=False)
# will pass
assert sub.get_stake_info_for_coldkey(
"5CQ6dMW8JZhKCZX9kWsZRqa3kZRKmNHxbPPVFEt6FgyvGv2G", 4943592
)
# needs to use legacy
assert sub.get_stake_info_for_coldkey(
"5CQ6dMW8JZhKCZX9kWsZRqa3kZRKmNHxbPPVFEt6FgyvGv2G", 4670227
)
Loading