import {z} from 'zod' export type TimeStampAsString = unknown; export type Authorizer = unknown; export type CustodyDestinationID = string; export type CustodySymbol = string; export type NetworkID = string; export type CustodyQuantity = string; export type CustodyApiEcdsaWithdrawalCommand = {commandType: string, destinationId: CustodyDestinationID, symbol: CustodySymbol, network: NetworkID, quantity: CustodyQuantity}; export type CustodyApiEcdsaWithdrawalRequest = {timestamp: TimeStampAsString, nonce: string, authorizer: Authorizer, command: CustodyApiEcdsaWithdrawalCommand}; export type CreateApiV1WalletsWithdrawalArgs = {'BX-SIGNATURE': string, body: CustodyApiEcdsaWithdrawalRequest}; export type CreateApiV1WalletsWithdrawalOptions = {baseUrl?: string | undefined, jwtTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const custodyTransactionID = z.string(); export const custodyApiWithdrawalResponse = z.object({statusReason: z.string().optional(), statusReasonCode: z.number().int().optional(), custodyTransactionId: custodyTransactionID.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 }; /** * Create Withdrawal * * POST /v1/wallets/withdrawal */ export const createApiV1WalletsWithdrawal = async (args: CreateApiV1WalletsWithdrawalArgs, options?: CreateApiV1WalletsWithdrawalOptions) => { const {baseUrl = "https://api.exchange.bullish.com/trading-api", fetch: fetchFn = fetch, jwtTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['BX-SIGNATURE'] != null) headers.set("BX-SIGNATURE", String(args['BX-SIGNATURE'])) if (jwtTokenAuth !== undefined) headers.set('authorization', `Bearer ${jwtTokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/wallets/withdrawal${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(custodyApiWithdrawalResponse, await res.json()) };