Skip to content

Macro system for standardizing wasm attribute emission/parsing #1928

Open
@0xForerunner

Description

@0xForerunner

So I was recently banging my head against the wall trying to parse wasm attributes and I came up with an idea for making this process painless and convenient. It looks something like this right now but I'd be open to suggestions.

#[derive(Attrs)]
#[cw_serde]
pub enum ExecuteMsg {
    #[attrs = "HelloAttrs"]
    Hello{},
    #[attrs = "WorldAttrs"]
    World{}
}

#[derive(MsgAttr)]
pub struct HelloAttrs {
    /// The action field should be mandatory and unique so parsing 
    /// any message can be fully automated. Perhaps the action field shouldn't even be part of the struct
    /// and just assumed to be present in the attributes.
    action: String, 
    sender: Addr // <-- fields must be `Serialize + Deserialize`
}

MsgAttr derives From<Vec<Attribute>> and Into<Vec<Attribute>> to make parsing extremely easy, and

Then in the smart contract we can have

let res = Response::new().with_attributes(HelloAttrs{
    action: "hello".to_string(),
    sender: "foo bar baz".to_string()
});

The Attrs macro the creates a new enum that looks like this:

pub enum ExecuteMsgAttrs {
    Hello(HelloAttrs),
    World(WorldAttrs),
}

and we can access to a parsing function like this:

pub fn parse_attrs(attrs: Vec<Attribute>) -> Option<ExecuteMsgAttrs>

This could be made a little more robust by including wasm events as well but I haven't really thought that far ahead. Let me know what you think!

Metadata

Metadata

Assignees

No one assigned

    Labels

    schemacosmwasm-schema crate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions