How do you make a custom struct Serializable?

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