import {z} from 'zod' export type ChainGasBalances = Record; export type Route = {routeId: string, fromAmount: string, chainGasBalances: ChainGasBalances, toAmount: string, usedBridgeNames: Array<'polygon-bridge' | 'hop' | 'anyswap-router-v4' | 'hyphen' | 'arbitrum-bridge' | 'connext'>, totalUserTx: number, totalGasFeesInUsd: number, sender: string, userTxs: Array>, serviceTime: number}; export type StartActiveRouteInputDTO = {fromChainId: 'ETHEREUM' | 'OPTIMISM' | 'BSC' | 'XDAI' | 'POLYGON' | 'FANTOM' | 'ARBITRUM' | 'AVALANCHE', toChainId: 'ETHEREUM' | 'OPTIMISM' | 'BSC' | 'XDAI' | 'POLYGON' | 'FANTOM' | 'ARBITRUM' | 'AVALANCHE', fromAssetAddress: string, toAssetAddress: string, includeFirstTxDetails?: boolean | undefined, route: Route}; export type CreateApiV2RouteStartArgs = {'API-KEY'?: string | undefined, body: StartActiveRouteInputDTO}; export type CreateApiV2RouteStartOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const approvalData = z.object({minimumApprovalAmount: z.string(), approvalTokenAddress: z.string(), allowanceTarget: z.string(), owner: z.string()}); export const startActiveRouteResponseDTO = z.object({userTxType: z.enum(["approve", "fund-movr", "claim", "dex-swap", "sign"]).optional(), txTarget: z.string().optional(), chainId: z.enum(["ETHEREUM", "OPTIMISM", "BSC", "XDAI", "POLYGON", "FANTOM", "ARBITRUM", "AVALANCHE"]).optional(), activeRouteId: z.number(), txData: z.string().optional(), txType: z.enum(["eth_sendTransaction", "eth_signMessage"]).optional(), value: z.string().optional(), userTxIndex: z.number().optional(), totalUserTx: z.number(), approvalData: approvalData.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 }; /** * POST /v2/route/start */ export const createApiV2RouteStart = async (args: CreateApiV2RouteStartArgs, options: CreateApiV2RouteStartOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/route/start${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(startActiveRouteResponseDTO, await res.json()) };