diff --git a/packages/metadata/src/providers/githubMetadata.provider.ts b/packages/metadata/src/providers/githubMetadata.provider.ts index 2203815..4c533e5 100644 --- a/packages/metadata/src/providers/githubMetadata.provider.ts +++ b/packages/metadata/src/providers/githubMetadata.provider.ts @@ -2,7 +2,6 @@ import axios, { AxiosInstance } from "axios"; import { z } from "zod"; import { - Address, ILogger, Token, TokenType, @@ -12,31 +11,7 @@ import { import { IMetadataProvider } from "../interfaces/index.js"; import { FetchError, InvalidSchema } from "../internal.js"; - -const TokenSchema = z.object({ - name: z.string(), - symbol: z.string(), - coingeckoId: z.string(), // FIXME: on pricing refactor, this should not be part of the token metadata - type: z.union([z.literal("erc20"), z.literal("native")]), - contractAddress: z - .string() - .regex(/^0x[a-fA-F0-9]{40}$/) - .transform((v) => (v ? (v as Address) : null)) - .nullable(), - decimals: z.number(), - imageUrl: z.string().optional(), -}); - -const ChainSchema = z.object({ - chainId: z.number().positive(), - name: z.string(), - iconUrl: z.string().url().optional(), - publicRpcs: z.array(z.string().url()).default([]), - explorerUrl: z.string().url().optional(), - launchDate: z.number().positive(), - chainType: z.union([z.literal("Rollup"), z.literal("Validium")]), - baseToken: TokenSchema, -}); +import { ChainSchema, TokenSchema } from "../schemas/index.js"; /** * Represents a provider for retrieving metadata from GitHub. diff --git a/packages/metadata/src/schemas/index.ts b/packages/metadata/src/schemas/index.ts new file mode 100644 index 0000000..940a288 --- /dev/null +++ b/packages/metadata/src/schemas/index.ts @@ -0,0 +1,28 @@ +import { z } from "zod"; + +import { Address } from "@zkchainhub/shared"; + +export const TokenSchema = z.object({ + name: z.string(), + symbol: z.string(), + coingeckoId: z.string(), // FIXME: on pricing refactor, this should not be part of the token metadata + type: z.union([z.literal("erc20"), z.literal("native")]), + contractAddress: z + .custom
((val) => { + return typeof val === "string" && /^0x[a-fA-F0-9]{40}$/.test(val); + }, "Invalid Ethereum address") + .nullable(), + decimals: z.number(), + imageUrl: z.string().optional(), +}); + +export const ChainSchema = z.object({ + chainId: z.number().positive(), + name: z.string(), + iconUrl: z.string().url().optional(), + publicRpcs: z.array(z.string().url()).default([]), + explorerUrl: z.string().url().optional(), + launchDate: z.number().positive(), + chainType: z.union([z.literal("Rollup"), z.literal("Validium")]), + baseToken: TokenSchema, +});