Skip to content

Commit

Permalink
Get summary lifecycle through SnsSummaryWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dskloetd committed Jul 15, 2024
1 parent f1e1d05 commit bb91535
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
PROJECT_DETAIL_CONTEXT_KEY,
type ProjectDetailContext,
} from "$lib/types/project-detail.context";
import type { SnsSummaryWrapper } from "$lib/types/sns-summary-wrapper";
import {
hasUserParticipatedToSwap,
participateButtonStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
PROJECT_DETAIL_CONTEXT_KEY,
type ProjectDetailContext,
} from "$lib/types/project-detail.context";
import type { SnsSummary, SnsSummaryMetadata } from "$lib/types/sns";
import type { SnsSummaryMetadata } from "$lib/types/sns";
import type { SnsSummaryWrapper } from "$lib/types/sns-summary-wrapper";
import { snsProjectDashboardUrl } from "$lib/utils/projects.utils";
import type { Principal } from "@dfinity/principal";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
type ProjectDetailContext,
} from "$lib/types/project-detail.context";
import type { SnsSwapCommitment } from "$lib/types/sns";
import type { SnsSummaryWrapper } from "$lib/types/sns-summary-wrapper";
import { getCommitmentE8s } from "$lib/utils/sns.utils";
import ParticipateButton from "./ParticipateButton.svelte";
import ProjectCommitment from "./ProjectCommitment.svelte";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<script lang="ts">
import { i18n } from "$lib/stores/i18n";
import type {
SnsSummary,
SnsSummarySwap,
SnsSwapCommitment,
} from "$lib/types/sns";
import type { SnsSummarySwap, SnsSwapCommitment } from "$lib/types/sns";
import type { SnsSummaryWrapper } from "$lib/types/sns-summary-wrapper";
import {
durationTillSwapDeadline,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<script lang="ts">
import { i18n } from "$lib/stores/i18n";
import type { SnsSummary, SnsSwapCommitment } from "$lib/types/sns";
import type { SnsSwapCommitment } from "$lib/types/sns";
import type { SnsSummaryWrapper } from "$lib/types/sns-summary-wrapper";
import { canUserParticipateToSwap } from "$lib/utils/projects.utils";
export let summary: SnsSummary | undefined | null;
export let summary: SnsSummaryWrapper | undefined | null;
export let swapCommitment: SnsSwapCommitment | undefined | null;
let canParticipate = false;
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/lib/pages/ProjectDetail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
type ProjectDetailStore,
} from "$lib/types/project-detail.context";
import { SaleStep } from "$lib/types/sale";
import type { SnsSummaryWrapper } from "$lib/types/sns-summary-wrapper";
import { userCountryIsNeeded } from "$lib/utils/projects.utils";
import { hasBuyersCount } from "$lib/utils/sns-swap.utils";
import { getCommitmentE8s } from "$lib/utils/sns.utils";
Expand Down Expand Up @@ -149,7 +148,7 @@
let enableOpenProjectWatchers = false;
$: enableOpenProjectWatchers =
$projectDetailStore?.summary.getLifecycle() === SnsSwapLifecycle.Open;
$projectDetailStore?.summary?.getLifecycle() === SnsSwapLifecycle.Open;
let swapCanisterId: Principal | undefined;
$: swapCanisterId = $projectDetailStore.summary?.swapCanisterId;
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/lib/types/sns-summary-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export class SnsSummaryWrapper implements SnsSummary {
return this.summary.lifecycle;
}

getLifecycle(): SnsSwapLifecycle {
return this.swap.lifecycle;
}

public overrideDerivedState(
newDerivedState: SnsSwapDerivedState
): SnsSummaryWrapper {
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/lib/utils/projects.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
SnsSummarySwap,
SnsSwapCommitment,
} from "$lib/types/sns";
import type { SnsSummaryWrapper } from "$lib/types/sns-summary-wrapper";
import type { StoreData } from "$lib/types/store";
import type { Principal } from "@dfinity/principal";
import { SnsSwapLifecycle, type SnsSwapTicket } from "@dfinity/sns";
Expand Down Expand Up @@ -107,7 +108,7 @@ export const currentUserMaxCommitment = ({
export const projectRemainingAmount = ({ swap, derived }: SnsSummary): bigint =>
swap.params.max_icp_e8s - derived.buyer_total_icp_e8s;

const isProjectOpen = (summary: SnsSummary): boolean =>
const isProjectOpen = (summary: SnsSummaryWrapper): boolean =>
summary.getLifecycle() === SnsSwapLifecycle.Open;
// Checks whether the amount that the user wants to contribute is lower than the minimum for the project.
// It takes into account the current commitment of the user.
Expand Down Expand Up @@ -149,7 +150,7 @@ export const canUserParticipateToSwap = ({
summary,
swapCommitment,
}: {
summary: SnsSummary | undefined | null;
summary: SnsSummaryWrapper | undefined | null;
swapCommitment: SnsSwapCommitment | undefined | null;
}): boolean => {
const myCommitment = getCommitmentE8s(swapCommitment) ?? 0n;
Expand Down Expand Up @@ -179,7 +180,7 @@ export const userCountryIsNeeded = ({
swapCommitment,
loggedIn,
}: {
summary: SnsSummary | undefined | null;
summary: SnsSummaryWrapper | undefined | null;
swapCommitment: SnsSwapCommitment | undefined | null;
loggedIn: boolean;
}): boolean =>
Expand Down Expand Up @@ -320,7 +321,7 @@ export const participateButtonStatus = ({
ticket,
userCountry,
}: {
summary: SnsSummary | undefined | null;
summary: SnsSummaryWrapper | undefined | null;
swapCommitment: SnsSwapCommitment | undefined | null;
loggedIn: boolean;
ticket: SnsSwapTicket | undefined | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ describe("ProjectTimelineUserCommitment", () => {

it("should render starting info if status Adopted", () => {
const summaryData = summaryForLifecycle(SnsSwapLifecycle.Adopted);
const summary = {
...summaryData,
const summary = summaryData.override({
swap: {
...summaryData.swap,
decentralization_sale_open_timestamp_seconds: BigInt(
now + SECONDS_IN_DAY
),
},
};
});
const { queryByText } = render(ProjectTimelineUserCommitment, {
summary,
swapCommitment: mockSnsFullProject.swapCommitment as SnsSwapCommitment,
Expand Down
124 changes: 124 additions & 0 deletions frontend/src/tests/lib/types/sns-summary-wrapper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import type { SnsSummarySwap } from "$lib/types/sns";
import {
mockDerived,
mockInit,
mockLifecycleResponse,
mockMetadata,
mockSnsParams,
mockSummary,
mockSwap,
mockToken,
principal,
} from "$tests/mocks/sns-projects.mock";
import type {
SnsGetLifecycleResponse,
SnsParams,
SnsSwapDerivedState,
SnsSwapInit,
} from "@dfinity/sns";
import { SnsSwapLifecycle } from "@dfinity/sns";

describe("SnsSummaryWrapper", () => {
it("should return rootCanisterId", () => {
const rootCanisterId = principal(323);
const summary = mockSummary.override({ rootCanisterId });
expect(summary.rootCanisterId.toText()).toBe(rootCanisterId.toText());
});

it("should return swapCanisterId", () => {
const swapCanisterId = principal(324);
const summary = mockSummary.override({ swapCanisterId });
expect(summary.swapCanisterId.toText()).toBe(swapCanisterId.toText());
});

it("should return governanceCanisterId", () => {
const governanceCanisterId = principal(325);
const summary = mockSummary.override({ governanceCanisterId });
expect(summary.governanceCanisterId.toText()).toBe(
governanceCanisterId.toText()
);
});

it("should return ledgerCanisterId", () => {
const ledgerCanisterId = principal(326);
const summary = mockSummary.override({ ledgerCanisterId });
expect(summary.ledgerCanisterId.toText()).toBe(ledgerCanisterId.toText());
});

it("should return indexCanisterId", () => {
const indexCanisterId = principal(327);
const summary = mockSummary.override({ indexCanisterId });
expect(summary.indexCanisterId.toText()).toBe(indexCanisterId.toText());
});

it("should return indexCanisterId", () => {
const indexCanisterId = principal(328);
const summary = mockSummary.override({ indexCanisterId });
expect(summary.indexCanisterId.toText()).toBe(indexCanisterId.toText());
});

it("should return metadata", () => {
const metadata = {
...mockMetadata,
url: "https://example.com/wrapper-test",
};
const summary = mockSummary.override({ metadata });
expect(summary.metadata).toBe(metadata);
});

it("should return token", () => {
const token = {
...mockToken,
name: "Wrapper token",
};
const summary = mockSummary.override({ token });
expect(summary.token).toBe(token);
});

it("should return swap", () => {
const swap: SnsSummarySwap = {
...mockSwap,
next_ticket_id: [457831n],
};
const summary = mockSummary.override({ swap });
expect(summary.swap).toBe(swap);
});

it("should return derived", () => {
const derived: SnsSwapDerivedState = {
...mockDerived,
direct_participant_count: [34294n],
};
const summary = mockSummary.overrideDerivedState(derived);
expect(summary.derived).toBe(derived);
});

it("should return init", () => {
const init: SnsSwapInit = {
...mockInit,
nns_proposal_id: [91n],
};
const summary = mockSummary.override({ init });
expect(summary.init).toBe(init);
});

it("should return swapParams", () => {
const swapParams: SnsParams = {
...mockSnsParams,
max_icp_e8s: 13_785_000_000n,
};
const summary = mockSummary.override({ swapParams });
expect(summary.swapParams).toBe(swapParams);
});

it("should return lifecycle", () => {
const lifecycle: SnsGetLifecycleResponse = {
...mockLifecycleResponse,
lifecycle: [SnsSwapLifecycle.Aborted],
decentralization_sale_open_timestamp_seconds: [168_100_000n],
};
const summary = mockSummary.overrideLifecycleResponse(lifecycle);
expect(summary.lifecycle).toBe(lifecycle);
expect(summary.getLifecycle()).toBe(SnsSwapLifecycle.Aborted);
});
});
2 changes: 1 addition & 1 deletion frontend/src/tests/lib/utils/projects.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ describe("project-utils", () => {
describe("participateButtonStatus", () => {
const summary = mockSnsFullProject.summary;

const notOpenSummary: SnsSummary = mockSnsFullProject.summary.override({
const notOpenSummary = mockSnsFullProject.summary.override({
swap: {
...mockSnsFullProject.summary.swap,
lifecycle: SnsSwapLifecycle.Committed,
Expand Down

0 comments on commit bb91535

Please sign in to comment.