Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal based votable state #725

Merged
merged 17 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
12 changes: 6 additions & 6 deletions packages/nns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Parameters:
- `params.neurons`: The neurons to filter.
- `params.proposal`: The proposal to match against the selected neurons.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L39)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L38)

#### :gear: votableNeurons

Expand All @@ -111,22 +111,22 @@ Parameters:
- `params.neurons`: The neurons to filter.
- `params.proposal`: The proposal to match against the selected neurons.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L68)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L67)

#### :gear: votedNeurons

Filter the neurons that have voted for a proposal.

| Function | Type |
| -------------- | ------------------------------------------------------------------------------------------------------------------ |
| `votedNeurons` | `({ neurons, proposal: { id: proposalId }, }: { neurons: NeuronInfo[]; proposal: ProposalInfo; }) => NeuronInfo[]` |
| Function | Type |
| -------------- | ----------------------------------------------------------------------------------------------------------- |
| `votedNeurons` | `({ neurons, proposal: { ballots }, }: { neurons: NeuronInfo[]; proposal: ProposalInfo; }) => NeuronInfo[]` |

Parameters:

- `params.neurons`: The neurons to filter.
- `params.proposal`: The proposal for which some neurons might have already voted.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L94)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/utils/neurons.utils.ts#L93)

### :factory: GenesisTokenCanister

Expand Down
109 changes: 73 additions & 36 deletions packages/nns/src/utils/neurons.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ describe("neurons-utils", () => {
recentBallots: [],
votingPower: BigInt(1),
} as unknown as NeuronInfo,
{
createdTimestampSeconds: proposalTimestampSeconds - BigInt(2),
neuronId: proposalNeuronId + 1n,
recentBallots: [],
votingPower: BigInt(1),
} as unknown as NeuronInfo,
{
createdTimestampSeconds: proposalTimestampSeconds - BigInt(3),
neuronId: proposalNeuronId + 2n,
recentBallots: [],
votingPower: BigInt(1),
} as unknown as NeuronInfo,
];

it("should has an ineligible neuron because created after proposal", () => {
Expand Down Expand Up @@ -104,16 +116,19 @@ describe("neurons-utils", () => {

it("should have votable neurons because not yet voted", () => {
const votable = votableNeurons({
proposal,
proposal: {
...proposal,
ballots: [
{
neuronId: eligibleNeuronsDate[0].neuronId,
mstrasinskis marked this conversation as resolved.
Show resolved Hide resolved
vote: Vote.Unspecified,
votingPower: BigInt(1),
},
],
},
neurons: [
{
...eligibleNeuronsDate[0],
recentBallots: [
{
proposalId: BigInt(4),
vote: Vote.No,
},
],
},
],
});
mstrasinskis marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -122,11 +137,19 @@ describe("neurons-utils", () => {

it("should have votable neurons because never voted", () => {
const votable = votableNeurons({
proposal,
proposal: {
...proposal,
ballots: [
{
neuronId: eligibleNeuronsDate[0].neuronId,
vote: Vote.Unspecified,
votingPower: BigInt(1),
},
],
},
neurons: [
{
...eligibleNeuronsDate[0],
recentBallots: [],
},
],
});
mstrasinskis marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -135,40 +158,46 @@ describe("neurons-utils", () => {

it("should have votable neurons regardless of voting power", () => {
mstrasinskis marked this conversation as resolved.
Show resolved Hide resolved
const votable = votableNeurons({
proposal,
neurons: [
{
...eligibleNeuronsDate[0],
recentBallots: [],
votingPower: BigInt(0),
},
{
...eligibleNeuronsDate[0],
recentBallots: [],
votingPower: BigInt(1),
},
{
...eligibleNeuronsDate[0],
recentBallots: [],
votingPower: BigInt(0),
},
],
proposal: {
...proposal,
ballots: [
{
neuronId: eligibleNeuronsDate[0].neuronId,
vote: Vote.Unspecified,
votingPower: BigInt(0),
},
{
neuronId: eligibleNeuronsDate[1].neuronId,
vote: Vote.Unspecified,
votingPower: BigInt(1),
},
{
neuronId: eligibleNeuronsDate[2].neuronId,
vote: Vote.Unspecified,
votingPower: BigInt(0),
},
],
},
neurons: eligibleNeuronsDate,
});
expect(votable.length).toEqual(3);
mstrasinskis marked this conversation as resolved.
Show resolved Hide resolved
});

it("should not have voted neurons because votable", () => {
const voted = votedNeurons({
proposal,
proposal: {
...proposal,
ballots: [
{
neuronId: eligibleNeuronsDate[0].neuronId,
vote: Vote.Unspecified,
votingPower: BigInt(1),
},
],
},
neurons: [
{
...eligibleNeuronsDate[0],
recentBallots: [
{
proposalId: BigInt(4),
vote: Vote.No,
},
],
},
],
});
Expand All @@ -177,11 +206,19 @@ describe("neurons-utils", () => {

it("should not have voted neurons because never voted", () => {
const voted = votedNeurons({
proposal,
proposal: {
...proposal,
ballots: [
{
neuronId: eligibleNeuronsDate[0].neuronId,
vote: Vote.Unspecified,
votingPower: BigInt(1),
},
],
},
neurons: [
{
...eligibleNeuronsDate[0],
recentBallots: [],
},
],
});
Expand Down
31 changes: 15 additions & 16 deletions packages/nns/src/utils/neurons.utils.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import type { Vote } from "../enums/governance.enums";
import { Vote } from "../enums/governance.enums";
import type { NeuronId } from "../types/common";
import type {
Ballot,
BallotInfo,
NeuronInfo,
ProposalId,
ProposalInfo,
} from "../types/governance_converters";

const voteForProposal = ({
recentBallots,
mstrasinskis marked this conversation as resolved.
Show resolved Hide resolved
proposalId,
ballots,
neuronId,
mstrasinskis marked this conversation as resolved.
Show resolved Hide resolved
}: {
recentBallots: BallotInfo[];
proposalId: ProposalId | undefined;
ballots: Ballot[];
neuronId: NeuronId | undefined;
}): Vote | undefined => {
if (!proposalId) {
if (!neuronId) {
return undefined;
}

const ballot: BallotInfo | undefined = recentBallots.find(
({ proposalId: id }: BallotInfo) => id === proposalId,
const ballot: Ballot | undefined = ballots.find(
({ neuronId: id }) => id === neuronId,
);
return ballot?.vote;
};
Expand Down Expand Up @@ -72,11 +71,11 @@ export const votableNeurons = ({
neurons: NeuronInfo[];
proposal: ProposalInfo;
}): NeuronInfo[] => {
const { id: proposalId } = proposal;
const { ballots } = proposal;

return neurons.filter(
({ recentBallots, neuronId }: NeuronInfo) =>
voteForProposal({ recentBallots, proposalId }) === undefined &&
({ neuronId }: NeuronInfo) =>
voteForProposal({ ballots, neuronId }) === Vote.Unspecified &&
ineligibleNeurons({ neurons, proposal }).find(
({ neuronId: ineligibleNeuronId }: NeuronInfo) =>
ineligibleNeuronId === neuronId,
Expand All @@ -93,12 +92,12 @@ export const votableNeurons = ({
*/
export const votedNeurons = ({
neurons,
proposal: { id: proposalId },
proposal: { ballots },
}: {
neurons: NeuronInfo[];
proposal: ProposalInfo;
}): NeuronInfo[] =>
neurons.filter(
({ recentBallots }: NeuronInfo) =>
voteForProposal({ recentBallots, proposalId }) !== undefined,
({ neuronId }: NeuronInfo) =>
voteForProposal({ ballots, neuronId }) !== Vote.Unspecified,
);
Loading