Skip to content

Commit

Permalink
Governance: Fix MultiChoice VoteType serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaloup committed Oct 26, 2022
1 parent 3bbb3cc commit 392cbe2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 4 additions & 2 deletions packages/governance-sdk/src/governance/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,13 @@ export enum VoteTypeKind {

export class VoteType {
type: VoteTypeKind;
choiceCount: number | undefined;
max_voter_options: number | undefined;
max_winning_options: number | undefined;

constructor(args: { type: VoteTypeKind; choiceCount: number | undefined }) {
this.type = args.type;
this.choiceCount = args.choiceCount;
this.max_voter_options = args.choiceCount;
this.max_winning_options = args.choiceCount;
}

static SINGLE_CHOICE = new VoteType({
Expand Down
10 changes: 7 additions & 3 deletions packages/governance-sdk/src/governance/serialisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ import { deserializeBorsh } from '../tools/borsh';
return VoteType.SINGLE_CHOICE;
}

const choiceCount = reader.buf.readUInt16LE(reader.offset);
const choiceCount = reader.buf.readUInt8(reader.offset);
reader.offset += 2; // skip two bytes
return VoteType.MULTI_CHOICE(choiceCount);
};

Expand All @@ -111,8 +112,11 @@ import { deserializeBorsh } from '../tools/borsh';
writer.length += 1;

if (value.type === VoteTypeKind.MultiChoice) {
writer.buf.writeUInt16LE(value.choiceCount!, writer.length);
writer.length += 2;
// max_voter_options and max_winning_options are not implemented at backend
writer.buf.writeUInt8(value.max_voter_options || 0, writer.length);
writer.length += 1;
writer.buf.writeUInt8(value.max_winning_options || 0, writer.length);
writer.length += 1;
}
};

Expand Down

0 comments on commit 392cbe2

Please sign in to comment.