import {z} from 'zod' export type EIP155OrderRequestPermitTransaction = {order_request_id: string, permit_signature: unknown}; export type CreateApiApiV2AccountsAccountIdOrderRequestsEip155Args = {body: EIP155OrderRequestPermitTransaction}; export type CreateApiApiV2AccountsAccountIdOrderRequestsEip155Options = {baseUrl?: string | undefined, ApiKeyId?: string | undefined, ApiSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const orderRequestStatus = z.enum(["QUOTED", "PENDING", "PENDING_BRIDGE", "SUBMITTED", "ERROR", "CANCELLED", "EXPIRED", "REJECTED"]); export const orderSide = z.enum(["BUY", "SELL"]); export const orderType = z.enum(["MARKET", "LIMIT"]); export const orderTif = z.enum(["DAY", "GTC", "IOC", "FOK"]); export const eIP155OrderRequest = z.object({id: z.string(), account_id: z.string(), status: orderRequestStatus, created_dt: z.string(), order_side: orderSide, order_type: orderType, order_tif: orderTif, recipient_account_id: z.string().nullable().optional(), order_id: z.string().nullable().optional(), cancel_message: z.string().nullable().optional(), reject_message: z.string().nullable().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 }; /** * Submit EIP155 Order Request * * POST /api/v2/accounts/{account_id}/order_requests/eip155 */ export const createApiApiV2AccountsAccountIdOrderRequestsEip155 = async (args: CreateApiApiV2AccountsAccountIdOrderRequestsEip155Args, options?: CreateApiApiV2AccountsAccountIdOrderRequestsEip155Options) => { const {baseUrl = "https://api-enterprise.sbt.dinari.com", fetch: fetchFn = fetch, ApiKeyId, ApiSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyId !== undefined) headers.set("X-API-Key-Id", ApiKeyId) if (ApiSecretKey !== undefined) headers.set("X-API-Secret-Key", ApiSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/accounts/${encodeURIComponent(String(args.account_id))}/order_requests/eip155${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(eIP155OrderRequest, await res.json()) };