import {z} from 'zod' export type TransferType = 'DEPOSIT' | 'WITHDRAW'; export type GetApiV1TokenTransferArgs = {order?: ('asc' | 'desc') | undefined, limit?: number | undefined, cursor?: string | undefined, subaccountId: string, statuses?: (Array<'SUBMITTED' | 'PENDING' | 'COMPLETED' | 'REJECTED'>) | undefined, types?: Array | undefined, orderBy?: 'createdAt' | undefined, createdAfter?: number | undefined, createdBefore?: number | undefined}; export type GetApiV1TokenTransferOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const transferDto = z.object({amount: z.string(), createdAt: z.number().int(), fee: z.string(), finalizedBlockNumber: z.string().optional(), finalizedTransactionHash: z.string().optional(), id: z.string(), initiatedBlockNumber: z.string().optional(), initiatedTransactionHash: z.string().optional(), lzDestinationAddress: z.string().optional(), lzDestinationEid: z.number().int().optional(), status: z.enum(["SUBMITTED", "PENDING", "COMPLETED", "REJECTED"]), subaccountId: z.string(), tokenAddress: z.string(), tokenName: z.string(), type: z.enum(["DEPOSIT", "WITHDRAW"])}); export const pageOfTransfersDtos = z.object({data: z.array(transferDto), 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 a list of transfers for the given subaccount * * GET /v1/token/transfer */ export const getApiV1TokenTransfer = async (args: GetApiV1TokenTransferArgs, options?: GetApiV1TokenTransferOptions) => { 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)) for (const item of args.statuses ?? []) query.append("statuses", String(item)) for (const item of args.types ?? []) query.append("types", String(item)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/token/transfer${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(pageOfTransfersDtos, await res.json()) };