import {z} from 'zod' export type GetApiQuoteV7ChainIdArgs = {chainId: number, referrer?: string | undefined, tokenIn: string, tokenOut: string, amount: number, fee?: number | undefined, feeBy?: ('input' | 'output') | undefined, maxPriceImpact?: number | undefined, maxSlippage?: number | undefined, onlyDEX?: string | undefined, onlyPools?: string | undefined, excludeDEX?: string | undefined, excludePools?: string | undefined, excludeTokens?: string | undefined, visualize?: boolean | undefined}; export type GetApiQuoteV7ChainIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiQuoteV7ChainIdResponse = z.object({}); 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 }; /** * generates a quote * * GET /quote/v7/{chainId} */ export const getApiQuoteV7ChainId = async (args: GetApiQuoteV7ChainIdArgs, options?: GetApiQuoteV7ChainIdOptions) => { const {baseUrl = "https://api.sushi.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.referrer != null) query.append("referrer", String(args.referrer)) if (args.tokenIn != null) query.append("tokenIn", String(args.tokenIn)) if (args.tokenOut != null) query.append("tokenOut", String(args.tokenOut)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.fee != null) query.append("fee", String(args.fee)) if (args.feeBy != null) query.append("feeBy", String(args.feeBy)) if (args.maxPriceImpact != null) query.append("maxPriceImpact", String(args.maxPriceImpact)) if (args.maxSlippage != null) query.append("maxSlippage", String(args.maxSlippage)) if (args.onlyDEX != null) query.append("onlyDEX", String(args.onlyDEX)) if (args.onlyPools != null) query.append("onlyPools", String(args.onlyPools)) if (args.excludeDEX != null) query.append("excludeDEX", String(args.excludeDEX)) if (args.excludePools != null) query.append("excludePools", String(args.excludePools)) if (args.excludeTokens != null) query.append("excludeTokens", String(args.excludeTokens)) if (args.visualize != null) query.append("visualize", String(args.visualize)) const url = `${baseUrl.replace(/\/+$/, '')}/quote/v7/${encodeURIComponent(String(args.chainId))}${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(getApiQuoteV7ChainIdResponse, await res.json()) };