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

Default icrc canisters store #5383

Merged
merged 16 commits into from
Sep 2, 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
2 changes: 1 addition & 1 deletion frontend/src/lib/components/ic/GetTokensModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import { tokensByUniverseIdStore } from "$lib/derived/tokens.derived";
import { universesStore } from "$lib/derived/universes.derived";
import { getICPs, getBTC, getIcrcTokens } from "$lib/services/dev.services";
import { icrcCanistersStore } from "$lib/stores/icrc-canisters.store";
import { icrcCanistersStore } from "$lib/derived/icrc-canisters.derived";
import { toastsError } from "$lib/stores/toasts.store";
import type { Universe } from "$lib/types/universe";
import { isUniverseCkBTC, isUniverseNns } from "$lib/utils/universe.utils";
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/lib/derived/debug.derived.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { icpAccountsStore } from "$lib/derived/icp-accounts.derived";
import { snsProjectsStore } from "$lib/derived/sns/sns-projects.derived";
import { canistersStore } from "$lib/stores/canisters.store";
import { defaultIcrcCanistersStore } from "$lib/stores/default-icrc-canisters.store";
import { icrcAccountsStore } from "$lib/stores/icrc-accounts.store";
import { icrcCanistersStore } from "$lib/stores/icrc-canisters.store";
import { icrcTransactionsStore } from "$lib/stores/icrc-transactions.store";
import { knownNeuronsStore } from "$lib/stores/known-neurons.store";
import { neuronsStore } from "$lib/stores/neurons.store";
Expand Down Expand Up @@ -117,7 +117,7 @@ export const initDebugStore = () =>
snsAggregatorStore,
tokensStore,
icrcAccountsStore,
icrcCanistersStore,
defaultIcrcCanistersStore,
],
([
$busyStore,
Expand Down Expand Up @@ -145,7 +145,7 @@ export const initDebugStore = () =>
$aggregatorStore,
$tokensStore,
$icrcAccountsStore,
$icrcCanistersStore,
$defaultIcrcCanistersStore,
]) => ({
busy: $busyStore,
accounts: $accountsStore,
Expand All @@ -172,6 +172,6 @@ export const initDebugStore = () =>
aggregatorStore: $aggregatorStore,
tokensStore: $tokensStore,
icrcAccountsStore: $icrcAccountsStore,
icrcCanistersStore: $icrcCanistersStore,
defaultIcrcCanistersStore: $defaultIcrcCanistersStore,
})
);
34 changes: 34 additions & 0 deletions frontend/src/lib/derived/icrc-canisters.derived.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { defaultIcrcCanistersStore } from "$lib/stores/default-icrc-canisters.store";
import { importedTokensStore } from "$lib/stores/imported-tokens.store";
import type { UniverseCanisterIdText } from "$lib/types/universe";
import type { Principal } from "@dfinity/principal";
import { derived, type Readable } from "svelte/store";

export interface IcrcCanisters {
ledgerCanisterId: Principal;
indexCanisterId: Principal | undefined;
}

export type IcrcCanistersStoreData = Record<
UniverseCanisterIdText,
IcrcCanisters
>;

export type IcrcCanistersStore = Readable<IcrcCanistersStoreData>;

export const icrcCanistersStore: IcrcCanistersStore = derived(
[defaultIcrcCanistersStore, importedTokensStore],
([defaultIcrcCanisters, importedTokensStore]) => {
return {
...defaultIcrcCanisters,
...Object.fromEntries(
importedTokensStore?.importedTokens?.map(
({ ledgerCanisterId, indexCanisterId }) => [
ledgerCanisterId.toText(),
{ ledgerCanisterId, indexCanisterId },
]
) ?? []
),
};
}
);
2 changes: 1 addition & 1 deletion frontend/src/lib/derived/icrc-universes.derived.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import {
icrcCanistersStore,
type IcrcCanisters,
} from "$lib/stores/icrc-canisters.store";
} from "$lib/derived/icrc-canisters.derived";
import { tokensStore, type TokensStoreData } from "$lib/stores/tokens.store";
import type { Universe } from "$lib/types/universe";
import { isNullish, nonNullish } from "@dfinity/utils";
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/lib/derived/selectable-universes.derived.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { pageStore, type Page } from "$lib/derived/page.derived";
import {
icrcCanistersStore,
type IcrcCanistersStore,
type IcrcCanistersStoreData,
} from "$lib/stores/icrc-canisters.store";
} from "$lib/derived/icrc-canisters.derived";
import { pageStore, type Page } from "$lib/derived/page.derived";
import type { Universe } from "$lib/types/universe";
import { isAllTokensPath, isUniverseCkBTC } from "$lib/utils/universe.utils";
import { isNullish } from "@dfinity/utils";
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/lib/derived/selected-universe.derived.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import {
OWN_CANISTER_ID_TEXT,
} from "$lib/constants/canister-ids.constants";
import { CKBTC_UNIVERSE_CANISTER_ID } from "$lib/constants/ckbtc-canister-ids.constants";
import {
icrcCanistersStore,
type IcrcCanistersStore,
type IcrcCanistersStoreData,
} from "$lib/derived/icrc-canisters.derived";
import { pageStore, type Page } from "$lib/derived/page.derived";
import { selectableUniversesStore } from "$lib/derived/selectable-universes.derived";
import {
ENABLE_CKBTC,
ENABLE_CKTESTBTC,
} from "$lib/stores/feature-flags.store";
import {
icrcCanistersStore,
type IcrcCanistersStore,
type IcrcCanistersStoreData,
} from "$lib/stores/icrc-canisters.store";
import type { Universe, UniverseCanisterId } from "$lib/types/universe";
import {
isAllTokensPath,
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/lib/directives/debug.directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ const anonymiseStoreState = async () => {
aggregatorStore,
tokensStore,
icrcAccountsStore,
icrcCanistersStore,
} = get(debugStore);

return {
Expand Down Expand Up @@ -291,7 +290,6 @@ const anonymiseStoreState = async () => {
),
aggregatorStore,
tokensStore,
icrcCanistersStore,
icrcAccountsStore: await anonymizeSnsTypeStore(
icrcAccountsStore,
async ({ certified, accounts }) => ({
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/pages/IcrcWallet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import NoTransactions from "$lib/components/accounts/NoTransactions.svelte";
import { selectedIcrcTokenUniverseIdStore } from "$lib/derived/selected-universe.derived";
import { tokensByUniverseIdStore } from "$lib/derived/tokens.derived";
import { icrcCanistersStore } from "$lib/stores/icrc-canisters.store";
import { icrcCanistersStore } from "$lib/derived/icrc-canisters.derived";
import type { CanisterId } from "$lib/types/canister";
import type { IcrcTokenMetadata } from "$lib/types/icrc";
import type { WalletStore } from "$lib/types/wallet.context";
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/lib/services/icrc-canisters.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import {
CKUSDC_INDEX_CANISTER_ID,
CKUSDC_LEDGER_CANISTER_ID,
} from "$lib/constants/ckusdc-canister-ids.constants";
import { defaultIcrcCanistersStore } from "$lib/stores/default-icrc-canisters.store";
import { ENABLE_CKTESTBTC } from "$lib/stores/feature-flags.store";
import { icrcCanistersStore } from "$lib/stores/icrc-canisters.store";
import { isNullish } from "@dfinity/utils";
import { get } from "svelte/store";

export const loadIcrcCanisters = async () => {
const storeData = get(icrcCanistersStore);
const storeData = get(defaultIcrcCanistersStore);
// To avoid rerendering the UI and possibly triggering new requests
// We don't change the store if it's already filled.
if (isNullish(storeData[CKETH_LEDGER_CANISTER_ID.toText()])) {
icrcCanistersStore.setCanisters({
defaultIcrcCanistersStore.setCanisters({
ledgerCanisterId: CKETH_LEDGER_CANISTER_ID,
indexCanisterId: CKETH_INDEX_CANISTER_ID,
});
Expand All @@ -27,13 +27,13 @@ export const loadIcrcCanisters = async () => {
get(ENABLE_CKTESTBTC) &&
isNullish(storeData[CKETHSEPOLIA_LEDGER_CANISTER_ID.toText()])
) {
icrcCanistersStore.setCanisters({
defaultIcrcCanistersStore.setCanisters({
ledgerCanisterId: CKETHSEPOLIA_LEDGER_CANISTER_ID,
indexCanisterId: CKETHSEPOLIA_INDEX_CANISTER_ID,
});
}
if (isNullish(storeData[CKUSDC_LEDGER_CANISTER_ID.toText()])) {
icrcCanistersStore.setCanisters({
defaultIcrcCanistersStore.setCanisters({
ledgerCanisterId: CKUSDC_LEDGER_CANISTER_ID,
indexCanisterId: CKUSDC_INDEX_CANISTER_ID,
});
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/services/icrc-tokens.services.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { icrcCanistersStore } from "$lib/stores/icrc-canisters.store";
import { icrcCanistersStore } from "$lib/derived/icrc-canisters.derived";
import type { Unsubscriber } from "svelte/store";
import { loadIcrcToken } from "./icrc-accounts.services";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import { nonNullish } from "@dfinity/utils";
import type { Readable } from "svelte/store";
import { writable } from "svelte/store";

export interface IcrcCanisters {
export interface DefaultIcrcCanisters {
ledgerCanisterId: Principal;
indexCanisterId: Principal;
}

export type IcrcCanistersStoreData = Record<
export type DefaultIcrcCanistersStoreData = Record<
UniverseCanisterIdText,
IcrcCanisters
DefaultIcrcCanisters
>;

export interface IcrcCanistersStore extends Readable<IcrcCanistersStoreData> {
setCanisters: (data: IcrcCanisters) => void;
export interface IcrcCanistersStore
extends Readable<DefaultIcrcCanistersStoreData> {
setCanisters: (data: DefaultIcrcCanisters) => void;
reset: () => void;
}

Expand All @@ -28,18 +29,18 @@ export interface IcrcCanistersStore extends Readable<IcrcCanistersStoreData> {
* - reset: reset all information.
*
*/
const initIcrcCanistersStore = (): IcrcCanistersStore => {
const initialIcrcCanistersStoreData: IcrcCanistersStoreData = {};
const initDefaultIcrcCanistersStore = (): IcrcCanistersStore => {
const initialIcrcCanistersStoreData: DefaultIcrcCanistersStoreData = {};

const { subscribe, update, set } = writable<IcrcCanistersStoreData>(
const { subscribe, update, set } = writable<DefaultIcrcCanistersStoreData>(
initialIcrcCanistersStoreData
);

return {
subscribe,

setCanisters({ ledgerCanisterId, indexCanisterId }: IcrcCanisters) {
update((state: IcrcCanistersStoreData) => ({
setCanisters({ ledgerCanisterId, indexCanisterId }: DefaultIcrcCanisters) {
update((state: DefaultIcrcCanistersStoreData) => ({
...state,
[ledgerCanisterId.toText()]: {
ledgerCanisterId,
Expand All @@ -55,7 +56,7 @@ const initIcrcCanistersStore = (): IcrcCanistersStore => {
};
};

export const icrcCanistersStore = initIcrcCanistersStore();
export const defaultIcrcCanistersStore = initDefaultIcrcCanistersStore();

// Useful to help people record coins they accidentally sent to NNS dapp, but
// which are not officially supported by the NNS dapp.
Expand All @@ -75,7 +76,7 @@ if (browser) {
ledgerCanisterId: string,
indexCanisterId?: string
) => {
icrcCanistersStore.setCanisters({
defaultIcrcCanistersStore.setCanisters({
ledgerCanisterId: Principal.fromText(ledgerCanisterId),
indexCanisterId: nonNullish(indexCanisterId)
? Principal.fromText(indexCanisterId)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/utils/universe.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
CKTESTBTC_UNIVERSE_CANISTER_ID,
} from "$lib/constants/ckbtc-canister-ids.constants";
import { AppPath } from "$lib/constants/routes.constants";
import type { IcrcCanistersStoreData } from "$lib/derived/icrc-canisters.derived";
import type { Page } from "$lib/derived/page.derived";
import { i18n } from "$lib/stores/i18n";
import type { IcrcCanistersStoreData } from "$lib/stores/icrc-canisters.store";
import type { SnsSummary } from "$lib/types/sns";
import type { Universe } from "$lib/types/universe";
import { replacePlaceholders } from "$lib/utils/i18n.utils";
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/routes/(app)/(nns)/tokens/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import {
icrcCanistersStore,
type IcrcCanistersStoreData,
} from "$lib/stores/icrc-canisters.store";
} from "$lib/derived/icrc-canisters.derived";
import type { Account } from "$lib/types/account";
import { ActionType, type Action } from "$lib/types/actions";
import type { CkBTCAdditionalCanisters } from "$lib/types/ckbtc-canisters";
Expand Down
78 changes: 78 additions & 0 deletions frontend/src/tests/lib/derived/icrc-canisters.derived.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { icrcCanistersStore } from "$lib/derived/icrc-canisters.derived";
import { defaultIcrcCanistersStore } from "$lib/stores/default-icrc-canisters.store";
import { importedTokensStore } from "$lib/stores/imported-tokens.store";
import { principal } from "$tests/mocks/sns-projects.mock";
import { get } from "svelte/store";

describe("icrcCanistersStore", () => {
const ledgerCanisterId = principal(0);
const indexCanisterId = principal(1);
const ledgerCanisterId2 = principal(2);

beforeEach(() => {
defaultIcrcCanistersStore.reset();
importedTokensStore.reset();
});

it("returns empty object when no icrc tokens are present", () => {
expect(get(icrcCanistersStore)).toEqual({});
});

it("return data from defaultIcrcCanistersStore", () => {
defaultIcrcCanistersStore.setCanisters({
ledgerCanisterId,
indexCanisterId,
});

expect(get(icrcCanistersStore)).toEqual({
[ledgerCanisterId.toText()]: {
ledgerCanisterId,
indexCanisterId,
},
});
});

it("return data from importedTokensStore", () => {
importedTokensStore.set({
importedTokens: [
{
ledgerCanisterId,
indexCanisterId: undefined,
},
],
certified: true,
});

expect(get(icrcCanistersStore)).toEqual({
[ledgerCanisterId.toText()]: {
ledgerCanisterId,
},
});
});

it("return data from the defaultIcrcCanistersStore and the importedTokensStore", () => {
defaultIcrcCanistersStore.setCanisters({
ledgerCanisterId,
indexCanisterId,
});
importedTokensStore.set({
importedTokens: [
{
ledgerCanisterId: ledgerCanisterId2,
indexCanisterId: undefined,
},
],
certified: true,
});

expect(get(icrcCanistersStore)).toEqual({
[ledgerCanisterId.toText()]: {
ledgerCanisterId,
indexCanisterId,
},
[ledgerCanisterId2.toText()]: {
ledgerCanisterId: ledgerCanisterId2,
},
});
});
});
Loading