import {z} from 'zod' export type GetApiTradingSettlementsSettlementIdArgs = {settlementId: string, excludeTrades?: boolean | undefined, excludePositions?: boolean | undefined, excludeSettlementInstructions?: boolean | undefined}; export type GetApiTradingSettlementsSettlementIdOptions = {baseUrl?: string | undefined, ApiAccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const settlementInstructionsCrypto = z.object({address: z.string(), assetTypeID: z.string()}); export const amount = z.object({assetType: z.string(), currentPrice: z.string().optional(), currentUSDValue: z.string().optional(), quantity: z.string()}); export const tradingPosition = z.object({positionAmount: amount}); export const settlementStatus = z.enum(["PENDING", "SETTLING", "SETTLED", "CANCELED", "EXECUTED"]); export const settlementType = z.enum(["TRADE", "ADVISORY_FEE", "CUSTODY_FEE", "MANAGEMENT_FEE", "MODEL_FEE"]); export const settlementDetails = z.object({account: z.object({accountId: z.string().optional()}).optional(), createdAt: z.string(), instructionsCrypto: z.array(settlementInstructionsCrypto).nullable().optional(), positions: z.array(tradingPosition).nullable().optional(), settlementID: z.string(), settlementStatus: settlementStatus, settlementType: settlementType.optional(), vault: z.object({vaultID: z.string().optional()}).optional()}); export const settlementResponse = z.object({data: settlementDetails}); 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 settlement by ID * * GET /trading/settlements/{settlementId} */ export const getApiTradingSettlementsSettlementId = async (args: GetApiTradingSettlementsSettlementIdArgs, options?: GetApiTradingSettlementsSettlementIdOptions) => { const {baseUrl = "https://api.anchorage-staging.com/v2", fetch: fetchFn = fetch, ApiAccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.excludeTrades != null) query.append("excludeTrades", String(args.excludeTrades)) if (args.excludePositions != null) query.append("excludePositions", String(args.excludePositions)) if (args.excludeSettlementInstructions != null) query.append("excludeSettlementInstructions", String(args.excludeSettlementInstructions)) if (ApiAccessKey !== undefined) headers.set("Api-Access-Key", ApiAccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/trading/settlements/${encodeURIComponent(String(args.settlementId))}${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(settlementResponse, await res.json()) };