Skip to content

Commit

Permalink
feat: validate data
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Nov 5, 2024
1 parent b4f39a8 commit 4de89dd
Show file tree
Hide file tree
Showing 3 changed files with 381 additions and 33 deletions.
45 changes: 12 additions & 33 deletions src/frontend/src/icp/types/ic.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { CanisterIdTextSchema } from '$lib/types/canister';
import { CoingeckoCoinsIdSchema } from '$lib/types/coingecko';
import { TokenSchema } from '$lib/types/token';
import {
IcAppMetadataSchema,
IcCanistersSchema,
IcCkInterfaceSchema,
IcCkLinkedAssetsSchema,
IcCkMetadataSchema,
IcCkTokenSchema,
IcFeeSchema,
IcInterfaceSchema,
IcTokenSchema,
IcTokenWithoutIdSchema
} from '$icp/validation/ic-token.validation';
import type { TransactionType } from '$lib/types/transaction';
import type { Option } from '$lib/types/utils';
import { UrlSchema } from '$lib/validation/url.validation';
import type { Transaction, TransactionWithId } from '@dfinity/ledger-icp';
import type {
IcrcTransaction as IcrcTransactionCandid,
Expand Down Expand Up @@ -51,53 +59,24 @@ export interface IcTransactionUi {
txExplorerUrl?: string;
}

const IcFeeSchema = z.object({
fee: z.bigint()
});

export type IcFee = z.infer<typeof IcFeeSchema>;

const IcAppMetadataSchema = z.object({
exchangeCoinId: CoingeckoCoinsIdSchema.optional(),
position: z.number(),
explorerUrl: UrlSchema.optional()
});

export type IcAppMetadata = z.infer<typeof IcAppMetadataSchema>;

const IcCanistersSchema = z.object({
ledgerCanisterId: CanisterIdTextSchema,
indexCanisterId: CanisterIdTextSchema
});

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

const IcCkLinkedAssetsSchema = z.object({
twinToken: TokenSchema,
feeLedgerCanisterId: CanisterIdTextSchema.optional()
});

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

const IcCkMetadataSchema = IcCkLinkedAssetsSchema.partial().extend({
minterCanisterId: CanisterIdTextSchema
});

export type IcCkMetadata = z.infer<typeof IcCkMetadataSchema>;

const IcInterfaceSchema = IcCanistersSchema.merge(IcAppMetadataSchema);
export type IcInterface = z.infer<typeof IcInterfaceSchema>;

const IcTokenSchema = TokenSchema.merge(IcFeeSchema).merge(IcInterfaceSchema);
export type IcToken = z.infer<typeof IcTokenSchema>;

const IcTokenWithoutIdSchema = IcTokenSchema.omit({ id: true });
export type IcTokenWithoutId = z.infer<typeof IcTokenWithoutIdSchema>;

const IcCkTokenSchema = IcTokenSchema.merge(IcCkMetadataSchema.partial());
export type IcCkToken = z.infer<typeof IcCkTokenSchema>;

const IcCkInterfaceSchema = IcInterfaceSchema.merge(IcCkMetadataSchema);
export type IcCkInterface = z.infer<typeof IcCkInterfaceSchema>;

export type OptionIcToken = Option<IcToken>;
Expand Down
39 changes: 39 additions & 0 deletions src/frontend/src/icp/validation/ic-token.validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { CanisterIdTextSchema } from '$lib/types/canister';
import { CoingeckoCoinsIdSchema } from '$lib/types/coingecko';
import { TokenSchema } from '$lib/types/token';
import { UrlSchema } from '$lib/validation/url.validation';
import { z } from 'zod';

export const IcFeeSchema = z.object({
fee: z.bigint()
});

export const IcAppMetadataSchema = z.object({
exchangeCoinId: CoingeckoCoinsIdSchema.optional(),
position: z.number(),
explorerUrl: UrlSchema.optional()
});

export const IcCanistersSchema = z.object({
ledgerCanisterId: CanisterIdTextSchema,
indexCanisterId: CanisterIdTextSchema
});

export const IcCkLinkedAssetsSchema = z.object({
twinToken: TokenSchema,
feeLedgerCanisterId: CanisterIdTextSchema.optional()
});

export const IcCkMetadataSchema = IcCkLinkedAssetsSchema.partial().extend({
minterCanisterId: CanisterIdTextSchema
});

export const IcInterfaceSchema = IcCanistersSchema.merge(IcAppMetadataSchema);

export const IcTokenSchema = TokenSchema.merge(IcFeeSchema).merge(IcInterfaceSchema);

export const IcTokenWithoutIdSchema = IcTokenSchema.omit({ id: true }).strict();

export const IcCkTokenSchema = IcTokenSchema.merge(IcCkMetadataSchema.partial());

export const IcCkInterfaceSchema = IcInterfaceSchema.merge(IcCkMetadataSchema);
Loading

0 comments on commit 4de89dd

Please sign in to comment.