import {z} from 'zod' export type CMPackageActionMarginCall = 'NONE' | 'PARTIAL' | 'FULL' | 'SWEEP'; export type CMAcceleratedMarginCallUpdate = {action?: CMPackageActionMarginCall | undefined, curePeriod?: string | undefined, ltv?: string | undefined}; export type CMDeleveragingLevel = {liquidationPercentage: string, price: string}; export type CMAutoDeleveragingUpdate = {levels?: Array | undefined}; export type CMAssetTypeType = 'ANCHORAGECUSTODY' | 'EXTERNAL'; export type CMAssetType = {assetType: string, type: CMAssetTypeType}; export type CMPriceSource = 'COINBASEPRO' | 'KRAKEN' | 'KAMINO' | 'BINANCE'; export type CMAssetTrackingConfigUpdate = {asset: CMAssetType, liquidationPriority?: number | undefined, maxStalenessMinutes?: number | undefined, notes?: string | undefined, priceSource?: CMPriceSource | undefined, weight?: string | undefined}; export type CMPackageActionCritical = 'NONE' | 'PARTIAL' | 'FULL' | 'SWEEP'; export type CMCriticalUpdate = {action?: CMPackageActionCritical | undefined, defaultNotice?: boolean | undefined, ltv?: string | undefined, returnToLtv?: string | undefined, warningLtv?: string | undefined}; export type CMTopUpType = 'LTV' | 'QUANTITY'; export type CMMarginCallUpdate = {action?: CMPackageActionMarginCall | undefined, curePeriod?: string | undefined, curePeriodProtection?: boolean | undefined, ltv?: string | undefined, returnToLtv?: string | undefined, topUpType?: CMTopUpType | undefined, warningLtv?: string | undefined}; export type CMPackageActionMarginReturn = 'NONE' | 'SWEEP'; export type CMMarginReturnUpdate = {action?: CMPackageActionMarginReturn | undefined, ltv?: string | undefined, returnToLtv?: string | undefined, returnType?: string | undefined}; export type CMPriceStrategy = 'DEFAULT' | 'BID' | 'ASK' | 'MID' | 'LAST'; export type CMUpdatePackageRequest = {acceleratedMarginCall?: CMAcceleratedMarginCallUpdate | undefined, autoDeleveraging?: CMAutoDeleveragingUpdate | undefined, clientReferenceId?: string | undefined, collateralAssetsConfig?: Array | undefined, critical?: CMCriticalUpdate | undefined, isActive?: boolean | undefined, ltvRoundingPrecision?: number | undefined, marginCall?: CMMarginCallUpdate | undefined, marginReturn?: CMMarginReturnUpdate | undefined, priceStrategy?: CMPriceStrategy | undefined}; export type PatchApiCollateralManagementPackagesPackageIdArgs = {packageId: string, 'Api-Signature': string, 'Api-Timestamp': number, body: CMUpdatePackageRequest}; export type PatchApiCollateralManagementPackagesPackageIdOptions = {baseUrl?: string | undefined, ApiAccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const cMPackageActionMarginCall = z.enum(["NONE", "PARTIAL", "FULL", "SWEEP"]); export const cMAcceleratedMarginCall = z.object({action: cMPackageActionMarginCall, curePeriod: z.string(), ltv: z.string()}); export const cMAssetTypeType = z.enum(["ANCHORAGECUSTODY", "EXTERNAL"]); export const cMAssetType = z.object({assetType: z.string(), type: cMAssetTypeType}); export const cMCollateralAsset = z.object({asset: cMAssetType, price: z.string(), quantity: z.string(), weight: z.string(), weightedValue: z.string()}); export const cMPackageActionCritical = z.enum(["NONE", "PARTIAL", "FULL", "SWEEP"]); export const cMCritical = z.object({action: cMPackageActionCritical, defaultNotice: z.boolean(), ltv: z.string(), returnToLtv: z.string(), warningLtv: z.string()}); export const cMTopUpType = z.enum(["LTV", "QUANTITY"]); export const cMMarginCall = z.object({action: cMPackageActionMarginCall, curePeriod: z.string(), curePeriodProtection: z.boolean(), ltv: z.string(), returnToLtv: z.string(), topUpType: cMTopUpType, warningLtv: z.string()}); export const cMPackageActionMarginReturn = z.enum(["NONE", "SWEEP"]); export const cMMarginReturn = z.object({action: cMPackageActionMarginReturn, ltv: z.string(), returnToLtv: z.string()}); export const cMPackageState = z.enum(["MARGIN_RETURN", "HEALTHY", "MARGIN_CALL", "CRITICAL", "CLOSED", "CURE_PERIOD_EXPIRED", "DEFAULTED"]); export const cMPackage = z.object({acceleratedMarginCall: cMAcceleratedMarginCall.optional(), active: z.boolean(), clientReferenceId: z.string(), collateralAssets: z.array(cMCollateralAsset), critical: cMCritical, currentLtv: z.string().optional(), exposureValue: z.string(), ltvTimestamp: z.string().optional(), marginCall: cMMarginCall.optional(), marginReturn: cMMarginReturn.optional(), packageId: z.string(), packageValue: z.string(), pledgorId: z.string(), securedPartyId: z.string(), state: cMPackageState.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 }; /** * Update a collateral package * * PATCH /collateral_management/packages/{packageId} */ export const patchApiCollateralManagementPackagesPackageId = async (args: PatchApiCollateralManagementPackagesPackageIdArgs, options?: PatchApiCollateralManagementPackagesPackageIdOptions) => { const {baseUrl = "https://api.anchorage-staging.com/v2", fetch: fetchFn = fetch, ApiAccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Api-Signature'] != null) headers.set("Api-Signature", String(args['Api-Signature'])) if (args['Api-Timestamp'] != null) headers.set("Api-Timestamp", String(args['Api-Timestamp'])) if (ApiAccessKey !== undefined) headers.set("Api-Access-Key", ApiAccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/collateral_management/packages/${encodeURIComponent(String(args.packageId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(cMPackage, await res.json()) };