Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

fixed vote options serialization #560

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/governance-sdk/src/governance/serialisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ import { deserializeBorsh } from '../tools/borsh';
writer.length += 1;

if (value.type === VoteTypeKind.MultiChoice) {
writer.buf.writeUInt16LE(value.choiceCount!, writer.length);
writer.length += 2;
writer.buf.writeUInt8(value.choiceCount!, writer.length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writer.length += 1;
writer.buf.writeUInt8(value.choiceCount!, writer.length);
writer.length += 1;
}
};

Expand Down
78 changes: 78 additions & 0 deletions packages/governance-sdk/tests/governance/api.v4.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Keypair } from '@solana/web3.js';
import { GoverningTokenConfigAccountArgs, GoverningTokenType } from '../../src';
import { BenchBuilder } from '../tools/builders';

test('setRealmConfig', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests should be in V2.V3 because they should work for spl-gov V2 and V3 and there is no V4 yet

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between this test and the one which already exists?

// Arrange
const realm = await BenchBuilder.withConnection()
.then(b => b.withWallet())
.then(b => b.withRealm())
.then(b => b.sendTx());

const communityTokenConfig = new GoverningTokenConfigAccountArgs({
voterWeightAddin: Keypair.generate().publicKey,
maxVoterWeightAddin: Keypair.generate().publicKey,
tokenType: GoverningTokenType.Liquid,
});

// Act
await realm.setRealmConfig(communityTokenConfig);

// Assert
const realmConfig = await realm.getRealmConfig();

expect(realmConfig.account.realm).toEqual(realm.realmPk);

// communityTokenConfig
expect(realmConfig.account.communityTokenConfig.tokenType).toEqual(
communityTokenConfig.tokenType,
);
expect(realmConfig.account.communityTokenConfig.voterWeightAddin).toEqual(
communityTokenConfig.voterWeightAddin,
);
expect(realmConfig.account.communityTokenConfig.maxVoterWeightAddin).toEqual(
communityTokenConfig.maxVoterWeightAddin,
);

// councilTokenConfig
expect(realmConfig.account.councilTokenConfig.tokenType).toEqual(
GoverningTokenType.Liquid,
);
expect(realmConfig.account.councilTokenConfig.voterWeightAddin).toEqual(
undefined,
);
expect(realmConfig.account.councilTokenConfig.maxVoterWeightAddin).toEqual(
undefined,
);
});

test('createGovernance', async () => {
// Arrange
const realm = await BenchBuilder.withConnection()
.then(b => b.withWallet())
.then(b => b.withRealm())
.then(b => b.withCommunityMember())
.then(b => b.sendTx());

// Act
const governancePk = await realm.createGovernance();

// // Assert
const governance = await realm.getGovernance(governancePk);

expect(governance.account.realm).toEqual(realm.realmPk);
});

test('createProposal', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the test name more specific

// Arrange
const realm = await BenchBuilder.withConnection()
.then(b => b.withWallet())
.then(b => b.withRealm())
.then(b => b.withCommunityMember())
.then(b => b.withGovernance())
.then(b => b.sendTx());

// Act
const proposalPk = await realm.createProposal('proposal 1', true);

});
19 changes: 12 additions & 7 deletions packages/governance-sdk/tests/tools/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,17 +379,23 @@ export class RealmBuilder {
return this;
}

async createProposal(name?: string) {
const proposalPk = await this._createProposal(name);
async createProposal(name?: string, multiple?: boolean) {
const proposalPk = await this._createProposal(name, multiple);
await this.sendTx();
return proposalPk;
}

async _createProposal(name?: string) {
async _createProposal(name?: string, multiple?: boolean) {
// Create single choice Approve/Deny proposal with instruction to mint more governance tokens
const voteType = VoteType.SINGLE_CHOICE;
const options = ['Approve'];
const useDenyOption = true;
let voteType = VoteType.SINGLE_CHOICE;
let options = ['Approve'];
let useDenyOption = true;

if (multiple) {
voteType = VoteType.MULTI_CHOICE(4);
options = ['One', 'Two', 'Three', 'four']
useDenyOption = false
}

this.proposalPk = await withCreateProposal(
this.bench.instructions,
Expand All @@ -408,7 +414,6 @@ export class RealmBuilder {
useDenyOption,
this.bench.walletPk,
);

return this.proposalPk;
}

Expand Down