bt-decode
A python wrapper around the rust scale-codec crate for fast scale-decoding of Bittensor data structures.
Usage
DelegateInfo
get_delegates
import bittensor
from bt_decode import DelegateInfo
subtensor = bittensor.subtensor()
hex_bytes_result = sub.query_runtime_api(
runtime_api="DelegateInfoRuntimeApi",
method="get_delegates",
params=[ ]
)
delegates_info: List[DelegateInfo] = DelegateInfo.decode_vec(
bytes.fromhex(
hex_bytes_result
))
get_delegated
import bittensor
from bt_decode import DelegateInfo
validator_key = bittensor.Keypair(ss58_address="5E9fVY1jexCNVMjd2rdBsAxeamFGEMfzHcyTn2fHgdHeYc5p")
subtensor = bittensor.subtensor()
hex_bytes_result = sub.query_runtime_api(
runtime_api="DelegateInfoRuntimeApi",
method="get_delegated",
params=[list( validator_key.public_key )]
)
delegated_info: List[Tuple[DelegateInfo, int]] = DelegateInfo.decode_delegated(
bytes.fromhex(
hex_bytes_result
))
NeuronInfo
get_neuron
import bittensor
from bt_decode import NeuronInfo
subtensor = bittensor.subtensor()
NETUID = 1
UID = 0
hex_bytes_result = sub.query_runtime_api(
runtime_api="NeuronInfoRuntimeApi",
method="get_neuron",
params=[NETUID, UID]
)
neuron: NeuronInfo = NeuronInfo.decode(
bytes.fromhex(
hex_bytes_result
))
get_neurons
import bittensor
from bt_decode import NeuronInfo
subtensor = bittensor.subtensor()
NETUID = 1
hex_bytes_result = sub.query_runtime_api(
runtime_api="NeuronInfoRuntimeApi",
method="get_neurons",
params=[NETUID]
)
neurons: List[NeuronInfo] = NeuronInfo.decode_vec(
bytes.fromhex(
hex_bytes_result
))
NeuronInfoLite
get_neuron_lite
import bittensor
from bt_decode import NeuronInfoLite
subtensor = bittensor.subtensor()
NETUID = 1
UID = 0
hex_bytes_result = sub.query_runtime_api(
runtime_api="NeuronInfoRuntimeApi",
method="get_neuron_lite",
params=[NETUID, UID]
)
neuron_lite: NeuronInfoLite = NeuronInfoLite.decode(
bytes.fromhex(
hex_bytes_result
))
get_neurons_lite
import bittensor
from bt_decode import NeuronInfoLite
subtensor = bittensor.subtensor()
NETUID = 1
hex_bytes_result = sub.query_runtime_api(
runtime_api="NeuronInfoRuntimeApi",
method="get_neurons_lite",
params=[NETUID]
)
neurons_lite: List[NeuronInfoLite] = NeuronInfoLite.decode_vec(
bytes.fromhex(
hex_bytes_result
))
StakeInfo
get_stake_info_for_coldkey
import bittensor
from bt_decode import StakeInfo
validator_key = bittensor.Keypair(ss58_address="5HBtpwxuGNL1gwzwomwR7sjwUt8WXYSuWcLYN6f9KpTZkP4k")
subtensor = bittensor.subtensor()
encoded_coldkey = list( validator_key.public_key )
hex_bytes_result = sub.query_runtime_api(
runtime_api="StakeInfoRuntimeApi",
method="get_stake_info_for_coldkey",
params=[encoded_coldkey]
)
stake_info: List[StakeInfo] = StakeInfo.decode_vec(
bytes.fromhex(
hex_bytes_result
))
get_stake_info_for_coldkeys
import bittensor
from bt_decode import StakeInfo
validator_key_0 = bittensor.Keypair(ss58_address="5GcCZ2BPXBjgG88tXJCEtkbdg2hNrPbL4EFfbiVRvBZdSQDC")
validator_key_1 = bittensor.Keypair(ss58_address="5HBtpwxuGNL1gwzwomwR7sjwUt8WXYSuWcLYN6f9KpTZkP4k")
encoded_coldkeys = [
list( validator_key_0.public_key ),
list( validator_key_1.public_key )
]
subtensor = bittensor.subtensor()
hex_bytes_result = sub.query_runtime_api(
runtime_api="StakeInfoRuntimeApi",
method="get_stake_info_for_coldkeys",
params=[encoded_coldkeys]
)
stake_info: List[Tuple[bytes, List["StakeInfo"]]] = StakeInfo.decode_vec_tuple_vec(
bytes.fromhex(
hex_bytes_result
))
SubnetInfo
get_subnet_info
import bittensor
from bt_decode import SubnetInfo
subtensor = bittensor.subtensor()
NETUID = 1
hex_bytes_result = sub.query_runtime_api(
runtime_api="SubnetInfoRuntimeApi",
method="get_subnet_info",
params=[NETUID]
)
subnet_info: SubnetInfo = SubnetInfo.decode_option(
bytes.fromhex(
hex_bytes_result
))
get_subnets_info
import bittensor
from bt_decode import SubnetInfo
subtensor = bittensor.subtensor()
hex_bytes_result = sub.query_runtime_api(
runtime_api="SubnetInfoRuntimeApi",
method="get_subnets_info",
params=[ ]
)
subnets_info: List[Optional[SubnetInfo]] = SubnetInfo.decode_vec_option(
bytes.fromhex(
hex_bytes_result
))
SubnetHyperparameters
get_subnet_hyperparams
import bittensor
from bt_decode import SubnetHyperparameters
subtensor = bittensor.subtensor()
NETUID = 1
hex_bytes_result = sub.query_runtime_api(
runtime_api="SubnetInfoRuntimeApi",
method="get_subnet_hyperparams",
params=[NETUID]
)
subnet_hyper_params: Optional[SubnetHyperparameters] = SubnetHyperparameters.decode_option(
bytes.fromhex(
hex_bytes_result
))
decode by type string
Note: This feature is unstable, but working for multiple types.
You may also decode using a type-string formed from existing types by passing the metadata as pulled from a node (or formed manually).
import bittensor, bt_decode, scalecodec
sub = bittensor.subtensor()
v15_int = scalecodec.U32()
v15_int.value = 15
metadata_rpc_result = sub.substrate.rpc_request("state_call", [
"Metadata_metadata_at_version",
v15_int.encode().to_hex(),
sub.substrate.get_chain_finalised_head()
])
metadata_option_hex_str = metadata_rpc_result['result']
metadata_option_bytes = bytes.fromhex(metadata_option_hex_str[2:])
metadata_v15 = bt_decode.MetadataV15.decode_from_metadata_option(metadata_option_bytes)
registry = bt_decode.PortableRegistry.from_metadata_v15( metadata_v15 )
NETUID = 1
hex_bytes_result = sub.query_runtime_api(
runtime_api="NeuronInfoRuntimeApi",
method="get_neurons_lite",
params=[NETUID]
)
neurons_lite: List[NeuronInfoLite] = bt_decode.decode(
"Vec<NeuronInfoLite>",
registry,
bytes.fromhex(
hex_bytes_result
)
)
encode by type string
Note: This feature is unstable, but working for multiple types.
You may also encode using a type-string formed from existing types by passing the metadata as pulled from a node (or formed manually).
import bittensor, bt_decode, scalecodec
sub = bittensor.subtensor()
v15_int = scalecodec.U32()
v15_int.value = 15
metadata_rpc_result = sub.substrate.rpc_request("state_call", [
"Metadata_metadata_at_version",
v15_int.encode().to_hex(),
sub.substrate.get_chain_finalised_head()
])
metadata_option_hex_str = metadata_rpc_result['result']
metadata_option_bytes = bytes.fromhex(metadata_option_hex_str[2:])
metadata_v15 = bt_decode.MetadataV15.decode_from_metadata_option(metadata_option_bytes)
registry = bt_decode.PortableRegistry.from_metadata_v15( metadata_v15 )
compact_u16: list[int] = bt_decode.encode(
"Compact<u16>",
2**16-1,
registry
)
compact_u16_py_scale_codec = scalecodec.Compact()
compact_u16_py_scale_codec.value = 2**16-1
compact_u16_py_scale_codec.encode()
assert list(compact_u16_py_scale_codec.data.data) == compact_u16