import {z} from 'zod' export type TradingAccountType = 'COIN_FUTURES' | 'COIN_MARGINED_SWAP' | 'EXCHANGE' | 'FUNDING' | 'FUNDABLE' | 'FUTURES' | 'FUTURES_CROSS' | 'MARGIN' | 'MARGIN_CROSS' | 'OPTIONS' | 'SPOT' | 'USDT_MARGINED_SWAP_CROSS' | 'USDT_FUTURES' | 'UNIFIED' | 'INTERNAL_BALANCE_1' | 'INTERNAL_BALANCE_2' | 'INTERNAL_BALANCE_3' | 'INTERNAL_BALANCE_4' | 'INTERNAL_BALANCE_5' | 'INTERNAL_BALANCE_6' | 'INTERNAL_BALANCE_7' | 'INTERNAL_BALANCE_8' | 'INTERNAL_BALANCE_9' | 'INTERNAL_BALANCE_10' | 'INTERNAL_BALANCE_11'; export type CreateInternalTransferRequest = {asset: string, amount: string, sourceType: TradingAccountType, destType: TradingAccountType}; export type CreateApiExchangeAccountsExchangeAccountIdInternalTransferArgs = {exchangeAccountId: string, 'Idempotency-Key'?: string | undefined, body: CreateInternalTransferRequest}; export type CreateApiExchangeAccountsExchangeAccountIdInternalTransferOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const systemMessageInfo = z.object({type: z.enum(["WARN", "BLOCK"]).optional(), message: z.string().optional()}); export const internalTransferResponse = z.object({success: z.boolean(), id: z.string().nullable().optional(), status: z.string().nullable().optional(), systemMessages: z.array(systemMessageInfo).nullable().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 }; /** * Internal transfer for exchange accounts * * POST /exchange_accounts/{exchangeAccountId}/internal_transfer */ export const createApiExchangeAccountsExchangeAccountIdInternalTransfer = async (args: CreateApiExchangeAccountsExchangeAccountIdInternalTransferArgs, options?: CreateApiExchangeAccountsExchangeAccountIdInternalTransferOptions) => { const {baseUrl = "https://api.fireblocks.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/exchange_accounts/${encodeURIComponent(String(args.exchangeAccountId))}/internal_transfer${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(internalTransferResponse, await res.json()) };