import {z} from 'zod' export type DateTime = string; export type GetApiV1WalletsTransactionsArgs = {'createdAtDatetime[gte]'?: DateTime | undefined, 'createdAtDatetime[lte]'?: DateTime | undefined}; export type GetApiV1WalletsTransactionsOptions = {baseUrl?: string | undefined, jwtTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const custodyTransactionHistoryID = z.string(); export const custodyDirection = z.string(); export const custodyQuantity = z.string(); export const custodySymbol = z.string(); export const networkID = z.string(); export const custodyWithdrawalFee = z.string(); export const custodyDepositMemo = z.string(); export const custodyCreatedAtDateTime = z.string(); export const custodyTransactionStatus = z.string(); export const custodyTransactionDetails = z.object({address: z.string().optional(), blockchainTxId: z.string().optional(), swiftUetr: z.string().optional()}); export const custodyHistory = z.object({custodyTransactionId: custodyTransactionHistoryID.optional(), direction: custodyDirection.optional(), quantity: custodyQuantity.optional(), symbol: custodySymbol.optional(), network: networkID.optional(), fee: custodyWithdrawalFee.optional(), memo: custodyDepositMemo.optional(), createdAtDateTime: custodyCreatedAtDateTime.optional(), status: custodyTransactionStatus.optional(), transactionDetails: custodyTransactionDetails.optional()}); export const getApiV1WalletsTransactionsResponse = z.array(custodyHistory); 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 Custody Transaction History * * GET /v1/wallets/transactions */ export const getApiV1WalletsTransactions = async (args: GetApiV1WalletsTransactionsArgs, options?: GetApiV1WalletsTransactionsOptions) => { 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['createdAtDatetime[gte]'] != null) query.append("createdAtDatetime[gte]", String(args['createdAtDatetime[gte]'])) if (args['createdAtDatetime[lte]'] != null) query.append("createdAtDatetime[lte]", String(args['createdAtDatetime[lte]'])) if (jwtTokenAuth !== undefined) headers.set('authorization', `Bearer ${jwtTokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/wallets/transactions${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(getApiV1WalletsTransactionsResponse, await res.json()) };