import {z} from 'zod' export type FixedPoint = string; export type OrderType = 'public_order' | 'natively_settled_private_order' | 'renegade_settled_public_fill_order' | 'renegade_settled_private_fill_order'; export type ApiOrderCore = {id: string, in_token: string, out_token: string, owner: string, min_price: FixedPoint, amount_in: string, min_fill_size: string, order_type: OrderType, allow_external_matches: boolean}; export type SignatureWithNonce = {nonce: string, signature: unknown}; export type ApiBabyJubJubPoint = {x: string, y: string}; export type ApiSchnorrSignature = {s: string, r: ApiBabyJubJubPoint}; export type OrderAuth = {public_order: {intent_signature: SignatureWithNonce}} | {natively_settled_private_order: {intent_signature: ApiSchnorrSignature}} | {renegade_settled_order: {intent_signature: ApiSchnorrSignature, new_output_balance_signature: ApiSchnorrSignature}}; export type CreateOrderRequest = {order: ApiOrderCore, auth: OrderAuth, precompute_cancellation_proof: boolean}; export type CreateApiV2AccountAccountIdOrdersArgs = {account_id: string, non_blocking?: boolean | undefined, body: CreateOrderRequest}; export type CreateApiV2AccountAccountIdOrdersOptions = {baseUrl?: string | undefined, accountHmac?: string | undefined, fetch?: typeof fetch | undefined}; export const fixedPoint = z.string(); export const orderType = z.enum(["public_order", "natively_settled_private_order", "renegade_settled_public_fill_order", "renegade_settled_private_fill_order"]); export const apiOrderCore = z.object({id: z.string(), in_token: z.string(), out_token: z.string(), owner: z.string(), min_price: fixedPoint, amount_in: z.string(), min_fill_size: z.string(), order_type: orderType, allow_external_matches: z.boolean()}); export const apiPoseidonCSPRNG = z.object({seed: z.string(), index: z.number().int()}); export const apiOrderShare = z.object({in_token: z.string(), out_token: z.string(), owner: z.string(), min_price: z.string(), amount_in: z.string()}); export const orderState = z.enum(["created", "matching", "settling_match", "filled", "cancelled"]); export const apiTimestampedPriceFloat = z.object({price: z.string(), timestamp: z.number().int()}); export const feeTake = z.object({relayer_fee: z.string(), protocol_fee: z.string()}); export const apiPartialOrderFill = z.object({amount: z.string(), price: apiTimestampedPriceFloat, fees: feeTake, tx_hash: z.string()}); export const apiOrder = z.object({order: apiOrderCore, recovery_stream: apiPoseidonCSPRNG, share_stream: apiPoseidonCSPRNG, public_shares: apiOrderShare, state: orderState, fills: z.array(apiPartialOrderFill), created: z.number().int()}); export const createOrderResponse = z.object({task_id: z.string(), order: apiOrder, completed: z.boolean()}); 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/account/{account_id}/orders */ export const createApiV2AccountAccountIdOrders = async (args: CreateApiV2AccountAccountIdOrdersArgs, options?: CreateApiV2AccountAccountIdOrdersOptions) => { const {baseUrl = "https://api.renegade.fi", fetch: fetchFn = fetch, accountHmac} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.non_blocking != null) query.append("non_blocking", String(args.non_blocking)) if (accountHmac !== undefined) headers.set("X-Renegade-Auth", accountHmac) const url = `${baseUrl.replace(/\/+$/, '')}/v2/account/${encodeURIComponent(String(args.account_id))}/orders${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(createOrderResponse, await res.json()) };