Skip to content

Commit

Permalink
New voting power related fields in NeuronInfo (#794)
Browse files Browse the repository at this point in the history
# Motivation

After introducing new neuron properties (`deciding_voting_power` and
`potential_voting_power`), the original `voting_power` field has become
[obsolete](https://github.com/dfinity/ic/blob/586c57afa7af796185c76c8ff26bc37e813957c2/rs/nns/governance/canister/governance.did#L744-L751).
With this PR, we add three new fields to the NeuronInfo:
`decidingVotingPower`, `potentialVotingPower`, and a small drive-by
addition `votingPowerRefreshedTimestampSeconds`; and mark the original
votingPower field as deprecated.

# Changes

- Added 3 new fields.
- Mark `votingPower` as deprecated.

# Tests

- Pass.
- Tested manually by utilizing new fields in the NNS Dapp.

# Todos

- [ ] Add entry to changelog (if necessary).
Not necessary.

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
mstrasinskis and github-actions[bot] authored Dec 19, 2024
1 parent 98c893a commit d810d76
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 7 deletions.
131 changes: 124 additions & 7 deletions packages/nns/src/canisters/governance/response.converters.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { Principal } from "@dfinity/principal";
import type { Neuron as RawNeuron } from "../../../candid/governance";
import type {
Neuron as RawNeuron,
NeuronInfo as RawNeuronInfo,
} from "../../../candid/governance";
import { MAINNET_GOVERNANCE_CANISTER_ID } from "../../constants/canister_ids";
import type { Neuron } from "../../types/governance_converters";
import { toNeuron, toRawNeuron } from "./response.converters";
import { NeuronState } from "../../enums/governance.enums";
import type { Neuron, NeuronInfo } from "../../types/governance_converters";
import { toNeuron, toNeuronInfo, toRawNeuron } from "./response.converters";

describe("response.converters", () => {
const neuronId = 123n;
const neuronStake = 100_000_000n;
const controlledIdText = "souto-grxij-jbijj-tmr3q";
const createdTimestampSeconds = 1_234_567_000n;
const dissolveDelaySeconds = 8_640_000n;
const state = NeuronState.Locked;
const canisterId = MAINNET_GOVERNANCE_CANISTER_ID;

const defaultCandidNeuron: RawNeuron = {
id: [{ id: neuronId }],
Expand Down Expand Up @@ -66,12 +72,123 @@ describe("response.converters", () => {
decidingVotingPower: undefined,
};

const defaultCandidNeuronInfo: RawNeuronInfo = {
dissolve_delay_seconds: dissolveDelaySeconds,
recent_ballots: [],
voting_power_refreshed_timestamp_seconds: [],
potential_voting_power: [],
neuron_type: [],
deciding_voting_power: [],
created_timestamp_seconds: createdTimestampSeconds,
state,
stake_e8s: 0n,
joined_community_fund_timestamp_seconds: [],
retrieved_at_timestamp_seconds: 0n,
visibility: [],
known_neuron_data: [],
voting_power: 0n,
age_seconds: 0n,
};

const defaultNeuronInfo: NeuronInfo = {
neuronId,
dissolveDelaySeconds,
recentBallots: [],
neuronType: undefined,
createdTimestampSeconds,
state,
joinedCommunityFundTimestampSeconds: undefined,
retrievedAtTimestampSeconds: 0n,
votingPower: 0n,
votingPowerRefreshedTimestampSeconds: undefined,
decidingVotingPower: undefined,
potentialVotingPower: undefined,
ageSeconds: 0n,
fullNeuron: undefined,
visibility: undefined,
};

describe("toNeuronInfo", () => {
it("should convert a default candid NeuronInfo to ic-js NeuronInfo", () => {
expect(
toNeuronInfo({
neuronId,
neuronInfo: defaultCandidNeuronInfo,
rawNeuron: undefined,
canisterId,
}),
).toEqual(defaultNeuronInfo);
});

it("should convert a fullNeuron", () => {
expect(
toNeuronInfo({
neuronId,
neuronInfo: defaultCandidNeuronInfo,
rawNeuron: defaultCandidNeuron,
canisterId,
}),
).toEqual({
...defaultNeuronInfo,
fullNeuron: defaultNeuron,
});
});

it("should convert a voting power refreshed timestamp", () => {
const timestamp = 1_333_444_999n;
expect(
toNeuronInfo({
neuronId,
neuronInfo: {
...defaultCandidNeuronInfo,
voting_power_refreshed_timestamp_seconds: [timestamp],
},
rawNeuron: undefined,
canisterId,
}),
).toEqual({
...defaultNeuronInfo,
votingPowerRefreshedTimestampSeconds: timestamp,
});
});

it("should convert potential voting power", () => {
const potentialVotingPower = 1_000_000n;
expect(
toNeuronInfo({
neuronId,
neuronInfo: {
...defaultCandidNeuronInfo,
potential_voting_power: [potentialVotingPower],
},
rawNeuron: undefined,
canisterId,
}),
).toEqual({ ...defaultNeuronInfo, potentialVotingPower });
});

it("should convert deciding voting power", () => {
const decidingVotingPower = 1_001_000n;
expect(
toNeuronInfo({
neuronId,
neuronInfo: {
...defaultCandidNeuronInfo,
deciding_voting_power: [decidingVotingPower],
},
rawNeuron: undefined,
canisterId,
}),
).toEqual({ ...defaultNeuronInfo, decidingVotingPower });
});
});

describe("toNeuron", () => {
it("should convert a default candid Neuron to ic-js Neuron", () => {
expect(
toNeuron({
neuron: defaultCandidNeuron,
canisterId: MAINNET_GOVERNANCE_CANISTER_ID,
canisterId,
}),
).toEqual(defaultNeuron);
});
Expand All @@ -84,7 +201,7 @@ describe("response.converters", () => {
...defaultCandidNeuron,
voting_power_refreshed_timestamp_seconds: [timestamp],
},
canisterId: MAINNET_GOVERNANCE_CANISTER_ID,
canisterId,
}),
).toEqual({
...defaultNeuron,
Expand All @@ -101,7 +218,7 @@ describe("response.converters", () => {
...defaultCandidNeuron,
potential_voting_power: [votingPower],
},
canisterId: MAINNET_GOVERNANCE_CANISTER_ID,
canisterId,
}),
).toEqual({
...defaultNeuron,
Expand All @@ -118,7 +235,7 @@ describe("response.converters", () => {
...defaultCandidNeuron,
deciding_voting_power: [votingPower],
},
canisterId: MAINNET_GOVERNANCE_CANISTER_ID,
canisterId,
}),
).toEqual({
...defaultNeuron,
Expand Down
6 changes: 6 additions & 0 deletions packages/nns/src/canisters/governance/response.converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ export const toNeuronInfo = ({
? neuronInfo.joined_community_fund_timestamp_seconds[0]
: undefined,
retrievedAtTimestampSeconds: neuronInfo.retrieved_at_timestamp_seconds,
/** @deprecated */
votingPower: neuronInfo.voting_power,
votingPowerRefreshedTimestampSeconds: fromNullable(
neuronInfo.voting_power_refreshed_timestamp_seconds,
),
decidingVotingPower: fromNullable(neuronInfo.deciding_voting_power),
potentialVotingPower: fromNullable(neuronInfo.potential_voting_power),
ageSeconds: neuronInfo.age_seconds,
visibility: fromNullable(neuronInfo.visibility) as
| NeuronVisibility
Expand Down
3 changes: 3 additions & 0 deletions packages/nns/src/types/governance_converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ export interface NeuronInfo {
joinedCommunityFundTimestampSeconds: Option<bigint>;
retrievedAtTimestampSeconds: bigint;
votingPower: bigint;
votingPowerRefreshedTimestampSeconds: Option<bigint>;
decidingVotingPower: Option<bigint>;
potentialVotingPower: Option<bigint>;
ageSeconds: bigint;
fullNeuron: Option<Neuron>;
visibility: Option<NeuronVisibility>;
Expand Down

0 comments on commit d810d76

Please sign in to comment.