import {z} from 'zod' export type GetApiV2OrdBtcRunesCollectionStatsSearchArgs = {window: '10m' | '1h' | '6h' | '1d' | '7d' | '30d', limit?: number | undefined, offset?: number | undefined, sort: 'volume' | 'volumePercentageChange' | 'totalVolume' | 'sales' | 'salesPercentageChange' | 'totalSales' | 'floorPrice' | 'floorPricePercentageChange' | 'topOffer' | 'listedOverSupply' | 'ownerPercentage' | 'pending' | 'marketCap' | 'ownerCount' | 'listedCount', direction: 'asc' | 'desc', walletAddress?: string | undefined, searchTerm?: string | undefined, allCollections?: boolean | undefined}; export type GetApiV2OrdBtcRunesCollectionStatsSearchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const runeEtchingSchema = z.object({details: z.string().optional()}); export const runeStatsSchema = z.object({rune: z.string().optional(), etching: runeEtchingSchema.optional(), vol: z.number().nullable().optional(), totalVol: z.number().nullable().optional(), unitPriceSats: z.number().nullable().optional(), formattedUnitPriceSats: z.string().nullable().optional(), txnCount: z.number().nullable().optional(), imageURI: z.string().nullable().optional(), unitPriceChange: z.number().nullable().optional(), holderCount: z.number().nullable().optional(), pendingCount: z.number().nullable().optional()}); export const getCollectionStatsRuneResultSchema = z.object({runes: z.array(runeStatsSchema).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 Rune collection stats * * GET /v2/ord/btc/runes/collection_stats/search */ export const getApiV2OrdBtcRunesCollectionStatsSearch = async (args: GetApiV2OrdBtcRunesCollectionStatsSearchArgs, options?: GetApiV2OrdBtcRunesCollectionStatsSearchOptions) => { const {baseUrl = "https://api-mainnet.magiceden.dev/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.window != null) query.append("window", String(args.window)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.walletAddress != null) query.append("walletAddress", String(args.walletAddress)) if (args.searchTerm != null) query.append("searchTerm", String(args.searchTerm)) if (args.allCollections != null) query.append("allCollections", String(args.allCollections)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/ord/btc/runes/collection_stats/search${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(getCollectionStatsRuneResultSchema, await res.json()) };