Skip to content

Commit

Permalink
Filter out SNSes without lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
dskloetd committed Nov 21, 2024
1 parent 5415456 commit 284e405
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 38 deletions.
5 changes: 4 additions & 1 deletion frontend/src/lib/stores/sns-aggregator.store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CachedSnsDto } from "$lib/types/sns-aggregator";
import { SnsSwapLifecycle } from "@dfinity/sns";
import { nonNullish } from "@dfinity/utils";
import { derived, writable, type Readable } from "svelte/store";

/**
Expand Down Expand Up @@ -35,7 +36,9 @@ export const snsAggregatorStore: SnsAggregatorStore = derived(
snsAggregatorIncludingAbortedProjectsStore,
(store) => ({
data: store.data?.filter(
(sns) => sns.lifecycle.lifecycle !== SnsSwapLifecycle.Aborted
(sns) =>
nonNullish(sns.lifecycle) &&
sns.lifecycle.lifecycle !== SnsSwapLifecycle.Aborted
),
})
);
2 changes: 1 addition & 1 deletion frontend/src/lib/types/sns-aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,5 @@ export type CachedSnsDto = {
derived_state: CachedSnsSwapDerivedDto;
swap_params: CachedSwapParamsResponseDto;
init: CachedInitResponseDto;
lifecycle: CachedLifecycleResponseDto;
lifecycle: CachedLifecycleResponseDto | null;
};
33 changes: 20 additions & 13 deletions frontend/src/lib/utils/sns-aggregator-converters.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,35 +467,42 @@ const convertDtoToSnsSummarySwap = (
};

const convertDtoToLifecycle = (
data: CachedLifecycleResponseDto
): SnsGetLifecycleResponse => ({
decentralization_sale_open_timestamp_seconds: toNullable(
convertOptionalNumToBigInt(
data.decentralization_sale_open_timestamp_seconds
)
),
lifecycle: toNullable(data.lifecycle),
// TODO: Add support in SNS Aggregaro for these fields
decentralization_swap_termination_timestamp_seconds: [],
});
data: CachedLifecycleResponseDto | null
): SnsGetLifecycleResponse | undefined => {
if (isNullish(data)) {
return undefined;
}
return {
decentralization_sale_open_timestamp_seconds: toNullable(
convertOptionalNumToBigInt(
data.decentralization_sale_open_timestamp_seconds
)
),
lifecycle: toNullable(data.lifecycle),
// TODO: Add support in SNS Aggregaro for these fields
decentralization_swap_termination_timestamp_seconds: [],
};
};

type PartialSummary = Omit<
SnsSummary,
"metadata" | "token" | "swap" | "init" | "swapParams"
"metadata" | "token" | "swap" | "init" | "swapParams" | "lifecycle"
> & {
metadata?: SnsSummaryMetadata;
token?: IcrcTokenMetadata;
swap?: SnsSummarySwap;
init?: SnsSwapInit;
swapParams?: SnsParams;
lifecycle?: SnsGetLifecycleResponse;
};

const isValidSummary = (entry: PartialSummary): entry is SnsSummary =>
nonNullish(entry.metadata) &&
nonNullish(entry.token) &&
nonNullish(entry.swap) &&
nonNullish(entry.init) &&
nonNullish(entry.swapParams);
nonNullish(entry.swapParams) &&
nonNullish(entry.lifecycle);

export const convertDtoToSnsSummary = ({
canister_ids: {
Expand Down
74 changes: 51 additions & 23 deletions frontend/src/tests/lib/stores/sns-aggregator.store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import type { CachedSnsDto } from "$lib/types/sns-aggregator";
import { aggregatorMockSnsesDataDto } from "$tests/mocks/sns-aggregator.mock";
import { SnsSwapLifecycle } from "@dfinity/sns";
import { nonNullish } from "@dfinity/utils";
import { get } from "svelte/store";

describe("sns-aggregator store", () => {
Expand Down Expand Up @@ -57,10 +58,13 @@ describe("sns-aggregator store", () => {
});

describe("snsAggregatorStore", () => {
const snsWithLifecycle = (
sns: CachedSnsDto,
lifecycle: SnsSwapLifecycle
) => ({
const snsWithLifecycle = ({
sns,
lifecycle,
}: {
sns: CachedSnsDto;
lifecycle: SnsSwapLifecycle;
}) => ({
...sns,
swap_state: {
...sns.swap_state,
Expand All @@ -69,20 +73,33 @@ describe("sns-aggregator store", () => {
lifecycle,
},
},
lifecycle: {
...sns.lifecycle,
lifecycle,
},
...(nonNullish(lifecycle)
? {
lifecycle: {
...sns.lifecycle,
lifecycle,
},
}
: { lifecycle: null }),
});

const nonAbortedData = [
snsWithLifecycle(aggregatorMockSnsesDataDto[0], SnsSwapLifecycle.Pending),
snsWithLifecycle(aggregatorMockSnsesDataDto[1], SnsSwapLifecycle.Open),
snsWithLifecycle(
aggregatorMockSnsesDataDto[2],
SnsSwapLifecycle.Committed
),
snsWithLifecycle(aggregatorMockSnsesDataDto[3], SnsSwapLifecycle.Adopted),
snsWithLifecycle({
sns: aggregatorMockSnsesDataDto[0],
lifecycle: SnsSwapLifecycle.Pending,
}),
snsWithLifecycle({
sns: aggregatorMockSnsesDataDto[1],
lifecycle: SnsSwapLifecycle.Open,
}),
snsWithLifecycle({
sns: aggregatorMockSnsesDataDto[2],
lifecycle: SnsSwapLifecycle.Committed,
}),
snsWithLifecycle({
sns: aggregatorMockSnsesDataDto[3],
lifecycle: SnsSwapLifecycle.Adopted,
}),
];

it("should start empty", () => {
Expand All @@ -95,18 +112,29 @@ describe("sns-aggregator store", () => {
});

it("should not hold aborted projects", () => {
const abortedProject1 = snsWithLifecycle(
aggregatorMockSnsesDataDto[4],
SnsSwapLifecycle.Aborted
);
const abortedProject2 = snsWithLifecycle(
aggregatorMockSnsesDataDto[5],
SnsSwapLifecycle.Aborted
);
const abortedProject1 = snsWithLifecycle({
sns: aggregatorMockSnsesDataDto[4],
lifecycle: SnsSwapLifecycle.Aborted,
});
const abortedProject2 = snsWithLifecycle({
sns: aggregatorMockSnsesDataDto[5],
lifecycle: SnsSwapLifecycle.Aborted,
});
const data = [abortedProject1, ...nonAbortedData, abortedProject2];

snsAggregatorIncludingAbortedProjectsStore.setData(data);
expect(get(snsAggregatorStore).data).toEqual(nonAbortedData);
});

it("should not hold projects without lifecycle", () => {
const projectWithoutLifecycle = snsWithLifecycle({
sns: aggregatorMockSnsesDataDto[4],
lifecycle: null,
});
const data = [projectWithoutLifecycle, ...nonAbortedData];

snsAggregatorIncludingAbortedProjectsStore.setData(data);
expect(get(snsAggregatorStore).data).toEqual(nonAbortedData);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,16 @@ describe("sns aggregator converters utils", () => {
).toBeUndefined();
});

it("returns undefined if a lifecycle required field is missing", () => {
const aggregatorMissingLifecycle: CachedSnsDto = {
...mockData,
lifecycle: null,
};
expect(
convertDtoToSnsSummary(aggregatorMissingLifecycle)
).toBeUndefined();
});

it("converts fields related to NF participation enhancements", () => {
const aggregatorNFAndDirectParticipationFields: CachedSnsDto = {
...mockData,
Expand Down

0 comments on commit 284e405

Please sign in to comment.