{"id":20324125,"url":"https://github.com/opentensor/bt-decode","last_synced_at":"2025-04-11T19:40:43.011Z","repository":{"id":255417047,"uuid":"848953507","full_name":"opentensor/bt-decode","owner":"opentensor","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-31T21:00:43.000Z","size":1914,"stargazers_count":0,"open_issues_count":3,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-31T22:21:35.552Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/opentensor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-28T17:53:34.000Z","updated_at":"2025-03-18T23:27:59.000Z","dependencies_parsed_at":"2024-08-29T22:07:44.847Z","dependency_job_id":"1f154fe9-8935-4445-b9df-b20f0ee890f7","html_url":"https://github.com/opentensor/bt-decode","commit_stats":null,"previous_names":["opentensor/bt-decode"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentensor%2Fbt-decode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentensor%2Fbt-decode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentensor%2Fbt-decode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentensor%2Fbt-decode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/opentensor","download_url":"https://codeload.github.com/opentensor/bt-decode/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248468237,"owners_count":21108784,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T19:32:25.429Z","updated_at":"2025-04-11T19:40:43.000Z","avatar_url":"https://github.com/opentensor.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bt-decode\nA python wrapper around the rust scale-codec crate for fast scale-decoding of Bittensor data structures.\n\n## Usage\n\n### DelegateInfo\n#### get_delegates\n```python\nimport bittensor\nfrom bt_decode import DelegateInfo\n\n# Setup subtensor connection\nsubtensor = bittensor.subtensor()\n# Grab result from RuntimeAPI\nhex_bytes_result = sub.query_runtime_api(\n    runtime_api=\"DelegateInfoRuntimeApi\",\n    method=\"get_delegates\",\n    params=[ ]\n)\n# Decode scale-encoded Vec\u003cDelegateInfo\u003e\ndelegates_info: List[DelegateInfo] = DelegateInfo.decode_vec(\n    bytes.fromhex(\n        hex_bytes_result\n))\n```\n#### get_delegated\n```python\nimport bittensor\nfrom bt_decode import DelegateInfo\n\nvalidator_key = bittensor.Keypair(ss58_address=\"5E9fVY1jexCNVMjd2rdBsAxeamFGEMfzHcyTn2fHgdHeYc5p\")\n\n# Setup subtensor connection\nsubtensor = bittensor.subtensor()\n# Grab result from RuntimeAPI\nhex_bytes_result = sub.query_runtime_api(\n    runtime_api=\"DelegateInfoRuntimeApi\",\n    method=\"get_delegated\",\n    params=[list( validator_key.public_key )]\n)\n# Decode scale-encoded Vec\u003c(DelegateInfo, take)\u003e\ndelegated_info: List[Tuple[DelegateInfo, int]] = DelegateInfo.decode_delegated(\n    bytes.fromhex(\n        hex_bytes_result\n))\n```\n\n### NeuronInfo\n#### get_neuron\n```python\nimport bittensor\nfrom bt_decode import NeuronInfo\n\n# Setup subtensor connection\nsubtensor = bittensor.subtensor()\nNETUID = 1\nUID = 0\n# Grab result from RuntimeAPI\nhex_bytes_result = sub.query_runtime_api(\n    runtime_api=\"NeuronInfoRuntimeApi\",\n    method=\"get_neuron\",\n    params=[NETUID, UID]\n)\n# Decode scale-encoded NeuronInfo\nneuron: NeuronInfo = NeuronInfo.decode(\n    bytes.fromhex(\n        hex_bytes_result\n))\n```\n\n#### get_neurons\n```python\nimport bittensor\nfrom bt_decode import NeuronInfo\n\n# Setup subtensor connection\nsubtensor = bittensor.subtensor()\nNETUID = 1\n# Grab result from RuntimeAPI\nhex_bytes_result = sub.query_runtime_api(\n    runtime_api=\"NeuronInfoRuntimeApi\",\n    method=\"get_neurons\",\n    params=[NETUID]\n)\n# Decode scale-encoded Vec\u003cNeuronInfo\u003e\nneurons: List[NeuronInfo] = NeuronInfo.decode_vec(\n    bytes.fromhex(\n        hex_bytes_result\n))\n```\n\n### NeuronInfoLite\n#### get_neuron_lite\n```python\nimport bittensor\nfrom bt_decode import NeuronInfoLite\n\n# Setup subtensor connection\nsubtensor = bittensor.subtensor()\nNETUID = 1\nUID = 0\n# Grab result from RuntimeAPI\nhex_bytes_result = sub.query_runtime_api(\n    runtime_api=\"NeuronInfoRuntimeApi\",\n    method=\"get_neuron_lite\",\n    params=[NETUID, UID]\n)\n# Decode scale-encoded NeuronInfoLite\nneuron_lite: NeuronInfoLite = NeuronInfoLite.decode(\n    bytes.fromhex(\n        hex_bytes_result\n))\n```\n\n#### get_neurons_lite\n```python\nimport bittensor\nfrom bt_decode import NeuronInfoLite\n\n# Setup subtensor connection\nsubtensor = bittensor.subtensor()\nNETUID = 1\n# Grab result from RuntimeAPI\nhex_bytes_result = sub.query_runtime_api(\n    runtime_api=\"NeuronInfoRuntimeApi\",\n    method=\"get_neurons_lite\",\n    params=[NETUID]\n)\n# Decode scale-encoded Vec\u003cNeuronInfoLite\u003e\nneurons_lite: List[NeuronInfoLite] = NeuronInfoLite.decode_vec(\n    bytes.fromhex(\n        hex_bytes_result\n))\n```\n\n### StakeInfo\n#### get_stake_info_for_coldkey\n```python\nimport bittensor\nfrom bt_decode import StakeInfo\n\nvalidator_key = bittensor.Keypair(ss58_address=\"5HBtpwxuGNL1gwzwomwR7sjwUt8WXYSuWcLYN6f9KpTZkP4k\")\n\n# Setup subtensor connection\nsubtensor = bittensor.subtensor()\nencoded_coldkey = list( validator_key.public_key )\n# Grab result from RuntimeAPI\nhex_bytes_result = sub.query_runtime_api(\n    runtime_api=\"StakeInfoRuntimeApi\",\n    method=\"get_stake_info_for_coldkey\",\n    params=[encoded_coldkey]\n)\n# Decode scale-encoded StakeInfo\nstake_info: List[StakeInfo] = StakeInfo.decode_vec(\n    bytes.fromhex(\n        hex_bytes_result\n))\n```\n\n#### get_stake_info_for_coldkeys\n```python\nimport bittensor\nfrom bt_decode import StakeInfo\n\nvalidator_key_0 = bittensor.Keypair(ss58_address=\"5GcCZ2BPXBjgG88tXJCEtkbdg2hNrPbL4EFfbiVRvBZdSQDC\")\nvalidator_key_1 = bittensor.Keypair(ss58_address=\"5HBtpwxuGNL1gwzwomwR7sjwUt8WXYSuWcLYN6f9KpTZkP4k\")\n\nencoded_coldkeys = [\n    list( validator_key_0.public_key ),\n    list( validator_key_1.public_key )\n]\n\n# Setup subtensor connection\nsubtensor = bittensor.subtensor()\n# Grab result from RuntimeAPI\nhex_bytes_result = sub.query_runtime_api(\n    runtime_api=\"StakeInfoRuntimeApi\",\n    method=\"get_stake_info_for_coldkeys\",\n    params=[encoded_coldkeys]\n)\n# Decode scale-encoded Vec\u003c(AccountId, StakeInfo)\u003e\nstake_info: List[Tuple[bytes, List[\"StakeInfo\"]]] = StakeInfo.decode_vec_tuple_vec(\n    bytes.fromhex(\n        hex_bytes_result\n))\n```\n### SubnetInfo\n#### get_subnet_info\n```python\nimport bittensor\nfrom bt_decode import SubnetInfo\n\n# Setup subtensor connection\nsubtensor = bittensor.subtensor()\nNETUID = 1\n# Grab result from RuntimeAPI\nhex_bytes_result = sub.query_runtime_api(\n    runtime_api=\"SubnetInfoRuntimeApi\",\n    method=\"get_subnet_info\",\n    params=[NETUID]\n)\n# Decode scale-encoded Option\u003cSubnetInfo\u003e\nsubnet_info: SubnetInfo = SubnetInfo.decode_option(\n    bytes.fromhex(\n        hex_bytes_result\n))\n```\n\n#### get_subnets_info\n```python\nimport bittensor\nfrom bt_decode import SubnetInfo\n\n# Setup subtensor connection\nsubtensor = bittensor.subtensor()\n# Grab result from RuntimeAPI\nhex_bytes_result = sub.query_runtime_api(\n    runtime_api=\"SubnetInfoRuntimeApi\",\n    method=\"get_subnets_info\",\n    params=[ ]\n)\n# Decode scale-encoded Vec\u003cOption\u003cSubnetInfo\u003e\u003e\nsubnets_info: List[Optional[SubnetInfo]] = SubnetInfo.decode_vec_option(\n    bytes.fromhex(\n        hex_bytes_result\n))\n```\n\n### SubnetHyperparameters\n#### get_subnet_hyperparams\n```python\nimport bittensor\nfrom bt_decode import SubnetHyperparameters\n\n# Setup subtensor connection\nsubtensor = bittensor.subtensor()\nNETUID = 1\n# Grab result from RuntimeAPI\nhex_bytes_result = sub.query_runtime_api(\n    runtime_api=\"SubnetInfoRuntimeApi\",\n    method=\"get_subnet_hyperparams\",\n    params=[NETUID]\n)\n# Decode scale-encoded Option\u003cSubnetHyperparameters\u003e\nsubnet_hyper_params: Optional[SubnetHyperparameters] = SubnetHyperparameters.decode_option(\n    bytes.fromhex(\n        hex_bytes_result\n))\n```\n\n### decode by type string\n*Note: This feature is unstable, but working for multiple types.*\n\nYou may also decode using a type-string formed from existing types by passing the metadata as pulled from a node (or formed manually).\n```python\nimport bittensor, bt_decode, scalecodec\n# Get subtensor connection\nsub = bittensor.subtensor()\n# Create a param for the RPC call, using v15 metadata\nv15_int = scalecodec.U32()\nv15_int.value = 15\n# Make the RPC call to grab the metadata\nmetadata_rpc_result = sub.substrate.rpc_request(\"state_call\", [\n    \"Metadata_metadata_at_version\",\n    v15_int.encode().to_hex(),\n    sub.substrate.get_chain_finalised_head()\n])\n# Decode the metadata into a PortableRegistry type\nmetadata_option_hex_str = metadata_rpc_result['result']\nmetadata_option_bytes = bytes.fromhex(metadata_option_hex_str[2:])\nmetadata_v15 = bt_decode.MetadataV15.decode_from_metadata_option(metadata_option_bytes)\nregistry = bt_decode.PortableRegistry.from_metadata_v15( metadata_v15 )\n\n# Decode by type-string\nNETUID = 1\n## Grab result from RuntimeAPI\nhex_bytes_result = sub.query_runtime_api(\n    runtime_api=\"NeuronInfoRuntimeApi\",\n    method=\"get_neurons_lite\",\n    params=[NETUID]\n)\n## Decode scale-encoded NeuronInfoLite by type-string\nneurons_lite: List[NeuronInfoLite] = bt_decode.decode(\n    \"Vec\u003cNeuronInfoLite\u003e\", # type-string\n    registry, # registry as above\n    bytes.fromhex(\n        hex_bytes_result # bytes to decode\n    )\n)\n```\n\n### encode by type string\n*Note: This feature is unstable, but working for multiple types.*\n\nYou may also encode using a type-string formed from existing types by passing the metadata as pulled from a node (or formed manually).\n```python\nimport bittensor, bt_decode, scalecodec\n# Get subtensor connection\nsub = bittensor.subtensor()\n# Create a param for the RPC call, using v15 metadata\nv15_int = scalecodec.U32()\nv15_int.value = 15\n# Make the RPC call to grab the metadata\nmetadata_rpc_result = sub.substrate.rpc_request(\"state_call\", [\n    \"Metadata_metadata_at_version\",\n    v15_int.encode().to_hex(),\n    sub.substrate.get_chain_finalised_head()\n])\n# Decode the metadata into a PortableRegistry type\nmetadata_option_hex_str = metadata_rpc_result['result']\nmetadata_option_bytes = bytes.fromhex(metadata_option_hex_str[2:])\nmetadata_v15 = bt_decode.MetadataV15.decode_from_metadata_option(metadata_option_bytes)\nregistry = bt_decode.PortableRegistry.from_metadata_v15( metadata_v15 )\n\n\n## Encode an integer as a compact u16\ncompact_u16: list[int] = bt_decode.encode(\n    \"Compact\u003cu16\u003e\", # type-string,\n    2**16-1,\n    registry\n)\n# [254, 255, 3, 0]\ncompact_u16_py_scale_codec = scalecodec.Compact()\ncompact_u16_py_scale_codec.value = 2**16-1\ncompact_u16_py_scale_codec.encode()\n\nassert list(compact_u16_py_scale_codec.data.data) == compact_u16\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopentensor%2Fbt-decode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopentensor%2Fbt-decode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopentensor%2Fbt-decode/lists"}