import {z} from 'zod' export type GetApiRouterOptimalRouteArgs = {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, recipient?: string | undefined, amountSymbol: string}; export type GetApiRouterOptimalRouteOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiRouterOptimalRouteResponse = z.object({sourceChain: z.string().optional(), destinationChain: z.string().optional(), tokenIn: z.string().optional(), tokenOut: z.string().optional(), originalAmount: z.string().optional(), totalFees: z.string().optional(), bridgeToUse: z.enum(["ACROSS", "CCTP", "GATEWAY", "RHINOFI"]).nullable().optional(), bridgeAddress: z.string().optional(), bridgeExtraData: z.string().optional(), supportedBridges: z.array(z.enum(["ACROSS", "CCTP", "GATEWAY", "RHINOFI"])).optional(), bridgeFeeDetails: z.object({}).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 }; /** * Find optimal cross-chain transfer route * * GET /router/optimal-route */ export const getApiRouterOptimalRoute = async (args: GetApiRouterOptimalRouteArgs, options?: GetApiRouterOptimalRouteOptions) => { 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.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(/\/+$/, '')}/router/optimal-route${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(getApiRouterOptimalRouteResponse, await res.json()) };