Skip to content

Commit acad96a

Browse files
authored
[Feat] encode by string (#8)
* bump ver * add encode method * fix sequences * fix tuple encode * nump ver * use prerelease * add encode example to readme
1 parent 4b2a41d commit acad96a

7 files changed

+903
-6
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bt_decode"
3-
version = "0.1.0"
3+
version = "0.4.0-a0"
44
edition = "2021"
55

66
[lib]

README.md

+38
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,41 @@ neurons_lite: List[NeuronInfoLite] = bt_decode.decode(
288288
)
289289
)
290290
```
291+
292+
### encode by type string
293+
*Note: This feature is unstable, but working for multiple types.*
294+
295+
You may also encode using a type-string formed from existing types by passing the metadata as pulled from a node (or formed manually).
296+
```python
297+
import bittensor, bt_decode, scalecodec
298+
# Get subtensor connection
299+
sub = bittensor.subtensor()
300+
# Create a param for the RPC call, using v15 metadata
301+
v15_int = scalecodec.U32()
302+
v15_int.value = 15
303+
# Make the RPC call to grab the metadata
304+
metadata_rpc_result = sub.substrate.rpc_request("state_call", [
305+
"Metadata_metadata_at_version",
306+
v15_int.encode().to_hex(),
307+
sub.substrate.get_chain_finalised_head()
308+
])
309+
# Decode the metadata into a PortableRegistry type
310+
metadata_option_hex_str = metadata_rpc_result['result']
311+
metadata_option_bytes = bytes.fromhex(metadata_option_hex_str[2:])
312+
metadata_v15 = bt_decode.MetadataV15.decode_from_metadata_option(metadata_option_bytes)
313+
registry = bt_decode.PortableRegistry.from_metadata_v15( metadata_v15 )
314+
315+
316+
## Encode an integer as a compact u16
317+
compact_u16: list[int] = bt_decode.encode(
318+
"Compact<u16>", # type-string,
319+
2**16-1,
320+
registry
321+
)
322+
# [254, 255, 3, 0]
323+
compact_u16_py_scale_codec = scalecodec.Compact()
324+
compact_u16_py_scale_codec.value = 2**16-1
325+
compact_u16_py_scale_codec.encode()
326+
327+
assert list(compact_u16_py_scale_codec.data.data) == compact_u16
328+
```

bt_decode.pyi

+18
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,21 @@ def decode(
349349
type_string: str, portable_registry: PortableRegistry, encoded: bytes
350350
) -> Any:
351351
pass
352+
353+
def encode(
354+
type_string: str, portable_registry: PortableRegistry, to_encode: Any
355+
) -> list[int]:
356+
"""
357+
Encode a python object to bytes.
358+
359+
Returns a list of integers representing the encoded bytes.
360+
361+
Example:
362+
>>> import bittensor as bt
363+
>>> res = bt.decode.encode("u128", bt.decode.PortableRegistry.from_json(...), 1234567890)
364+
>>> res
365+
[210, 2, 150, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
366+
>>> bytes(res).hex()
367+
'd2029649000000000000000000000000'
368+
"""
369+
pass

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "bt-decode"
3-
version = "0.3.0a"
3+
version = "0.4.0-a0"
44
description = "A wrapper around the scale-codec crate for fast scale-decoding of Bittensor data structures."
55
readme = "README.md"
66
license = {file = "LICENSE"}

0 commit comments

Comments
 (0)