UNPKG

@yoroi/portfolio

Version:

The Portfolio package of Yoroi SDK

58 lines 2.74 kB
import { Portfolio } from '@yoroi/types'; import { cacheRecordSchemaMaker } from '@yoroi/common'; import { z } from 'zod'; import { TokenStatusSchema } from './token-status'; import { TokenApplicationSchema } from './token-application'; import { TokenIdSchema } from './token-id'; import { TokenTypeSchema } from './token-type'; import { responseRecordWithCacheSchemaMaker } from './response-record-with-cache-schema-maker'; import { primaryTokenId } from '../constants'; export const CommonTokenInfoSchema = z.object({ decimals: z.number().nonnegative(), ticker: z.string(), name: z.string(), symbol: z.string(), status: TokenStatusSchema, application: TokenApplicationSchema, tag: z.string(), reference: z.string(), fingerprint: z.string(), website: z.string(), originalImage: z.string(), description: z.string().optional(), icon: z.string().optional(), mediaType: z.string().optional() }); export const PrimaryTokenInfoSchema = CommonTokenInfoSchema.merge(z.object({ id: z.literal(primaryTokenId), nature: z.literal(Portfolio.Token.Nature.Primary), type: z.literal(Portfolio.Token.Type.FT) })); export const SecondaryTokenInfoSchema = CommonTokenInfoSchema.merge(z.object({ id: TokenIdSchema, nature: z.literal(Portfolio.Token.Nature.Secondary), type: TokenTypeSchema })); export const SecondaryTokenInfoApiResponseSchema = CommonTokenInfoSchema.merge(z.object({ id: TokenIdSchema, nature: z.literal(Portfolio.Token.Nature.Secondary).optional(), type: TokenTypeSchema })); export const TokenInfoSchema = z.union([PrimaryTokenInfoSchema, SecondaryTokenInfoSchema]); export const isPrimaryTokenInfo = data => PrimaryTokenInfoSchema.safeParse(data).success; export const isSecondaryTokenInfo = data => SecondaryTokenInfoSchema.safeParse(data).success; export const isTokenInfo = data => isPrimaryTokenInfo(data) || isSecondaryTokenInfo(data); export const parseTokenInfo = data => { return isTokenInfo(data) ? data : undefined; }; export const SecondaryTokenInfoApiResponseWithCacheRecordSchema = responseRecordWithCacheSchemaMaker(SecondaryTokenInfoApiResponseSchema); export const isSecondaryTokenInfoWithCacheRecord = data => SecondaryTokenInfoApiResponseWithCacheRecordSchema.safeParse(data).success; export const parseSecondaryTokenInfoWithCacheRecord = data => { return isSecondaryTokenInfoWithCacheRecord(data) ? data : undefined; }; export const TokenInfoWithCacheRecordSchema = cacheRecordSchemaMaker(TokenInfoSchema); export const isTokenInfoWithCacheRecord = data => TokenInfoWithCacheRecordSchema.safeParse(data).success; export const parseTokenInfoWithCacheRecord = data => { return isTokenInfoWithCacheRecord(data) ? data : undefined; }; //# sourceMappingURL=token-info.js.map