Skip to content

Commit

Permalink
feat: assert index canister id
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Nov 5, 2024
1 parent 320e995 commit f13e646
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import { modalStore } from '$lib/stores/modal.store';
import { token } from '$lib/stores/token.store';
import { last } from '$lib/utils/array.utils';
import { isIcToken, isIcTokenCanistersStrict } from '$icp/utils/ic-token.utils';
let ckEthereum: boolean;
$: ckEthereum = $tokenCkEthLedger || $tokenCkErc20Ledger;
Expand Down Expand Up @@ -69,6 +70,12 @@
return;
}
if (!isIcToken($tokenAsIcToken) || !isIcTokenCanistersStrict($tokenAsIcToken)) {
// On one hand, we assume that the parent component does not mount this component if no transactions can be fetched; on the other hand, we want to avoid displaying an error toast that could potentially appear multiple times.
// Therefore, we do not particularly display a visual error. In any case, we cannot load transactions without an Index canister.
return;
}
await loadNextTransactions({
owner: $authIdentity.getPrincipal(),
identity: $authIdentity,
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/src/icp/services/ic-transactions.services.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getTransactions as getTransactionsIcp } from '$icp/api/icp-index.api';
import { getTransactions as getTransactionsIcrc } from '$icp/api/icrc-index-ng.api';
import { icTransactionsStore } from '$icp/stores/ic-transactions.store';
import type { IcToken } from '$icp/types/ic-token';
import type { IcCanistersStrict, IcToken } from '$icp/types/ic-token';
import type { IcTransaction } from '$icp/types/ic-transaction';
import { mapIcTransaction } from '$icp/utils/ic-transactions.utils';
import { mapTransactionIcpToSelf } from '$icp/utils/icp-transactions.utils';
Expand All @@ -23,7 +23,7 @@ const getTransactions = async ({
identity: OptionIdentity;
start?: bigint;
maxResults?: bigint;
token: IcToken;
token: IcToken & IcCanistersStrict;
}): Promise<IcTransaction[]> => {
if (standard === 'icrc') {
const { transactions } = await getTransactionsIcrc({
Expand All @@ -49,7 +49,7 @@ export const loadNextTransactions = ({
identity: OptionIdentity;
start?: bigint;
maxResults?: bigint;
token: IcToken;
token: IcToken & IcCanistersStrict;
signalEnd: () => void;
}): Promise<void> =>
queryAndUpdate<IcTransaction[]>({
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/src/icp/types/ic-token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
IcAppMetadataSchema,
IcCanistersSchema,
IcCanistersSchema, IcCanistersStrictSchema,
IcCkInterfaceSchema,
IcCkLinkedAssetsSchema,
IcCkMetadataSchema,
Expand All @@ -19,6 +19,8 @@ export type IcAppMetadata = z.infer<typeof IcAppMetadataSchema>;

export type IcCanisters = z.infer<typeof IcCanistersSchema>;

export type IcCanistersStrict = z.infer<typeof IcCanistersStrictSchema>;

export type IcCkLinkedAssets = z.infer<typeof IcCkLinkedAssetsSchema>;

export type IcCkMetadata = z.infer<typeof IcCkMetadataSchema>;
Expand Down
13 changes: 13 additions & 0 deletions src/frontend/src/icp/utils/ic-token.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { IcCanistersStrict, IcToken } from '$icp/types/ic-token';
import { IcCanistersStrictSchema, IcTokenSchema } from '$icp/validation/ic-token.validation';
import type { Token } from '$lib/types/token';

export const isIcToken = (token: Token): token is IcToken => {
const { success } = IcTokenSchema.safeParse(token);
return success;
};

export const isIcTokenCanistersStrict = (token: IcToken): token is IcToken & IcCanistersStrict => {
const { success } = IcCanistersStrictSchema.safeParse(token);
return success;
};
16 changes: 16 additions & 0 deletions src/frontend/src/tests/icp/validation/ic-token.validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,28 @@ describe('Schema Validation Tests', () => {
});

describe('IcCanistersStrictSchema', () => {
const validToken = {
...mockToken,
...mockFee,
...mockCanisters,
...mockApp
};

it('should validate with correct data', () => {
expect(IcCanistersSchema.parse(validToken)).toEqual(validToken);
});

it('should fail with missing index canister field', () => {
const invalidData = {
ledgerCanisterId: IC_CKBTC_LEDGER_CANISTER_ID
};
expect(() => IcCanistersStrictSchema.parse(invalidData)).toThrow();
});

it('should fail for token with missing index canister field', () => {
const { indexCanisterId: _, ...tokenWithoutIndexCanisterId } = validToken;
expect(() => IcCanistersStrictSchema.parse(tokenWithoutIndexCanisterId)).toThrow();
});
});

describe('IcCkLinkedAssetsSchema', () => {
Expand Down

0 comments on commit f13e646

Please sign in to comment.