How do you make a custom struct Serializable?

As the title states, how do you make a custom struct in Sway serializable? The motivation behind this question is because I am using the struct in rust and want to serialize the struct and pass it as a params to the contract function for testing purposes.

To encode the custom struct (generated by abigen!) you can either use the call_data! macro:

    let call_data = calldata!(
            MyStruct {
                field: 101,
           }
        )?;

or use ABIEncoder directly:

        let instance = MyStruct { field: 101 };
        let encoded: Vec<u8> = ABIEncoder::default().encode(&[instance.into_token()])?.resolve(0);

This will give you a Vec<u8> which you can pass as the contract input.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.