import {z} from 'zod' export type GetApiQuotesMultiSourceArgs = {destinationChain: 'ARBITRUM_MAINNET' | 'ARBITRUM_TESTNET' | 'AVALANCHE_MAINNET' | 'AVALANCHE_TESTNET' | 'BASE_MAINNET' | 'BASE_TESTNET' | 'STARKNET_MAINNET' | 'STARKNET_TESTNET' | 'BSC_MAINNET' | 'ETHEREUM_MAINNET' | 'ETHEREUM_TESTNET' | 'OPTIMISM_MAINNET' | 'OPTIMISM_TESTNET' | 'MONAD_MAINNET' | 'MONAD_TESTNET' | 'POLYGON_MAINNET' | 'HYPEREVM_MAINNET' | 'LISK_MAINNET' | 'SOLANA_MAINNET' | 'SOLANA_TESTNET' | 'TRON_MAINNET', amount: string, tokenOut: string, recipient?: string | undefined, excludeChains?: string | undefined}; export type GetApiQuotesMultiSourceOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiQuotesMultiSourceResponse = z.object({destinationChain: z.string().optional(), quotes: z.array(z.object({sourceChain: z.string().optional(), destinationChain: z.string().optional(), totalFee: z.string().optional(), totalFeeFormatted: z.string().optional(), bridge: z.enum(["ACROSS", "CCTP", "GATEWAY", "RHINOFI"]).nullable().optional(), paymentOptions: z.array(z.object({token: z.enum(["USDC", "USDT", "DAI", "BUSD", "EURC", "ETH", "WETH", "STRK", "BNB", "AVAX"]).optional(), tokenAddress: z.string().optional(), depositAmount: z.string().optional(), depositAmountFormatted: z.string().optional(), slippage: z.number().optional()})).optional()})).optional(), cheapestOption: z.object({sourceChain: z.string().optional(), destinationChain: z.string().optional(), totalFee: z.string().optional(), totalFeeFormatted: z.string().optional(), bridge: z.enum(["ACROSS", "CCTP", "GATEWAY", "RHINOFI"]).nullable().optional(), paymentOptions: z.array(z.object({token: z.string().optional(), tokenAddress: z.string().optional(), depositAmount: z.string().optional(), depositAmountFormatted: z.string().optional(), slippage: z.number().optional()})).optional()}).nullable().optional()}); 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 quotes from all source chains to destination * * GET /quotes/multi-source */ export const getApiQuotesMultiSource = async (args: GetApiQuotesMultiSourceArgs, options?: GetApiQuotesMultiSourceOptions) => { const {baseUrl = "https://api.chainrails.io/api/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.destinationChain != null) query.append("destinationChain", String(args.destinationChain)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.tokenOut != null) query.append("tokenOut", String(args.tokenOut)) if (args.recipient != null) query.append("recipient", String(args.recipient)) if (args.excludeChains != null) query.append("excludeChains", String(args.excludeChains)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/quotes/multi-source${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(getApiQuotesMultiSourceResponse, await res.json()) };