import {z} from 'zod' export type GetApiBridgeSwapTokenConfigsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const statusSchema = z.object({state: z.union([z.literal("PENDING_REVIEW"), z.literal("LISTED"), z.literal("NOT_LISTED"), z.literal("BLACKLISTED"), z.literal("DELISTED")]), reason: z.array(z.union([z.literal("MANUAL"), z.literal("CONFIG_SYNC"), z.literal("LISTED_FOR_OTHER_CHAIN"), z.literal("INVALID_DECIMALS"), z.literal("UNKNOWN_QUANTIZATION"), z.literal("NO_MARKET_CAP"), z.literal("NO_LIQUIDITY"), z.literal("CATEGORY_BLACKLIST"), z.literal("NO_COINGECKO_DATA")]))}); export const jsonNumber = z.number(); export const tokenConfigResponseSchema = z.object({chain: z.string(), tokenAddress: z.string(), name: z.string(), symbol: z.string(), decimals: z.number(), iconUrl: z.string(), aggregators: z.array(z.union([z.literal("KYBERSWAP"), z.literal("RHINO"), z.literal("ONEINCH"), z.literal("BEBOP")])), status: statusSchema, coingeckoId: z.string().optional(), marketCap: jsonNumber.optional(), inactive: z.boolean().optional()}); export const getApiBridgeSwapTokenConfigsResponse = z.record(z.string(), z.array(tokenConfigResponseSchema)); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get all token configs for bridge and swap * * GET /bridge-swap-token-configs */ export const getApiBridgeSwapTokenConfigs = async (options?: GetApiBridgeSwapTokenConfigsOptions) => { const {baseUrl = "https://api.rhino.fi/bridge", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/bridge-swap-token-configs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiBridgeSwapTokenConfigsResponse, await res.json()) };