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 a19de08 commit 0525d12
Show file tree
Hide file tree
Showing 25 changed files with 271 additions and 179 deletions.
13 changes: 4 additions & 9 deletions frontend/src/lib/components/launchpad/ProjectCardSwapInfo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import ProjectUserCommitmentLabel from "$lib/components/project-detail/ProjectUserCommitmentLabel.svelte";
import type { SnsFullProject } from "$lib/derived/sns/sns-projects.derived";
import { i18n } from "$lib/stores/i18n";
import type {
SnsSummary,
SnsSwapCommitment,
SnsSummarySwap,
} from "$lib/types/sns";
import type { SnsSummarySwap, SnsSwapCommitment } from "$lib/types/sns";
import type { SnsSummaryWrapper } from "$lib/types/sns-summary-wrapper";
import {
durationTillSwapDeadline,
durationTillSwapStart,
Expand All @@ -23,17 +20,15 @@
// The data to know whether it's finalizing or not is not in the SnsFullProject.
export let isFinalizing: boolean;
let summary: SnsSummary;
let summary: SnsSummaryWrapper;
let swapCommitment: SnsSwapCommitment | undefined;
$: ({ summary, swapCommitment } = project);
let swap: SnsSummarySwap;
$: ({ swap } = summary);
let lifecycle: number;
$: ({
swap: { lifecycle },
} = summary);
$: lifecycle = summary.getLifecycle();
let durationTillDeadline: bigint | undefined;
$: durationTillDeadline = durationTillSwapDeadline(swap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
PROJECT_DETAIL_CONTEXT_KEY,
type ProjectDetailContext,
} from "$lib/types/project-detail.context";
import type { SnsSummary } from "$lib/types/sns";
import {
hasUserParticipatedToSwap,
type ParticipationButtonStatus,
participateButtonStatus,
type ParticipationButtonStatus,
} from "$lib/utils/projects.utils";
import { BottomSheet } from "@dfinity/gix-components";
import { Tooltip } from "@dfinity/gix-components";
Expand All @@ -29,13 +28,8 @@
);
let lifecycle: number;
$: ({
swap: { lifecycle },
} =
$projectDetailStore.summary ??
({
swap: { state: { lifecycle: SnsSwapLifecycle.Unspecified } },
} as unknown as SnsSummary));
$: lifecycle =
$projectDetailStore.summary?.getLifecycle() ?? SnsSwapLifecycle.Unspecified;
let showModal = false;
const openModal = () => (showModal = true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
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";
import { SnsSwapLifecycle } from "@dfinity/sns";
Expand All @@ -19,14 +20,14 @@
PROJECT_DETAIL_CONTEXT_KEY
);
let summary: SnsSummary | undefined | null;
let summary: SnsSummaryWrapper | undefined | null;
$: summary = $projectDetailStore.summary;
let rootCanisterId: Principal | undefined;
$: rootCanisterId = summary?.rootCanisterId;
let lifecycle: SnsSwapLifecycle | undefined;
$: lifecycle = summary?.swap.lifecycle;
$: lifecycle = summary?.getLifecycle();
let metadata: SnsSummaryMetadata | undefined;
let token: IcrcTokenMetadata | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
PROJECT_DETAIL_CONTEXT_KEY,
type ProjectDetailContext,
} from "$lib/types/project-detail.context";
import type { SnsSwapCommitment, SnsSummary } from "$lib/types/sns";
import type { SnsSwapCommitment } from "$lib/types/sns";
import { getCommitmentE8s } from "$lib/utils/sns.utils";
import ParticipateButton from "./ParticipateButton.svelte";
import ProjectCommitment from "./ProjectCommitment.svelte";
import ProjectStatus from "./ProjectStatus.svelte";
import ProjectTimelineUserCommitment from "./ProjectTimelineUserCommitment.svelte";
import { SnsSwapLifecycle } from "@dfinity/sns";
import { TokenAmount, ICPToken, nonNullish } from "@dfinity/utils";
import { ICPToken, TokenAmount, nonNullish } from "@dfinity/utils";
import { isNullish } from "@dfinity/utils";
import { getContext } from "svelte";
Expand All @@ -34,13 +34,8 @@
$: loadingSummary = isNullish($projectDetailStore.summary);
let lifecycle: number;
$: ({
swap: { lifecycle },
} =
$projectDetailStore.summary ??
({
swap: { state: { lifecycle: SnsSwapLifecycle.Unspecified } },
} as unknown as SnsSummary));
$: lifecycle =
$projectDetailStore.summary?.getLifecycle() ?? SnsSwapLifecycle.Unspecified;
let displayStatusSection = false;
$: displayStatusSection =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
<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,
durationTillSwapStart,
} from "$lib/utils/projects.utils";
import AmountDisplay from "../ic/AmountDisplay.svelte";
import Separator from "../ui/Separator.svelte";
import ProjectUserCommitmentLabel from "./ProjectUserCommitmentLabel.svelte";
import { Value, KeyValuePair } from "@dfinity/gix-components";
import { KeyValuePair, Value } from "@dfinity/gix-components";
import { SnsSwapLifecycle } from "@dfinity/sns";
import { TokenAmount, nonNullish } from "@dfinity/utils";
import { secondsToDuration } from "@dfinity/utils";
export let myCommitment: TokenAmount | undefined;
export let summary: SnsSummary;
export let summary: SnsSummaryWrapper;
export let swapCommitment: SnsSwapCommitment | undefined | null;
let swap: SnsSummarySwap;
Expand All @@ -31,11 +28,14 @@
let durationTillStart: bigint | undefined;
$: durationTillStart = durationTillSwapStart(swap);
let lifecycle: SnsSwapLifecycle;
$: lifecycle = summary.getLifecycle();
let isOpen: boolean;
$: isOpen = swap.lifecycle === SnsSwapLifecycle.Open;
$: isOpen = lifecycle === SnsSwapLifecycle.Open;
let isAdopted: boolean;
$: isAdopted = swap.lifecycle === SnsSwapLifecycle.Adopted;
$: isAdopted = lifecycle === SnsSwapLifecycle.Adopted;
let hasParticipated: boolean;
$: hasParticipated = nonNullish(myCommitment) && myCommitment.toE8s() > 0n;
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
11 changes: 4 additions & 7 deletions frontend/src/lib/derived/sns/sns-projects.derived.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import {
snsSwapCommitmentsStore,
type SnsSwapCommitmentsStore,
} from "$lib/stores/sns.store";
import type {
RootCanisterIdText,
SnsSummary,
SnsSwapCommitment,
} from "$lib/types/sns";
import type { RootCanisterIdText, SnsSwapCommitment } from "$lib/types/sns";
import type { SnsSummaryWrapper } from "$lib/types/sns-summary-wrapper";
import {
filterActiveProjects,
filterCommittedProjects,
Expand All @@ -21,7 +18,7 @@ import { derived, type Readable } from "svelte/store";

export interface SnsFullProject {
rootCanisterId: Principal;
summary: SnsSummary;
summary: SnsSummaryWrapper;
swapCommitment: SnsSwapCommitment | undefined;
}

Expand All @@ -32,7 +29,7 @@ export interface SnsFullProject {
* @return SnsFullProject[] | undefined What we called project - i.e. the summary and swap of a Sns with the user commitment
*/
export const snsProjectsStore = derived<
[Readable<SnsSummary[]>, SnsSwapCommitmentsStore],
[Readable<SnsSummaryWrapper[]>, SnsSwapCommitmentsStore],
SnsFullProject[]
>(
[snsSummariesStore, snsSwapCommitmentsStore],
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/lib/modals/sns/sale/ParticipateSwapModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
type ProjectDetailContext,
} from "$lib/types/project-detail.context";
import { SaleStep } from "$lib/types/sale";
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 type {
NewTransaction,
ValidateAmountFn,
Expand All @@ -36,7 +37,7 @@
hasOpenTicketInProcess,
} from "$lib/utils/sns.utils";
import type { WizardStep } from "@dfinity/gix-components";
import { TokenAmount, ICPToken } from "@dfinity/utils";
import { ICPToken, TokenAmount } from "@dfinity/utils";
import { nonNullish } from "@dfinity/utils";
import {
createEventDispatcher,
Expand All @@ -56,10 +57,10 @@
const { store: projectDetailStore, reload } =
getContext<ProjectDetailContext>(PROJECT_DETAIL_CONTEXT_KEY);
let summary: SnsSummary;
let summary: SnsSummaryWrapper;
let swapCommitment: SnsSwapCommitment | undefined | null;
// type safety validation is done in ProjectDetail component
$: summary = $projectDetailStore.summary as SnsSummary;
$: summary = $projectDetailStore.summary as SnsSummaryWrapper;
$: swapCommitment = $projectDetailStore.swapCommitment;
let userHasParticipatedToSwap = false;
$: userHasParticipatedToSwap = hasUserParticipatedToSwap({
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/lib/pages/ProjectDetail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
} from "$lib/services/sns-sale.services";
import { loadSnsSwapMetrics } from "$lib/services/sns-swap-metrics.services";
import {
loadSnsDerivedState,
loadSnsLifecycle,
loadSnsSwapCommitment,
loadSnsDerivedState,
watchSnsTotalCommitment,
} from "$lib/services/sns.services";
import { loadUserCountry } from "$lib/services/user-country.services";
Expand All @@ -42,7 +42,7 @@
import { Principal } from "@dfinity/principal";
import { SnsSwapLifecycle } from "@dfinity/sns";
import { isNullish, nonNullish } from "@dfinity/utils";
import { setContext, onDestroy } from "svelte";
import { onDestroy, setContext } from "svelte";
import { writable } from "svelte/store";
export let rootCanisterId: string | undefined | null;
Expand Down Expand Up @@ -148,7 +148,7 @@
let enableOpenProjectWatchers = false;
$: enableOpenProjectWatchers =
$projectDetailStore?.summary?.swap.lifecycle === SnsSwapLifecycle.Open;
$projectDetailStore?.summary?.getLifecycle() === SnsSwapLifecycle.Open;
let swapCanisterId: Principal | undefined;
$: swapCanisterId = $projectDetailStore.summary?.swapCanisterId;
Expand All @@ -175,7 +175,7 @@
$: if (
nonNullish(rootCanisterId) &&
$projectDetailStore.summary?.swap.lifecycle === SnsSwapLifecycle.Committed
$projectDetailStore.summary?.getLifecycle() === SnsSwapLifecycle.Committed
) {
loadSnsFinalizationStatus({
rootCanisterId: Principal.fromText(rootCanisterId),
Expand Down Expand Up @@ -235,7 +235,7 @@
// - no root canister id
// - ticket already in progress for the same root canister id
$: if (
$projectDetailStore.summary?.swap.lifecycle === SnsSwapLifecycle.Open &&
$projectDetailStore.summary?.getLifecycle() === SnsSwapLifecycle.Open &&
$authSignedInStore &&
nonNullish(userCommitment) &&
nonNullish(swapCanisterId) &&
Expand Down
5 changes: 1 addition & 4 deletions frontend/src/lib/services/neurons.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,10 +983,7 @@ export const makeDummyProposals = async (neuronId: NeuronId): Promise<void> => {
const { snsSummariesStore } = await import("../stores/sns.store");
const projects = get(snsSummariesStore);
const pendingProject = projects.find(
({
swap: { lifecycle },
// Use 1 instead of using enum to avoid importing sns-js
}) => lifecycle === 1
(summary) => summary.getLifecycle() === 1
);
await makeDummyProposalsApi({
neuronId,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/services/sns-finalization.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const loadSnsFinalizationStatus = async ({
if (
!forceFetch &&
(isNullish(summary) ||
summary.swap.lifecycle !== SnsSwapLifecycle.Committed ||
summary.getLifecycle() !== SnsSwapLifecycle.Committed ||
swapEndedMoreThanOneWeekAgo({ summary, nowInSeconds: nowInSeconds() }))
) {
return;
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/lib/stores/sns.store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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 { convertDtoToSnsSummary } from "$lib/utils/sns-aggregator-converters.utils";
import { ProposalStatus, type ProposalInfo } from "@dfinity/nns";
Expand Down Expand Up @@ -151,7 +151,7 @@ const overrideLifecycle =
*/
export const snsSummariesStore = derived<
[SnsAggregatorStore, SnsDerivedStateStore, SnsLifecycleStore],
SnsSummary[]
SnsSummaryWrapper[]
>(
[snsAggregatorStore, snsDerivedStateStore, snsLifecycleStore],
([aggregatorData, derivedStates, lifecycles]) => {
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/lib/types/project-detail.context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SnsSummaryWrapper } from "$lib/types/sns-summary-wrapper";
import type { TokenAmountV2 } from "@dfinity/utils";
import type { Writable } from "svelte/store";
import type { SnsSummary, SnsSwapCommitment } from "./sns";
import type { SnsSwapCommitment } from "./sns";

/**
* SnsSummary or SnsSwapCommitment is a valid project
Expand All @@ -9,7 +10,7 @@ import type { SnsSummary, SnsSwapCommitment } from "./sns";
* `undefined` means not found
*/
export type ProjectDetailStore = {
summary: SnsSummary | undefined | null;
summary: SnsSummaryWrapper | undefined | null;
swapCommitment: SnsSwapCommitment | undefined | null;
totalTokensSupply?: TokenAmountV2 | undefined | null;
};
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/lib/types/sns-summary-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
SnsParams,
SnsSwapDerivedState,
SnsSwapInit,
SnsSwapLifecycle,
} from "@dfinity/sns";
import { fromNullable, isNullish } from "@dfinity/utils";

Expand Down Expand Up @@ -56,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 Expand Up @@ -85,4 +90,18 @@ export class SnsSummaryWrapper implements SnsSummary {
lifecycle: newLifecycle,
});
}

public overrideLifecycle(lifecycle: SnsSwapLifecycle): SnsSummaryWrapper {
return this.overrideLifecycleResponse({
...this.lifecycle,
lifecycle: [lifecycle],
});
}

public override(summary: Partial<SnsSummary>): SnsSummaryWrapper {
return new SnsSummaryWrapper({
...this.summary,
...summary,
});
}
}
Loading

0 comments on commit 0525d12

Please sign in to comment.