import {z} from 'zod' export type OtcTradeExternalStatus = 'APPROVAL_PENDING' | 'COUNTERPARTY_PENDING' | 'COUNTERPARTY_PAIRED' | 'RISK_PENDING' | 'MATCHED' | 'CANCELLED' | 'REJECTED'; export type TradingAccountId = string; export type SharedMatchKey = string; export type ClientOtcTradeId = string; export type IdbShortCode = string; export type DateTime = string; export type GetApiV2OtcTradesArgs = {status?: OtcTradeExternalStatus | undefined, tradingAccountId: TradingAccountId, sharedMatchKey?: SharedMatchKey | undefined, clientOtcTradeId?: ClientOtcTradeId | undefined, brokeredBy?: IdbShortCode | undefined, 'createdAtDatetime[ gte ]'?: DateTime | undefined, 'createdAtDatetime[ lte ]'?: DateTime | undefined}; export type GetApiV2OtcTradesOptions = {baseUrl?: string | undefined, jwtTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const otcTradeId = z.string(); export const clientOtcTradeId = z.string(); export const tradingAccountId = z.string(); export const sharedMatchKey = z.string(); export const otcTradeExternalStatus = z.enum(["APPROVAL_PENDING", "COUNTERPARTY_PENDING", "COUNTERPARTY_PAIRED", "RISK_PENDING", "MATCHED", "CANCELLED", "REJECTED"]); export const dateTime = z.string(); export const timeStampAsString = z.string(); export const remarks = z.string(); export const idbShortCode = z.string(); export const tradeID = z.string(); export const otcMarketSymbol = z.string(); export const assetValue = z.string(); export const tradeSideAsString = z.string(); export const boolean = z.boolean(); export const otcTradeLegView = z.object({tradeId: tradeID.optional(), symbol: otcMarketSymbol, price: assetValue, quantity: assetValue, quoteAmount: assetValue.optional(), baseFee: assetValue.optional(), quoteFee: assetValue.optional(), tradeRebateAssetSymbol: otcMarketSymbol.optional(), tradeRebateAmount: assetValue.optional(), side: tradeSideAsString, isTaker: boolean, createdAtDatetime: dateTime.optional(), createdAtTimestamp: timeStampAsString.optional()}); export const otcTradeView = z.object({otcTradeId: otcTradeId, clientOtcTradeId: clientOtcTradeId.optional(), tradingAccountId: tradingAccountId.optional(), tradingAccountNickname: z.string().optional(), sharedMatchKey: sharedMatchKey, status: otcTradeExternalStatus, statusReason: z.string(), statusReasonCode: z.string(), createdAtDatetime: dateTime, createdAtTimestamp: timeStampAsString, expireDatetime: dateTime, expireTimestamp: timeStampAsString, remarks: remarks.optional(), brokeredBy: idbShortCode.optional(), trades: z.array(otcTradeLegView)}); export const getApiV2OtcTradesResponse = z.array(otcTradeView); 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 }; /** * Get OTC Trades * * GET /v2/otc-trades */ export const getApiV2OtcTrades = async (args: GetApiV2OtcTradesArgs, options?: GetApiV2OtcTradesOptions) => { const {baseUrl = "https://api.exchange.bullish.com/trading-api", fetch: fetchFn = fetch, jwtTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.tradingAccountId != null) query.append("tradingAccountId", String(args.tradingAccountId)) if (args.sharedMatchKey != null) query.append("sharedMatchKey", String(args.sharedMatchKey)) if (args.clientOtcTradeId != null) query.append("clientOtcTradeId", String(args.clientOtcTradeId)) if (args.brokeredBy != null) query.append("brokeredBy", String(args.brokeredBy)) if (args['createdAtDatetime[ gte ]'] != null) query.append("createdAtDatetime[ gte ]", String(args['createdAtDatetime[ gte ]'])) if (args['createdAtDatetime[ lte ]'] != null) query.append("createdAtDatetime[ lte ]", String(args['createdAtDatetime[ lte ]'])) if (jwtTokenAuth !== undefined) headers.set('authorization', `Bearer ${jwtTokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/otc-trades${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV2OtcTradesResponse, await res.json()) };