It's possible to get the metadata information using the state_getMetadata
RPC method. Then I want to reconstruct the corresponding rust types described in the metadata json type registry and decode the runtime state content based on the parsed type. Since the type registry has implemented JsonSchema, I tried to reconstruct using quicktype, but no luck. Any other ideas?
1 Answer
In Rust, it is not possible to create a struct at runtime due to the absence of a reflection system.
Currently, the most convenient approach is employed in subxt
. It involves fetching the metadata first and constructing those types statically before embedding them into the compiling code.
For further reading on this topic, you may refer to:
- https://stackoverflow.com/questions/47159418/is-it-possible-to-define-structs-at-runtime-or-otherwise-achieve-a-similar-effec
- https://www.reddit.com/r/rust/comments/9wr5wi/crazy_idea_is_possible_to_build_structs/
- https://github.com/paritytech/subxt?tab=readme-ov-file#downloading-metadata-from-a-substrate-node
-
I'd also just add that if you do want to be able to decode SCALE bytes (with a known type ID + metadata) at runtime, then github.com/paritytech/scale-value is useful; it's sortof the SCALE equivalent of
serde_json::Value
:)– jsdwSep 7 at 10:50