import {z} from 'zod' export type GetApiV1TokenWithdrawArgs = {order?: ('asc' | 'desc') | undefined, limit?: number | undefined, cursor?: string | undefined, subaccountId: string, isActive?: boolean | undefined, orderBy?: 'createdAt' | undefined}; export type GetApiV1TokenWithdrawOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const transferStatus = z.enum(["SUBMITTED", "PENDING", "COMPLETED", "REJECTED"]); export const withdrawDto = z.object({amount: z.string(), createdAt: z.number().int(), finalizedBlockNumber: z.string().optional(), id: z.string(), initiatedBlockNumber: z.string().optional(), lzDestinationAddress: z.string().optional(), lzDestinationEid: z.number().int().optional(), status: transferStatus, subaccount: z.string(), token: z.string(), withdrawDigest: z.string()}); export const pageOfWithdrawDtos = z.object({data: z.array(withdrawDto), hasNext: z.boolean(), nextCursor: z.string().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 }; /** * Returns initiated or pending finalize withdraws for the given subaccount * * GET /v1/token/withdraw */ export const getApiV1TokenWithdraw = async (args: GetApiV1TokenWithdrawArgs, options?: GetApiV1TokenWithdrawOptions) => { const {baseUrl = "https://api.ethereal.trade", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.order != null) query.append("order", String(args.order)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.subaccountId != null) query.append("subaccountId", String(args.subaccountId)) if (args.isActive != null) query.append("isActive", String(args.isActive)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/token/withdraw${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(pageOfWithdrawDtos, await res.json()) };