import {z} from 'zod' export type GetApiSapiV1NftHistoryTransactionsArgs = {orderType: number, startTime?: number | undefined, endTime?: number | undefined, limit?: number | undefined, page?: number | undefined, recvWindow?: number | undefined, timestamp: number, signature: string}; export type GetApiSapiV1NftHistoryTransactionsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSapiV1NftHistoryTransactionsResponse = z.object({total: z.number().int(), list: z.array(z.object({orderNo: z.string(), tokens: z.array(z.object({network: z.string(), tokenId: z.string(), contractAddress: z.string()})), tradeTime: z.number().int(), tradeAmount: z.string(), tradeCurrency: z.string()}))}); 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 NFT Transaction History (USER_DATA) * * GET /sapi/v1/nft/history/transactions */ export const getApiSapiV1NftHistoryTransactions = async (args: GetApiSapiV1NftHistoryTransactionsArgs, options?: GetApiSapiV1NftHistoryTransactionsOptions) => { const {baseUrl = "https://api.binance.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.orderType != null) query.append("orderType", String(args.orderType)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.recvWindow != null) query.append("recvWindow", String(args.recvWindow)) if (args.timestamp != null) query.append("timestamp", String(args.timestamp)) if (args.signature != null) query.append("signature", String(args.signature)) if (ApiKeyAuth !== undefined) headers.set("X-MBX-APIKEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/sapi/v1/nft/history/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(getApiSapiV1NftHistoryTransactionsResponse, await res.json()) };