import {z} from 'zod' export type GetApiConfigsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bridgeTokenEntry = z.object({token: z.string(), address: z.string(), decimals: z.number(), maxDepositLimit: z.number().optional(), maxWithdrawLimit: z.number().optional(), minAmountUsdInContract: z.number().optional(), allowedRouteChains: z.array(z.string()).optional()}); export const bridgeBadge = z.object({type: z.enum(["new", "hot", "campaign", "achievement", "free", "airdrop", "optimistic", "launch"]), translationKey: z.enum(["airdrop", "free_gas", "memes", "hot", "new"])}); export const bridgeConfigEntrySchema = z.object({name: z.string(), type: z.enum(["EVM", "SOL", "STK", "TON", "TRON", "CEX", "KARNOT", "STELLAR"]), networkId: z.string(), contractAddress: z.string(), multicallContractAddress: z.string().optional(), confirmationBlocks: z.number(), avgBlockTime: z.number(), nativeTokenName: z.string(), nativeTokenDecimals: z.number(), nativeTokenSafeguard: z.number(), gasMultiplier: z.number().optional(), blockExplorer: z.string(), rpc: z.string(), site: z.string(), status: z.enum(["enabled", "disabled"]), statusReason: z.string().optional(), tokens: z.record(z.string(), bridgeTokenEntry), badge: bridgeBadge.optional(), category: z.enum(["experimental", "popular"]).optional(), paradexSignerChainId: z.string().optional(), gasBoostEnabled: z.boolean(), enabledDepositAddress: z.boolean(), sameChainSwapsAddress: z.string().optional(), executionChain: z.string().optional()}); export const getApiConfigsResponse = z.record(z.string(), bridgeConfigEntrySchema); 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 }; /** * Configs (chains & tokens) * * GET /configs */ export const getApiConfigs = async (options?: GetApiConfigsOptions) => { 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(/\/+$/, '')}/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(getApiConfigsResponse, await res.json()) };