import {z} from 'zod' export type ExternalOrder = {input_mint: string, output_mint: string, input_amount: string, output_amount: string, use_exact_output_amount: boolean, min_fill_size: string}; export type FixedPoint = string; export type ApiTimestampedPriceFp = {price: FixedPoint, timestamp: number}; export type ApiExternalMatchResult = {input_mint: string, output_mint: string, input_amount: string, output_amount: string, price_fp: ApiTimestampedPriceFp}; export type FeeTake = {relayer_fee: string, protocol_fee: string}; export type ApiExternalAssetTransfer = {mint: string, amount: string}; export type ApiTimestampedPrice = {price: string, timestamp: number}; export type ApiExternalQuote = {order: ExternalOrder, match_result: ApiExternalMatchResult, fees: FeeTake, send: ApiExternalAssetTransfer, receive: ApiExternalAssetTransfer, price: ApiTimestampedPrice, timestamp: number}; export type ApiSignedQuote = {quote: ApiExternalQuote, signature: string, deadline: number}; export type AssemblyType = {QuotedOrder: {signed_quote: ApiSignedQuote, updated_order?: ExternalOrder | undefined}} | {NewOrder: {external_order: ExternalOrder}}; export type AssembleExternalMatchRequest = {do_gas_estimation?: boolean | undefined, receiver_address?: string | undefined, assembly: AssemblyType}; export type CreateApiV2ExternalMatchesAssembleMatchBundleArgs = {body: AssembleExternalMatchRequest}; export type CreateApiV2ExternalMatchesAssembleMatchBundleOptions = {baseUrl?: string | undefined, apiKeyHmac?: string | undefined, fetch?: typeof fetch | undefined}; export const fixedPoint = z.string(); export const apiBoundedMatchResult = z.object({input_mint: z.string(), output_mint: z.string(), price_fp: fixedPoint, output_quoted_price_fp: fixedPoint, min_input_amount: z.string(), max_input_amount: z.string()}); export const feeTakeRate = z.object({relayer_fee_rate: fixedPoint, protocol_fee_rate: fixedPoint}); export const apiExternalAssetTransfer = z.object({mint: z.string(), amount: z.string()}); export const transactionRequest = z.object({from: z.string().optional(), to: z.string().optional(), gas: z.string().optional(), gasPrice: z.string().optional(), value: z.string().optional(), data: z.string().optional(), nonce: z.string().optional(), chainId: z.string().optional()}); export const malleableAtomicMatchApiBundle = z.object({match_result: apiBoundedMatchResult, fee_rates: feeTakeRate, max_receive: apiExternalAssetTransfer, min_receive: apiExternalAssetTransfer, max_send: apiExternalAssetTransfer, min_send: apiExternalAssetTransfer, settlement_tx: transactionRequest, deadline: z.number().int()}); export const gasSponsorshipInfo = z.object({refund_amount: z.number().int(), refund_native_eth: z.boolean(), refund_address: z.string().optional()}); export const externalMatchResponse = z.object({match_bundle: malleableAtomicMatchApiBundle, gas_sponsorship_info: gasSponsorshipInfo.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/external-matches/assemble-match-bundle */ export const createApiV2ExternalMatchesAssembleMatchBundle = async (args: CreateApiV2ExternalMatchesAssembleMatchBundleArgs, options?: CreateApiV2ExternalMatchesAssembleMatchBundleOptions) => { const {baseUrl = "https://api.renegade.fi", fetch: fetchFn = fetch, apiKeyHmac} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyHmac !== undefined) headers.set("X-Renegade-Auth", apiKeyHmac) const url = `${baseUrl.replace(/\/+$/, '')}/v2/external-matches/assemble-match-bundle${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(externalMatchResponse, await res.json()) };