import {z} from 'zod' export type GetApiV1AccountTransferHistoryArgs = {account?: string | undefined, type?: ('TRANSFER_UNKNOWN' | 'DEPOSIT' | 'WITHDRAW') | undefined, block_time_from?: unknown | undefined, block_time_to?: unknown | undefined, page?: number | undefined, limit?: number | undefined, token_address?: string | undefined, sorted_by?: string | undefined}; export type GetApiV1AccountTransferHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiTransferItemType = z.enum(["TRANSFER_UNKNOWN", "DEPOSIT", "WITHDRAW"]); export const apiTransferItem = z.object({address: z.string().optional(), type: apiTransferItemType.optional(), block_number: z.string().optional(), transaction_hash: z.string().optional(), token_address: z.string().optional(), amount: z.string().optional(), block_time: z.string().optional()}); export const apiGetTransferHistoryResponse = z.object({items: z.array(apiTransferItem).optional(), page: z.number().int().optional(), has_next_page: z.boolean().optional(), total_deposit: z.string().optional(), total_withdraw: 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 }; /** * Get transfer history * * GET /v1/account/transfer-history */ export const getApiV1AccountTransferHistory = async (args: GetApiV1AccountTransferHistoryArgs, options?: GetApiV1AccountTransferHistoryOptions) => { const {baseUrl = "https://api.testnet.rise.trade", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.account != null) query.append("account", String(args.account)) if (args.type != null) query.append("type", String(args.type)) if (args.block_time_from != null) query.append("block_time_from", String(args.block_time_from)) if (args.block_time_to != null) query.append("block_time_to", String(args.block_time_to)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.token_address != null) query.append("token_address", String(args.token_address)) if (args.sorted_by != null) query.append("sorted_by", String(args.sorted_by)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/account/transfer-history${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(apiGetTransferHistoryResponse, await res.json()) };