import {z} from 'zod' export type ContractCall = {fromAmount: string, fromTokenAddress: string, toContractAddress: string, toContractCallData: string, toContractGasLimit: string, toApprovalAddress?: string | undefined, toTokenAddress?: string | undefined}; export type ContractCallsRequest = {fromChain: number, fromToken: string, fromAddress: string, toChain: number, toToken: string, toAmount: string, contractCalls: Array, toFallbackAddress?: string | undefined, contractOutputsToken?: string | undefined, slippage?: number | undefined, integrator?: string | undefined, referrer?: string | undefined, allowBridges?: Array | undefined, denyBridges?: Array | undefined, preferBridges?: Array | undefined, allowExchanges?: Array | undefined, denyExchanges?: Array | undefined, preferExchanges?: Array | undefined, allowDestinationCall?: boolean | undefined, fee?: number | undefined}; export type CreateApiV1QuoteContractCallsArgs = {'x-lifi-api-key'?: string | undefined, body: ContractCallsRequest}; export type CreateApiV1QuoteContractCallsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const exchangesEnum = z.string(); export const exchange = z.object({key: exchangesEnum.optional(), name: z.string().optional(), logoURI: z.string().optional(), supportedChains: z.string().optional()}); export const bridgesEnum = z.string(); export const supportedChains = z.object({fromChainId: z.string().optional(), toChainId: z.string().optional()}); export const bridge = z.object({key: bridgesEnum.optional(), name: z.string().optional(), logoURI: z.string().optional(), supportedChains: z.array(supportedChains).optional()}); export const tools = z.object({exchanges: z.array(exchange).optional(), bridges: z.array(bridge).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 }; /** * Perform multiple contract calls across blockchains (BETA) * * POST /v1/quote/contractCalls */ export const createApiV1QuoteContractCalls = async (args: CreateApiV1QuoteContractCallsArgs, options?: CreateApiV1QuoteContractCallsOptions) => { const {baseUrl = "https://li.quest", 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['x-lifi-api-key'] != null) headers.set("x-lifi-api-key", String(args['x-lifi-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/quote/contractCalls${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(tools, await res.json()) };