Skip to content

Commit 0a92c3c

Browse files
authored
update readme with decode func (#5)
* update readme with decode func * fix name of get_neuron_lite
1 parent 47cb074 commit 0a92c3c

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

README.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ neurons: List[NeuronInfo] = NeuronInfo.decode_vec(
9090
```
9191

9292
### NeuronInfoLite
93-
#### get_neuron
93+
#### get_neuron_lite
9494
```python
9595
import bittensor
9696
from bt_decode import NeuronInfoLite
@@ -247,3 +247,44 @@ subnet_hyper_params: Optional[SubnetHyperparameters] = SubnetHyperparameters.dec
247247
hex_bytes_result
248248
))
249249
```
250+
251+
### decode by type string
252+
*Note: This feature is unstable, but working for multiple types.*
253+
254+
You may also decode using a type-string formed from existing types by passing the metadata as pulled from a node (or formed manually).
255+
```python
256+
import bittensor, bt_decode, scalecodec
257+
# Get subtensor connection
258+
sub = bittensor.subtensor()
259+
# Create a param for the RPC call, using v15 metadata
260+
v15_int = scalecodec.U32()
261+
v15_int.value = 15
262+
# Make the RPC call to grab the metadata
263+
metadata_rpc_result = sub.substrate.rpc_request("state_call", [
264+
"Metadata_metadata_at_version",
265+
v15_int.encode().to_hex(),
266+
sub.substrate.get_chain_finalised_head()
267+
])
268+
# Decode the metadata into a PortableRegistry type
269+
metadata_option_hex_str = metadata_rpc_result['result']
270+
metadata_option_bytes = bytes.fromhex(metadata_option_hex_str[2:])
271+
metadata_v15 = bt_decode.MetadataV15.decode_from_metadata_option(metadata_option_bytes)
272+
registry = bt_decode.PortableRegistry.from_metadata_v15( metadata_v15 )
273+
274+
# Decode by type-string
275+
NETUID = 1
276+
## Grab result from RuntimeAPI
277+
hex_bytes_result = sub.query_runtime_api(
278+
runtime_api="NeuronInfoRuntimeApi",
279+
method="get_neurons_lite",
280+
params=[NETUID]
281+
)
282+
## Decode scale-encoded NeuronInfoLite by type-string
283+
neurons_lite: List[NeuronInfoLite] = bt_decode.decode(
284+
"Vec<NeuronInfoLite>", # type-string
285+
registry, # registry as above
286+
bytes.fromhex(
287+
hex_bytes_result # bytes to decode
288+
)
289+
)
290+
```

0 commit comments

Comments
 (0)