Is it possible to encode a struct in Solidity without serializing each variable separately?
Something like:
struct MyStruct {
uint x;
uint y;
}
MyStruct storage myStruct = MyStruct({
x: 11,
y: 12
});
bytes memory data = abi.encode(myStruct, (MyStruct));
Or in assembly?
Best Answer
If you just want to store this on your contract, solidity structs are already stored encoded in storage.
Just define a data member as struct and assign values, it will be properly encoded.
In case you want to send this as a function parameter, you can just pass the struct (if that's the input type) and it will also be encoded properly.