import {z} from 'zod' export type CustodySymbol = string; export type CustodyDestinationSigned = boolean; export type CustodyDestinationLocked = boolean; export type GetApiV1WalletsWithdrawalInstructionsCryptoSymbolArgs = {symbol: CustodySymbol, signed?: CustodyDestinationSigned | undefined, locked?: CustodyDestinationLocked | undefined}; export type GetApiV1WalletsWithdrawalInstructionsCryptoSymbolOptions = {baseUrl?: string | undefined, jwtTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const networkID = z.string(); export const custodySymbol = z.string(); export const custodyNetworkAddress = z.string(); export const custodyWithdrawalFee = z.string(); export const custodyWithdrawalMemo = z.string(); export const custodyWithdrawalLabel = z.string(); export const custodyDestinationID = z.string(); export const custodyDestinationVaspName = z.string(); export const custodyDestinationUserWalletType = z.enum(["HOSTED", "SELF_HOSTED", "UNKNOWN"]); export const custodyDestinationSigned = z.boolean(); export const custodyDestinationLocked = z.boolean(); export const custodyLockExpiryDateTime = z.string(); export const custodyCryptoWithdrawalInstructions = z.object({network: networkID, symbol: custodySymbol, address: custodyNetworkAddress, fee: custodyWithdrawalFee, memo: custodyWithdrawalMemo.optional(), label: custodyWithdrawalLabel, destinationId: custodyDestinationID, vaspName: custodyDestinationVaspName.optional(), userWalletType: custodyDestinationUserWalletType, signed: custodyDestinationSigned, locked: custodyDestinationLocked.optional(), lockExpiryDateTime: custodyLockExpiryDateTime.optional()}); export const getApiV1WalletsWithdrawalInstructionsCryptoSymbolResponse = z.array(custodyCryptoWithdrawalInstructions); 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 Withdrawal Instructions for Crypto * * GET /v1/wallets/withdrawal-instructions/crypto/{symbol} */ export const getApiV1WalletsWithdrawalInstructionsCryptoSymbol = async (args: GetApiV1WalletsWithdrawalInstructionsCryptoSymbolArgs, options?: GetApiV1WalletsWithdrawalInstructionsCryptoSymbolOptions) => { const {baseUrl = "https://api.exchange.bullish.com/trading-api", fetch: fetchFn = fetch, jwtTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.signed != null) query.append("signed", String(args.signed)) if (args.locked != null) query.append("locked", String(args.locked)) if (jwtTokenAuth !== undefined) headers.set('authorization', `Bearer ${jwtTokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/wallets/withdrawal-instructions/crypto/${encodeURIComponent(String(args.symbol))}${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(getApiV1WalletsWithdrawalInstructionsCryptoSymbolResponse, await res.json()) };