Structs
Putting together all of the basic types covered in the preceding sections, we can use Struct
to create more complex provable types.
Struct
is a generic function that takes a configuration of properties and types that returns a class with the same properties and types.
The struct is always provable, but the types in the configuration must also be provable.
class Vote extends Struct({
hasVoted: Bool,
inFavor: Bool,
}) {
static default() {
return new Vote({ hasVoted: Bool(false), inFavor: Bool(false) });
}
}
class Voter extends Struct({
publicKey: PublicKey,
vote: Vote,
}) {}
// Example usage
const voter = new Voter({
publicKey: PrivateKey.random().toPublicKey(),
vote: Vote.default(),
});
// set vote
voter.vote.hasVoted = Bool(true);
voter.vote.inFavor = Bool(true);
Read more at the language reference: Struct.