Skip to content

Commit

Permalink
update readme with decode func (#5)
Browse files Browse the repository at this point in the history
* update readme with decode func

* fix name of get_neuron_lite
  • Loading branch information
camfairchild authored Oct 15, 2024
1 parent 47cb074 commit 0a92c3c
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ neurons: List[NeuronInfo] = NeuronInfo.decode_vec(
```

### NeuronInfoLite
#### get_neuron
#### get_neuron_lite
```python
import bittensor
from bt_decode import NeuronInfoLite
Expand Down Expand Up @@ -247,3 +247,44 @@ subnet_hyper_params: Optional[SubnetHyperparameters] = SubnetHyperparameters.dec
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).
```python
import bittensor, bt_decode, scalecodec
# Get subtensor connection
sub = bittensor.subtensor()
# Create a param for the RPC call, using v15 metadata
v15_int = scalecodec.U32()
v15_int.value = 15
# Make the RPC call to grab the metadata
metadata_rpc_result = sub.substrate.rpc_request("state_call", [
"Metadata_metadata_at_version",
v15_int.encode().to_hex(),
sub.substrate.get_chain_finalised_head()
])
# Decode the metadata into a PortableRegistry type
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 )

# Decode by type-string
NETUID = 1
## Grab result from RuntimeAPI
hex_bytes_result = sub.query_runtime_api(
runtime_api="NeuronInfoRuntimeApi",
method="get_neurons_lite",
params=[NETUID]
)
## Decode scale-encoded NeuronInfoLite by type-string
neurons_lite: List[NeuronInfoLite] = bt_decode.decode(
"Vec<NeuronInfoLite>", # type-string
registry, # registry as above
bytes.fromhex(
hex_bytes_result # bytes to decode
)
)
```

0 comments on commit 0a92c3c

Please sign in to comment.