import {z} from 'zod' export type QuoteRequestV3DTO = {sourceChainID: number, destinationChainID: number, sourceToken: string, destinationToken: string, sourceAmount: string, funder: string, refundRecipient?: string | undefined, recipient: string}; export type QuotesV3RequestContractsDTO = {sourcePortal?: string | undefined, destinationPortal?: string | undefined, prover?: string | undefined}; export type QuotesRequestV3DTO = {dAppID: string, intentExecutionTypes: Array<'SELF_PUBLISH' | 'GASLESS'>, quoteRequest: QuoteRequestV3DTO, contracts?: QuotesV3RequestContractsDTO | undefined}; export type CreateApiApiV3QuotesExactOutArgs = {body: QuotesRequestV3DTO}; export type CreateApiApiV3QuotesExactOutOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const tokenInfoV3DTO = z.object({address: z.string(), decimals: z.number(), symbol: z.string()}); export const feeV3DTO = z.object({name: z.string(), description: z.string(), token: tokenInfoV3DTO, amount: z.string()}); export const quoteResponseDataV3DTO = z.object({intentExecutionType: z.enum(["SELF_PUBLISH", "GASLESS"]), sourceChainID: z.number(), destinationChainID: z.number(), sourceToken: z.string(), destinationToken: z.string(), sourceAmount: z.string(), destinationAmount: z.string(), funder: z.string(), refundRecipient: z.string(), recipient: z.string(), fees: z.array(feeV3DTO), deadline: z.number(), estimatedFulfillTimeSec: z.number(), encodedRoute: z.string()}); export const quoteV3ResponseContractsDTO = z.object({sourcePortal: z.string(), destinationPortal: z.string(), prover: z.string()}); export const solverQuoteDataV2DTO = z.object({quoteResponse: quoteResponseDataV3DTO.optional(), contracts: quoteV3ResponseContractsDTO}); export const solverQuoteV2ResponseDTO = z.object({quoteID: z.string(), solverID: z.string(), receiveSignedIntentUrl: z.string(), quoteData: solverQuoteDataV2DTO}); export const quotesResponseV3DTO = z.object({data: z.array(solverQuoteV2ResponseDTO)}); 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 Exact Out Quotes * * POST /api/v3/quotes/exactOut */ export const createApiApiV3QuotesExactOut = async (args: CreateApiApiV3QuotesExactOutArgs, options?: CreateApiApiV3QuotesExactOutOptions) => { const {baseUrl = "https://quotes.eco.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/quotes/exactOut${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(quotesResponseV3DTO, await res.json()) };