import {z} from 'zod' export type GetApiAccountsAccountIdTradesArgs = {account_id: string, cursor?: string | undefined, limit?: number | undefined, instrument?: string | undefined, order?: string | undefined, securities_account?: string | undefined, 'LMG-Data-Privacy-Access-Principal': string, 'LMG-Data-Privacy-Access-Justification': string}; export type GetApiAccountsAccountIdTradesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const side = z.enum(["buy", "sell"]); export const direction = z.enum(["debit", "credit"]); export const taxResponseField = z.object({type: z.string(), amount: z.string(), direction: direction}); export const tradeResponse = z.object({id: z.string(), side: side, instrument: z.string(), quantity: z.string(), price: z.string(), amount: z.string(), taxes: z.array(taxResponseField).optional(), fee: z.string(), currency: z.string().min(3).max(3), order: z.string(), execution_venue: z.string(), securities_account: z.string(), executed_at: z.string()}); export const pagination = z.object({next_cursor: z.string().optional()}); export const pageTradeResponse = z.object({data: z.array(tradeResponse), pagination: pagination}); 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 }; /** * List Trades * * GET /accounts/{account_id}/trades */ export const getApiAccountsAccountIdTrades = async (args: GetApiAccountsAccountIdTradesArgs, options?: GetApiAccountsAccountIdTradesOptions) => { const {baseUrl = "https://sandbox.api.lemon.markets/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.instrument != null) query.append("instrument", String(args.instrument)) if (args.order != null) query.append("order", String(args.order)) if (args.securities_account != null) query.append("securities_account", String(args.securities_account)) if (args['LMG-Data-Privacy-Access-Principal'] != null) headers.set("LMG-Data-Privacy-Access-Principal", String(args['LMG-Data-Privacy-Access-Principal'])) if (args['LMG-Data-Privacy-Access-Justification'] != null) headers.set("LMG-Data-Privacy-Access-Justification", String(args['LMG-Data-Privacy-Access-Justification'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/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(pageTradeResponse, await res.json()) };