From f3e8faefbd461b69e6343c81c75978b146b6bc9b Mon Sep 17 00:00:00 2001 From: Max Strasinsky Date: Fri, 30 Aug 2024 11:27:21 +0200 Subject: [PATCH] Add imported tokens to icrcCanistersStore --- .../lib/services/imported-tokens.services.ts | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/frontend/src/lib/services/imported-tokens.services.ts b/frontend/src/lib/services/imported-tokens.services.ts index f77f6c568cc..686cdce003f 100644 --- a/frontend/src/lib/services/imported-tokens.services.ts +++ b/frontend/src/lib/services/imported-tokens.services.ts @@ -10,6 +10,7 @@ import type { ImportedTokens } from "$lib/canisters/nns-dapp/nns-dapp.types"; import { MAX_IMPORTED_TOKENS } from "$lib/constants/imported-tokens.constants"; import { FORCE_CALL_STRATEGY } from "$lib/constants/mockable.constants"; import { getAuthenticatedIdentity } from "$lib/services/auth.services"; +import { icrcCanistersStore } from "$lib/stores/icrc-canisters.store"; import { importedTokensStore } from "$lib/stores/imported-tokens.store"; import { toastsError, toastsSuccess } from "$lib/stores/toasts.store"; import type { ImportedTokenData } from "$lib/types/imported-tokens"; @@ -19,6 +20,7 @@ import { toImportedTokenData, } from "$lib/utils/imported-tokens.utils"; import { isNullish } from "@dfinity/utils"; +import { get } from "svelte/store"; import { queryAndUpdate } from "./utils.services"; /** Load imported tokens from the `nns-dapp` backend and update the `importedTokensStore` store. @@ -32,11 +34,26 @@ export const loadImportedTokens = async ({ return queryAndUpdate({ request: (options) => getImportedTokens(options), strategy: FORCE_CALL_STRATEGY, - onLoad: ({ response: { imported_tokens: importedTokens }, certified }) => + onLoad: ({ + response: { imported_tokens: rawImportedTokens }, + certified, + }) => { + const importedTokens = rawImportedTokens.map(toImportedTokenData); importedTokensStore.set({ - importedTokens: importedTokens.map(toImportedTokenData), + importedTokens, certified, - }), + }); + + const icrcCanistersStoreData = get(icrcCanistersStore); + for (const { ledgerCanisterId, indexCanisterId } of importedTokens) { + if (isNullish(icrcCanistersStoreData[ledgerCanisterId.toText()])) { + icrcCanistersStore.setCanisters({ + ledgerCanisterId, + indexCanisterId, + }); + } + } + }, onError: ({ error: err, certified }) => { console.error(err); @@ -97,8 +114,6 @@ export const addImportedToken = async ({ tokenToAdd: ImportedTokenData; importedTokens: ImportedTokenData[]; }): Promise<{ success: boolean }> => { - // TODO: validate importedToken (not sns, not ck, is unique, etc.) - const tokens = [...importedTokens, tokenToAdd]; const { err } = await saveImportedToken({ tokens });