import {z} from 'zod' export type GetApiV2TokenListsToTokenListArgs = {fromChainId: string, toChainId: string, disableSwapping?: boolean | undefined, includeDexes?: 'oneinch' | undefined, excludeDexes?: 'oneinch' | undefined, includeBridges?: (Array<'polygon-bridge' | 'hop' | 'anyswap-router-v4' | 'hyphen' | 'arbitrum-bridge' | 'connext'>) | undefined, excludeBridges?: (Array<'polygon-bridge' | 'hop' | 'anyswap-router-v4' | 'hyphen' | 'arbitrum-bridge' | 'connext'>) | undefined, singleTxOnly?: boolean | undefined, isShortList?: boolean | undefined, 'API-KEY'?: string | undefined}; export type GetApiV2TokenListsToTokenListOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const tokenAsset = z.object({name: z.string().optional(), address: z.string(), icon: z.string().optional(), decimals: z.number().optional(), symbol: z.string(), chainId: z.enum(["ETHEREUM", "OPTIMISM", "BSC", "XDAI", "POLYGON", "FANTOM", "ARBITRUM", "AVALANCHE"])}); export const tokenListOutputDTO = z.object({success: z.boolean(), result: z.array(tokenAsset)}); 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 /v2/token-lists/to-token-list */ export const getApiV2TokenListsToTokenList = async (args: GetApiV2TokenListsToTokenListArgs, options: GetApiV2TokenListsToTokenListOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fromChainId != null) query.append("fromChainId", String(args.fromChainId)) if (args.toChainId != null) query.append("toChainId", String(args.toChainId)) if (args.disableSwapping != null) query.append("disableSwapping", String(args.disableSwapping)) if (args.includeDexes != null) query.append("includeDexes", String(args.includeDexes)) if (args.excludeDexes != null) query.append("excludeDexes", String(args.excludeDexes)) for (const item of args.includeBridges ?? []) query.append("includeBridges", String(item)) for (const item of args.excludeBridges ?? []) query.append("excludeBridges", String(item)) if (args.singleTxOnly != null) query.append("singleTxOnly", String(args.singleTxOnly)) if (args.isShortList != null) query.append("isShortList", String(args.isShortList)) if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/token-lists/to-token-list${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(tokenListOutputDTO, await res.json()) };