4

How can I decode an extrinsic or event dynamically in rust? (I want to be able to decode across all parachains)

A lot of the heavy lifting seems to be frame-metadata and scale-info. There's a desub project that seems like it ought to be able to do this. I've managed to get a &Type<PortableForm> based on the v14 metadata but I can't quite see how I can decode it (or alternatively decode it into a JsonValue).

Hmm will try this for extrinsics. Not sure on events?

2
  • Seems that desub requires a byte slice but the extrinsics from a block seem to be OpaqueExtrinsics that are not so keen on showing their bytes...
    – Squirrel
    Apr 28, 2022 at 8:07
  • 1
    OpaqueExtrinsic is Encode, so it's easy enough to get a byte slice. IIRC extrinsics are typically stored double-encoded (to avoid prescience of a chain's transaction format in lower-level code), so dpending on how desub works you may need to pass it an encoded byte slice. e.g. if you have xt: OpaqueExtrinsic, and you want a byte slice you would do either: &xt.encode()[..] or &xt.encode().encode()[..].
    – Gav
    Apr 28, 2022 at 8:58

1 Answer 1

4

Regarding dynamic event decoding, this is something that cargo-contract does. It is not easily reusable at the moment but feel free to copy the code and use it for inspiration. Start here.

The other alternative at the moment would be to add that functionality to desub, which should be relatively straightforward since it already implements the underlying dynamic type decoding.

Eventually there should be a unified crate for dynamically decoding events, possibly as part of subxt.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.