import {z} from 'zod' export type QuoteRequestDto = {srcChainId: number, dstChainId: number, srcTokenAddress: string, dstTokenAddress: string, srcWalletAddress: string, dstWalletAddress: string, amount: string}; export type CreateApiValueTransferQuotesArgs = {body: QuoteRequestDto}; export type CreateApiValueTransferQuotesOptions = {baseUrl: string, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const routeStepDto = z.object({chainId: z.object({}).nullable(), description: z.string()}); export const routeDto = z.object({label: z.string(), srcChainId: z.number(), dstChainId: z.number(), steps: z.array(routeStepDto)}); export const amountsDto = z.object({srcRaw: z.string(), srcUsd: z.string(), dstRaw: z.string(), dstUsd: z.string(), dstMinRaw: z.string(), dstDecimals: z.number(), dstSymbol: z.string()}); export const feeItemDto = z.object({labelKey: z.enum(["MESSAGE", "GENERAL", "DST_NATIVE_DROP", "CCTP_RECEIVE"]), description: z.string(), chainId: z.object({}).nullable(), amount: z.string(), symbol: z.string(), decimals: z.number(), usd: z.object({}).nullable()}); export const feesDto = z.object({totalUsd: z.string(), percent: z.string(), items: z.array(feeItemDto)}); export const txEncodedDto = z.object({to: z.string(), data: z.string(), value: z.string().optional(), chainId: z.number(), from: z.string().optional(), gasLimit: z.string().optional()}); export const userStepDto = z.object({type: z.enum(["TRANSACTION", "SIGNATURE"]), description: z.string(), chainId: z.number(), transaction: txEncodedDto.optional(), signature: z.object({}).optional()}); export const transferQuoteDto = z.object({quoteId: z.string(), action: z.enum(["transfer", "bridge", "convert"]), trackable: z.boolean(), expiresAt: z.object({}).nullable(), route: routeDto, amounts: amountsDto, fees: feesDto, durationMs: z.object({}).nullable(), userSteps: z.array(userStepDto)}); export const quotesResponseDto = z.object({quote: transferQuoteDto.nullable(), alternatives: z.array(transferQuoteDto)}); 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 the best transfer route for a move * * POST /value-transfer/quotes */ export const createApiValueTransferQuotes = async (args: CreateApiValueTransferQuotesArgs, options: CreateApiValueTransferQuotesOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/value-transfer/quotes${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(quotesResponseDto, await res.json()) };