Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Add metadata v15 class #1

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ codec = { package = "parity-scale-codec", version = "3.2.2", default-features =
"derive",
] }
scale-info = { version = "2.11.2", default-features = false }
serde_json = { version = "1.0.127", default-features = false }
31 changes: 31 additions & 0 deletions bt_decode.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,34 @@ class DelegateInfo:
@staticmethod
def decode_delegated(encoded: bytes) -> List[Tuple["DelegateInfo", int]]:
pass

class MetadataV15:
"""
MetadataV15 is the 15th version-style of metadata for the chain.
It contains information about all the chain types, including the type signatures
of the Runtime API functions.

Example:
>>> 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_hex_str = metadata_rpc_result['result']
>>> metadata_bytes = bytes.fromhex(metadata_hex_str[2:])
>>> metadata_v15 = bt_decode.MetadataV15.decode_from_metadata(encoded_metadata)
>>> print(metadata_v15.to_json())
"""

@staticmethod
def decode_from_metadata_option(encoded_metadata_v15: bytes) -> "MetadataV15":
pass
def to_json(self) -> str:
"""
Returns a JSON representation of the metadata.
"""
pass
32 changes: 32 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use codec::{Decode, Encode};
use custom_derive::pydecode;
use serde_json;

use pyo3::prelude::*;

Expand All @@ -26,6 +27,8 @@ type AccountId = [u8; 32];

#[pymodule(name = "bt_decode")]
mod bt_decode {
use frame_metadata::v15::RuntimeMetadataV15;

use super::*;

#[pyclass(name = "AxonInfo", get_all)]
Expand Down Expand Up @@ -291,6 +294,35 @@ mod bt_decode {
}
}

#[pyclass(name = "MetadataV15")]
struct PyMetadataV15 {
metadata: RuntimeMetadataV15,
}

#[pymethods]
impl PyMetadataV15 {
fn to_json(&self) -> String {
serde_json::to_string(&self.metadata).unwrap().into()
}

#[staticmethod]
fn decode_from_metadata_option(encoded_metadata_v15: &[u8]) -> Self {
let option_vec = Option::<Vec<u8>>::decode(&mut &encoded_metadata_v15[..])
.ok()
.flatten()
.expect("Failed to Option metadata");

let metadata_v15 = RuntimeMetadataPrefixed::decode(&mut &option_vec[..])
.expect("Failed to decode metadata")
.1;

match metadata_v15 {
RuntimeMetadata::V15(metadata) => PyMetadataV15 { metadata },
_ => panic!("Invalid metadata version"),
}
}
}

// #[pyfunction(name = "decode")]
// fn py_decode(
// type_string: &str,
Expand Down