import {z} from 'zod' export type OrderSide = 'BUY' | 'SELL'; export type Market = 'ETHEUR' | 'ETHSGD' | 'ETHUSD' | 'BTCEUR' | 'BTCSGD' | 'BTCUSD' | 'PAXGUSD' | 'BCHUSD' | 'LTCUSD' | 'USDPUSD' | 'LINKUSD' | 'AAVEUSD' | 'UNIUSD' | 'PEPEUSD' | 'TRUMPUSD' | 'SHIBUSD' | 'ARBUSD' | 'BONKUSD' | 'ENAUSD' | 'MNTUSD' | 'ONDOUSD' | 'PENGUUSD' | 'QNTUSD' | 'RENDERUSD' | 'SKYUSD' | 'WIFUSD' | 'WLDUSD' | 'DOGEUSD' | 'AVAXUSD' | 'SUIUSD' | 'POLUSD' | 'XLMUSD' | 'BNBUSD'; export type OrderType = 'LIMIT' | 'MARKET' | 'POST_ONLY_LIMIT' | 'STOP_MARKET' | 'STOP_LIMIT'; export type TimeInForce = 'GTC' | 'FOK' | 'IOC' | 'GTT'; export type ExchangePublicCreateOrderBody = {ref_id?: string | undefined, side: OrderSide, market: Market, type: OrderType, base_amount?: string | undefined, price?: unknown | undefined, quote_amount?: string | undefined, metadata?: {[key: string]: string} | undefined, await_fill_millis?: number | undefined, time_in_force?: TimeInForce | undefined, expiration_date?: unknown | undefined, identity_id?: string | undefined, identity_account_id?: string | undefined, stop_price?: unknown | undefined, recipient_profile_id?: string | undefined, self_match_prevention_id?: string | undefined}; export type CreateApiProfilesProfileIdOrdersArgs = {profile_id: string, body: ExchangePublicCreateOrderBody}; export type CreateApiProfilesProfileIdOrdersOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const orderStatus = z.enum(["PENDING_SUBMISSION", "SUBMITTED", "OPEN", "FILLED", "CANCELLED", "REJECTED", "EXPIRED"]); export const orderSide = z.enum(["BUY", "SELL"]); export const market = z.enum(["ETHEUR", "ETHSGD", "ETHUSD", "BTCEUR", "BTCSGD", "BTCUSD", "PAXGUSD", "BCHUSD", "LTCUSD", "USDPUSD", "LINKUSD", "AAVEUSD", "UNIUSD", "PEPEUSD", "TRUMPUSD", "SHIBUSD", "ARBUSD", "BONKUSD", "ENAUSD", "MNTUSD", "ONDOUSD", "PENGUUSD", "QNTUSD", "RENDERUSD", "SKYUSD", "WIFUSD", "WLDUSD", "DOGEUSD", "AVAXUSD", "SUIUSD", "POLUSD", "XLMUSD", "BNBUSD"]); export const orderType = z.enum(["LIMIT", "MARKET", "POST_ONLY_LIMIT", "STOP_MARKET", "STOP_LIMIT"]); export const timeInForce = z.enum(["GTC", "FOK", "IOC", "GTT"]); export const order = z.object({id: z.string().optional(), profile_id: z.string().optional(), ref_id: z.string().optional(), status: orderStatus.optional(), side: orderSide.optional(), market: market.optional(), type: orderType.optional(), base_amount: z.string().optional(), price: z.string().optional(), quote_amount: z.string().optional(), metadata: z.record(z.string(), z.string()).optional(), created_at: z.string().optional(), modified_at: z.string().optional(), amount_filled: z.string().optional(), volume_weighted_average_price: z.string().optional(), time_in_force: timeInForce.optional(), expiration_date: z.string().optional(), identity_id: z.string().optional(), identity_account_id: z.string().optional(), stop_price: z.string().optional(), recipient_profile_id: z.string().optional(), is_triggered: z.boolean().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 }; /** * Create Order * * POST /profiles/{profile_id}/orders */ export const createApiProfilesProfileIdOrders = async (args: CreateApiProfilesProfileIdOrdersArgs, options?: CreateApiProfilesProfileIdOrdersOptions) => { const {baseUrl = "https://api.paxos.com/v2", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/profiles/${encodeURIComponent(String(args.profile_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(order, await res.json()) };