import {z} from 'zod' export type GetApiQuotesSingleArgs = {tokenIn: string, tokenOut: string, sourceChain: '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', 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, bridge: 'ACROSS' | 'CCTP' | 'GATEWAY' | 'RHINOFI', recipient?: string | undefined, amountSymbol?: ('USDC' | 'USDT' | 'DAI' | 'BUSD' | 'EURC' | 'ETH' | 'WETH' | 'STRK' | 'BNB' | 'LORDS' | 'USDS' | 'USDG' | 'USDT0' | 'WBTC' | 'LINK' | 'SOL' | 'EKUBO' | 'AAVE' | 'UNI' | 'CURVE' | 'cbBTC' | 'VIRTUAL' | 'stETH' | 'wstETH' | 'SKY' | 'AUSD' | 'WMON' | 'LVMON' | 'OP' | 'WLD' | 'ARB' | 'sAVAX' | 'WAVAX' | 'anyBTC' | 'WBNB' | 'TRON' | 'AVAX' | 'POL' | 'MON') | undefined}; export type GetApiQuotesSingleOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiQuotesSingleResponse = z.object({bridgeFeeDetails: z.object({}).optional(), totalFee: z.string().optional(), totalFeeFormatted: z.string().optional(), route: z.object({tokenIn: z.string().optional(), tokenOut: z.string().optional(), sourceChain: z.string().optional(), destinationChain: z.string().optional(), amount: z.string().optional(), bridge: z.enum(["ACROSS", "CCTP", "GATEWAY", "RHINOFI"]).optional(), recipient: z.string().nullable().optional()}).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 quote from specific bridge * * GET /quotes/single */ export const getApiQuotesSingle = async (args: GetApiQuotesSingleArgs, options?: GetApiQuotesSingleOptions) => { 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.tokenIn != null) query.append("tokenIn", String(args.tokenIn)) if (args.tokenOut != null) query.append("tokenOut", String(args.tokenOut)) if (args.sourceChain != null) query.append("sourceChain", String(args.sourceChain)) if (args.destinationChain != null) query.append("destinationChain", String(args.destinationChain)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.bridge != null) query.append("bridge", String(args.bridge)) if (args.recipient != null) query.append("recipient", String(args.recipient)) if (args.amountSymbol != null) query.append("amountSymbol", String(args.amountSymbol)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/quotes/single${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(getApiQuotesSingleResponse, await res.json()) };