Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

housekeeping: removes fillTokensStoreFromAggregatorData #5943

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 1 addition & 39 deletions frontend/src/lib/utils/icrc-tokens.utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { IMPORTANT_CK_TOKEN_IDS } from "$lib/constants/tokens.constants";
import type { TokensStore, TokensStoreData } from "$lib/stores/tokens.store";
import type { IcrcTokenMetadata } from "$lib/types/icrc";
import type { CachedSnsDto } from "$lib/types/sns-aggregator";
import { convertIcrc1Metadata } from "$lib/utils/sns-aggregator-converters.utils";
import {
IcrcMetadataResponseEntries,
type IcrcTokenMetadataResponse,
} from "@dfinity/ledger-icrc";
import type { Principal } from "@dfinity/principal";
import { isNullish, nonNullish } from "@dfinity/utils";
import { isNullish } from "@dfinity/utils";

/**
* Token metadata is given only if the properties NNS-dapp needs (name, symbol and fee) are defined.
Expand Down Expand Up @@ -54,41 +51,6 @@ export const mapOptionalToken = (
return nullishToken as IcrcTokenMetadata;
};

export const fillTokensStoreFromAggregatorData = ({
tokensStore,
aggregatorData,
}: {
tokensStore: TokensStore;
aggregatorData: CachedSnsDto[];
}) => {
tokensStore.setTokens(
aggregatorData
.map(({ icrc1_metadata, canister_ids }) => ({
token: mapOptionalToken(convertIcrc1Metadata(icrc1_metadata)),
// TODO: Remove root_canister_id and only used ledger_canister_id.
root_canister_id: canister_ids.root_canister_id,
ledger_canister_id: canister_ids.ledger_canister_id,
}))
.filter(({ token }) => nonNullish(token))
.reduce(
(acc, { root_canister_id, ledger_canister_id, token }) => ({
...acc,
[root_canister_id]: {
// Above filter ensure the token is not undefined therefore it can be safely cast
token: token as IcrcTokenMetadata,
certified: true,
},
[ledger_canister_id]: {
// Above filter ensure the token is not undefined therefore it can be safely cast
token: token as IcrcTokenMetadata,
certified: true,
},
}),
{} as TokensStoreData
)
);
};

export const isImportantCkToken = ({
ledgerCanisterId,
}: {
Expand Down
37 changes: 1 addition & 36 deletions frontend/src/tests/lib/utils/icrc-tokens.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
import {
OWN_CANISTER_ID,
OWN_CANISTER_ID_TEXT,
} from "$lib/constants/canister-ids.constants";
import { OWN_CANISTER_ID } from "$lib/constants/canister-ids.constants";
import {
CKBTC_LEDGER_CANISTER_ID,
CKTESTBTC_LEDGER_CANISTER_ID,
} from "$lib/constants/ckbtc-canister-ids.constants";
import { CKETH_LEDGER_CANISTER_ID } from "$lib/constants/cketh-canister-ids.constants";
import { CKUSDC_LEDGER_CANISTER_ID } from "$lib/constants/ckusdc-canister-ids.constants";
import { tokensStore } from "$lib/stores/tokens.store";
import {
fillTokensStoreFromAggregatorData,
isImportantCkToken,
mapOptionalToken,
} from "$lib/utils/icrc-tokens.utils";
import {
aggregatorSnsMockDto,
aggregatorTokenMock,
} from "$tests/mocks/sns-aggregator.mock";
import {
mockQueryTokenResponse,
mockSnsToken,
principal,
} from "$tests/mocks/sns-projects.mock";
import { IcrcMetadataResponseEntries } from "@dfinity/ledger-icrc";
import { get } from "svelte/store";

describe("ICRC tokens utils", () => {
describe("mapOptionalToken", () => {
Expand Down Expand Up @@ -76,31 +66,6 @@ describe("ICRC tokens utils", () => {
});
});

describe("fillTokensStoreFromAggregatorData", () => {
it("should fill the tokens store with SNS tokens", () => {
expect(Object.keys(get(tokensStore))).toEqual([OWN_CANISTER_ID_TEXT]);
fillTokensStoreFromAggregatorData({
tokensStore,
aggregatorData: [aggregatorSnsMockDto],
});
const tokensStoreData = get(tokensStore);

expect(Object.keys(tokensStoreData)).toEqual([
OWN_CANISTER_ID_TEXT,
aggregatorSnsMockDto.canister_ids.root_canister_id,
aggregatorSnsMockDto.canister_ids.ledger_canister_id,
]);
expect(
tokensStoreData[aggregatorSnsMockDto.canister_ids.ledger_canister_id]
.token
).toEqual(aggregatorTokenMock);
expect(
tokensStoreData[aggregatorSnsMockDto.canister_ids.root_canister_id]
.token
).toEqual(aggregatorTokenMock);
});
});

describe("isImportantCkToken", () => {
it("should return true for important token ledger canisters", () => {
expect(
Expand Down
Loading