Skip to content

Commit

Permalink
GIX-1937: Use fields in min/max participation getters (#3553)
Browse files Browse the repository at this point in the history
# Motivation

Add support for the new Neurons' Fund enhancements.

In this PR, the getters `getMinDirectParticipation` and
`getMaxDirectParticipation` return the actual value from the SNS
project.

# Changes

* Return the values in the `init` field of the SnsSummary.

# Tests

* Add two fields to createSummary helper.
* Remove the mocks of the getters and instead create a summary with the
desired values.

# Todos

- [x] Add entry to changelog (if necessary).
  • Loading branch information
lmuntaner authored Oct 13, 2023
1 parent 53a2827 commit a026bdc
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 113 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-Nns-Dapp-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ proposal is successful, the changes it released will be moved from this file to
* New Tag style. Used in followees topic and project status.
* New header UI in the wallet pages.
* Integrated library `marked` within dependencies instead of shipping it as a static asset.
* Detailed values of the Neurons' Fund and direct participation in the project detai page.

#### Deprecated
#### Removed
Expand Down
16 changes: 8 additions & 8 deletions frontend/src/lib/getters/sns-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export const getNeuronsFundParticipation = ({
}: SnsSummary): bigint | undefined =>
fromNullable(derived.neurons_fund_participation_icp_e8s);

// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
export const getMinDirectParticipation = (
_summary: SnsSummary
): bigint | undefined => undefined;
export const getMinDirectParticipation = ({
init,
}: SnsSummary): bigint | undefined =>
fromNullable(init?.min_direct_participation_icp_e8s ?? []);

// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
export const getMaxDirectParticipation = (
_summary: SnsSummary
): bigint | undefined => undefined;
export const getMaxDirectParticipation = ({
init,
}: SnsSummary): bigint | undefined =>
fromNullable(init?.max_direct_participation_icp_e8s ?? []);
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ProjectCommitment from "$lib/components/project-detail/ProjectCommitment.svelte";
import * as summaryGetters from "$lib/getters/sns-summary";
import { snsSwapMetricsStore } from "$lib/stores/sns-swap-metrics.store";
import type { SnsSummary, SnsSwapCommitment } from "$lib/types/sns";
import {
Expand Down Expand Up @@ -89,17 +88,8 @@ describe("ProjectCommitment", () => {
currentTotalCommitment: directCommitment + nfCommitment,
directCommitment,
neuronsFundCommitment: nfCommitment,
});

beforeEach(() => {
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(summaryGetters, "getMinDirectParticipation").mockImplementation(
() => 10000000000n
);
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(summaryGetters, "getMaxDirectParticipation").mockImplementation(
() => 100000000000n
);
minDirectParticipation: 10000000000n,
maxDirectParticipation: 100000000000n,
});

it("should render a progress bar with direct participation", async () => {
Expand All @@ -122,23 +112,14 @@ describe("ProjectCommitment", () => {
});

describe("when Neurons' Fund enhancements fields are available and NF commitment is 0", () => {
beforeEach(() => {
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(summaryGetters, "getMinDirectParticipation").mockImplementation(
() => 10000000000n
);
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(summaryGetters, "getMaxDirectParticipation").mockImplementation(
() => 100000000000n
);
});

it("should render detailed participation if neurons fund participation is zero", async () => {
const directCommitment = 20000000000n;
const summary = createSummary({
currentTotalCommitment: directCommitment,
neuronsFundCommitment: 0n,
directCommitment,
minDirectParticipation: 10000000000n,
maxDirectParticipation: 100000000000n,
});
const po = renderComponent(summary);
expect(await po.getNeuronsFundParticipation()).toEqual("0 ICP");
Expand All @@ -152,17 +133,8 @@ describe("ProjectCommitment", () => {
currentTotalCommitment: overallCommitment,
neuronsFundCommitment: undefined,
directCommitment: undefined,
});

beforeEach(() => {
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(summaryGetters, "getMinDirectParticipation").mockImplementation(
() => undefined
);
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(summaryGetters, "getMaxDirectParticipation").mockImplementation(
() => undefined
);
minDirectParticipation: undefined,
maxDirectParticipation: undefined,
});

it("should render a progress bar with overall participation", async () => {
Expand Down
99 changes: 30 additions & 69 deletions frontend/src/tests/lib/utils/projects.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NOT_LOADED } from "$lib/constants/stores.constants";
import type { SnsFullProject } from "$lib/derived/sns/sns-projects.derived";
import * as summaryGetters from "$lib/getters/sns-summary";
import type { SnsSummary, SnsSwapCommitment } from "$lib/types/sns";
import { nowInSeconds } from "$lib/utils/date.utils";
import {
Expand Down Expand Up @@ -1276,63 +1275,49 @@ describe("project-utils", () => {
describe("getProjectCommitmentSplit", () => {
const nfCommitment = 10000000000n;
const directCommitment = 20000000000n;
const summary = createSummary({
currentTotalCommitment: directCommitment + nfCommitment,
directCommitment,
neuronsFundCommitment: nfCommitment,
});

describe("when NF participation is present", () => {
it("returns the commitments split if min-max direct participations are present", () => {
const minDirectParticipation = 10000000000n;
const maxDirectParticipation = 100000000000n;
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(
summaryGetters,
"getMinDirectParticipation"
).mockImplementation(() => minDirectParticipation);
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(
summaryGetters,
"getMaxDirectParticipation"
).mockImplementation(() => maxDirectParticipation);
const summary = createSummary({
currentTotalCommitment: directCommitment + nfCommitment,
directCommitment,
neuronsFundCommitment: nfCommitment,
minDirectParticipation,
maxDirectParticipation,
});

expect(getProjectCommitmentSplit(summary)).toEqual({
totalCommitmentE8s: 30000000000n,
directCommitmentE8s: 20000000000n,
nfCommitmentE8s: 10000000000n,
totalCommitmentE8s: directCommitment + nfCommitment,
directCommitmentE8s: directCommitment,
nfCommitmentE8s: nfCommitment,
minDirectCommitmentE8s: minDirectParticipation,
maxDirectCommitmentE8s: maxDirectParticipation,
});
});

it("returns the full commitment if min direct participation is not present", () => {
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(
summaryGetters,
"getMinDirectParticipation"
).mockImplementation(() => undefined);
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(
summaryGetters,
"getMaxDirectParticipation"
).mockImplementation(() => 100000000000n);
const summary = createSummary({
currentTotalCommitment: directCommitment + nfCommitment,
directCommitment,
neuronsFundCommitment: nfCommitment,
minDirectParticipation: undefined,
maxDirectParticipation: 100000000000n,
});
expect(getProjectCommitmentSplit(summary)).toEqual({
totalCommitmentE8s: 30000000000n,
});
});

it("returns the full commitment if max direct participation is not present", () => {
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(
summaryGetters,
"getMinDirectParticipation"
).mockImplementation(() => 100000000000n);
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(
summaryGetters,
"getMaxDirectParticipation"
).mockImplementation(() => undefined);
const summary = createSummary({
currentTotalCommitment: directCommitment + nfCommitment,
directCommitment,
neuronsFundCommitment: nfCommitment,
minDirectParticipation: 100000000000n,
maxDirectParticipation: undefined,
});
expect(getProjectCommitmentSplit(summary)).toEqual({
totalCommitmentE8s: 30000000000n,
});
Expand All @@ -1344,20 +1329,12 @@ describe("project-utils", () => {
const directCommitment = 20000000000n;
const minDirectParticipation = 10000000000n;
const maxDirectParticipation = 100000000000n;
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(
summaryGetters,
"getMinDirectParticipation"
).mockImplementation(() => minDirectParticipation);
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(
summaryGetters,
"getMaxDirectParticipation"
).mockImplementation(() => maxDirectParticipation);
const summary = createSummary({
currentTotalCommitment: directCommitment,
directCommitment,
neuronsFundCommitment: 0n,
minDirectParticipation,
maxDirectParticipation,
});
expect(getProjectCommitmentSplit(summary)).toEqual({
totalCommitmentE8s: 20000000000n,
Expand All @@ -1376,17 +1353,9 @@ describe("project-utils", () => {
currentTotalCommitment,
directCommitment: undefined,
neuronsFundCommitment: undefined,
minDirectParticipation: 100000000000n,
maxDirectParticipation: 1000000000000n,
});
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(
summaryGetters,
"getMinDirectParticipation"
).mockImplementation(() => 100000000000n);
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(
summaryGetters,
"getMaxDirectParticipation"
).mockImplementation(() => 1000000000000n);
expect(getProjectCommitmentSplit(summary)).toEqual({
totalCommitmentE8s: currentTotalCommitment,
});
Expand All @@ -1397,22 +1366,14 @@ describe("project-utils", () => {
it("returns the overall commitments even if nf commitment and min-max direct participations are present", () => {
const minDirectParticipation = 10000000000n;
const maxDirectParticipation = 100000000000n;
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(
summaryGetters,
"getMinDirectParticipation"
).mockImplementation(() => minDirectParticipation);
// TODO: https://dfinity.atlassian.net/browse/GIX-1936 use min direct field when present
vi.spyOn(
summaryGetters,
"getMaxDirectParticipation"
).mockImplementation(() => maxDirectParticipation);

const currentTotalCommitment = 30000000000n;
const summary = createSummary({
currentTotalCommitment,
directCommitment: undefined,
neuronsFundCommitment: 10n,
minDirectParticipation,
maxDirectParticipation,
});

expect(getProjectCommitmentSplit(summary)).toEqual({
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/tests/mocks/sns-projects.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ export const mockInit: SnsSwapInit = {
restricted_countries: [],
min_icp_e8s: [1_500_000_000n],
neurons_fund_participation_constraints: [],
min_direct_participation_icp_e8s: [],
max_direct_participation_icp_e8s: [],
min_direct_participation_icp_e8s: [1_000_000_000n],
max_direct_participation_icp_e8s: [3_000_000_000n],
};

export const mockSwap: SnsSummarySwap = {
Expand Down Expand Up @@ -315,6 +315,8 @@ type SnsSummaryParams = {
currentTotalCommitment?: bigint;
neuronsFundCommitment?: bigint;
directCommitment?: bigint;
minDirectParticipation?: bigint;
maxDirectParticipation?: bigint;
};

export const createSummary = ({
Expand All @@ -332,11 +334,15 @@ export const createSummary = ({
currentTotalCommitment,
neuronsFundCommitment,
directCommitment,
minDirectParticipation,
maxDirectParticipation,
}: SnsSummaryParams): SnsSummary => {
const init: SnsSwapInit = {
...mockInit,
swap_due_timestamp_seconds: [swapDueTimestampSeconds],
confirmation_text: toNullable(confirmationText),
min_direct_participation_icp_e8s: toNullable(minDirectParticipation),
max_direct_participation_icp_e8s: toNullable(maxDirectParticipation),
restricted_countries: nonNullish(restrictedCountries)
? [{ iso_codes: restrictedCountries }]
: [],
Expand Down Expand Up @@ -368,6 +374,7 @@ export const createSummary = ({
params,
},
derived,
init,
};
};

Expand Down

0 comments on commit a026bdc

Please sign in to comment.