1

I'm trying to serialize a struct off chain so that I can pass it to a function as bytes32[length] that dynamically casts it to a struct (using assembly).

However looking at memory representation of structs in Remix, I don't "get it". How are they encoded?

I have looked at various ABI encoders but couldn't find any solution for structs.

  • I'm not sure if is possible to serialize structs to pass as function params, maybe because the functions params are in memory and structs is a user-defined type, serialization of params should respect builtin type – Eduardo Pereira Oct 24 '17 at 21:21
  • there is a function called mapAbiToStructFields() in accounts/abi/reflect.go , maybe you will find it useful – Nulik Sep 13 '18 at 18:16
  • is your input really that long , or is your input really so dynamic or is your input really so diverse ... to use serialization? – Nulik Sep 13 '18 at 18:18
1

You can take the struct elements and serialize them into a byte string. For eg.

struct Person{ string name; uint age; string gender; }

You can break this struct into 3 elements i.e. string name, uint age, string gender; and serialize these 3 elements into a byte string, and inside the function, you can deserialize the elements and convert it back to a struct object. You can follow this short article for serializing/deserializing different datatypes in solidity.

0

In-memory layout of structs is not specified in documentation, because it is not externally visible. It probably may be different in different compiler versions. Though it should be not too hard to reverse engineer it. Could you show your struct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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