Skip to content

Commit

Permalink
feat: improve zod inference and move schemas to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Aug 23, 2024
1 parent 95d90ee commit a71942a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
27 changes: 1 addition & 26 deletions packages/metadata/src/providers/githubMetadata.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import axios, { AxiosInstance } from "axios";
import { z } from "zod";

import {
Address,
ILogger,
Token,
TokenType,
Expand All @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions packages/metadata/src/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -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<Address>((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,
});

0 comments on commit a71942a

Please sign in to comment.