Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dskloetd committed Nov 4, 2024
1 parent d16c674 commit e1359da
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
118 changes: 118 additions & 0 deletions packages/nns/src/canisters/governance/response.converters.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { Principal } from "@dfinity/principal";
import type { Neuron as RawNeuron } 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";

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 defaultCandidNeuron: RawNeuron = {
id: [{ id: neuronId }],
staked_maturity_e8s_equivalent: [0n],
controller: [Principal.fromText(controlledIdText)],
recent_ballots: [],
kyc_verified: false,
neuron_type: [],
not_for_profit: false,
cached_neuron_stake_e8s: neuronStake,
created_timestamp_seconds: createdTimestampSeconds,
auto_stake_maturity: [false],
maturity_e8s_equivalent: 0n,
aging_since_timestamp_seconds: createdTimestampSeconds,
neuron_fees_e8s: 0n,
hot_keys: [],
account: new Uint8Array(),
joined_community_fund_timestamp_seconds: [],
dissolve_state: [{ DissolveDelaySeconds: dissolveDelaySeconds }],
spawn_at_timestamp_seconds: [],
followees: [],
visibility: [],
transfer: [],
known_neuron_data: [],
voting_power_refreshed_timestamp_seconds: [],
};

const defaultNeuron: Neuron = {
id: neuronId,
stakedMaturityE8sEquivalent: 0n,
controller: controlledIdText,
recentBallots: [],
neuronType: undefined,
kycVerified: false,
notForProfit: false,
cachedNeuronStake: neuronStake,
createdTimestampSeconds,
autoStakeMaturity: false,
maturityE8sEquivalent: 0n,
agingSinceTimestampSeconds: createdTimestampSeconds,
neuronFees: 0n,
hotKeys: [],
accountIdentifier:
"5608e9e28e45cb752c31464abe8edfca5fca00971942a882989613c20848da6f",
joinedCommunityFundTimestampSeconds: undefined,
dissolveState: { DissolveDelaySeconds: dissolveDelaySeconds },
spawnAtTimesSeconds: undefined,
followees: [],
visibility: undefined,
votingPowerRefreshedTimestampSeconds: undefined,
};

describe("toNeuron", () => {
it("should convert a default candid Neuron to ic-js Neuron", () => {
expect(
toNeuron({
neuron: defaultCandidNeuron,
canisterId: MAINNET_GOVERNANCE_CANISTER_ID,
}),
).toEqual(defaultNeuron);
});

it("should convert a voting power refreshed timestamp", () => {
const timestamp = 1_333_444_999n;
expect(
toNeuron({
neuron: {
...defaultCandidNeuron,
voting_power_refreshed_timestamp_seconds: [timestamp],
},
canisterId: MAINNET_GOVERNANCE_CANISTER_ID,
}),
).toEqual({
...defaultNeuron,
votingPowerRefreshedTimestampSeconds: timestamp,
});
});
});

describe("toRawNeuron", () => {
it("should convert a default ic-js Neuron to candid Neuron", () => {
expect(
toRawNeuron({
neuron: defaultNeuron,
account: new Uint8Array(),
}),
).toEqual(defaultCandidNeuron);
});

it("should convert a voting power refreshed timestamp", () => {
const timestamp = 1_333_444_998n;
expect(
toRawNeuron({
neuron: {
...defaultNeuron,
votingPowerRefreshedTimestampSeconds: timestamp,
},
account: new Uint8Array(),
}),
).toEqual({
...defaultCandidNeuron,
voting_power_refreshed_timestamp_seconds: [timestamp],
});
});
});
});
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 @@ -190,6 +190,9 @@ export const toNeuron = ({
toFollowees({ topic, followees }),
),
visibility: fromNullable(neuron.visibility) as NeuronVisibility | undefined,
votingPowerRefreshedTimestampSeconds: fromNullable(
neuron.voting_power_refreshed_timestamp_seconds,
),
});

export const toRawNeuron = ({
Expand Down Expand Up @@ -241,6 +244,9 @@ export const toRawNeuron = ({
transfer: [],
// Not kept when converted to Neuron.
known_neuron_data: [],
voting_power_refreshed_timestamp_seconds: toNullable(
neuron.votingPowerRefreshedTimestampSeconds,
),
});

const toBallotInfo = ({ vote, proposal_id }: RawBallotInfo): BallotInfo => ({
Expand Down
2 changes: 2 additions & 0 deletions packages/nns/src/mocks/governance.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const mockNeuronInfo: NeuronInfo = {
voting_power: one,
age_seconds: one,
visibility: [1],
voting_power_refreshed_timestamp_seconds: [],
};
export const mockNeuron: Neuron = {
id: [{ id: mockNeuronId }],
Expand All @@ -44,6 +45,7 @@ export const mockNeuron: Neuron = {
known_neuron_data: [],
spawn_at_timestamp_seconds: [],
visibility: [1],
voting_power_refreshed_timestamp_seconds: [],
};
export const mockListNeuronsResponse: ListNeuronsResponse = {
neuron_infos: [[mockNeuronId, mockNeuronInfo]],
Expand Down
1 change: 1 addition & 0 deletions packages/nns/src/types/governance_converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ export interface Neuron {
dissolveState: Option<DissolveState>;
followees: Array<Followees>;
visibility: Option<NeuronVisibility>;
votingPowerRefreshedTimestampSeconds: Option<bigint>;
}
export type NeuronIdOrSubaccount =
| { Subaccount: Array<number> }
Expand Down

0 comments on commit e1359da

Please sign in to comment.