import {z} from 'zod' export type GetApiCoinsArgs = {ticker?: Array | undefined, mint?: Array | undefined, owner_id?: Array | undefined, limit?: number | undefined, offset?: number | undefined, query?: string | undefined, sort_method?: ('market_cap' | 'price' | 'volume' | 'created_at' | 'holder') | undefined, sort_direction?: ('asc' | 'desc') | undefined}; export type GetApiCoinsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const dynamicBondingCurveInsights = z.object({address: z.string().optional(), price: z.number().optional(), priceUSD: z.number().optional(), curveProgress: z.number().optional(), isMigrated: z.boolean().optional(), creatorQuoteFee: z.number().optional(), totalTradingQuoteFee: z.number().optional(), creatorWalletAddress: z.string().optional()}); export const artistLocker = z.object({address: z.string().optional(), locked: z.number().optional(), unlocked: z.number().optional(), claimable: z.number().optional()}); export const artistCoinFees = z.object({unclaimed_fees: z.number().optional(), total_fees: z.number().optional()}); export const rewardPool = z.object({address: z.string().optional(), balance: z.number().optional()}); export const coin = z.object({mint: z.string(), ticker: z.string(), decimals: z.number().int(), name: z.string(), logo_uri: z.string().optional(), banner_image_url: z.string().optional(), description: z.string().optional(), x_handle: z.string().optional(), instagram_handle: z.string().optional(), tiktok_handle: z.string().optional(), website: z.string().optional(), link_1: z.string().optional(), link_2: z.string().optional(), link_3: z.string().optional(), link_4: z.string().optional(), has_discord: z.boolean().optional(), created_at: z.string(), updated_at: z.string().optional(), owner_id: z.string().optional(), escrow_recipient: z.string().optional(), dynamicBondingCurve: dynamicBondingCurveInsights.optional(), artist_locker: artistLocker.optional(), artist_fees: artistCoinFees.optional(), reward_pool: rewardPool.optional(), price: z.number().optional(), marketCap: z.number().optional(), holder: z.number().int().optional(), totalSupply: z.number().optional(), liquidity: z.number().optional(), circulatingSupply: z.number().optional(), priceChange24hPercent: z.number().optional(), displayPrice: z.number().optional(), displayMarketCap: z.number().optional()}); export const coinsResponse = z.object({data: z.array(coin)}); 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 }; /** * Gets a list of coins with optional filtering * * GET /coins */ export const getApiCoins = async (args: GetApiCoinsArgs, options: GetApiCoinsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.ticker ?? []) query.append("ticker", String(item)) for (const item of args.mint ?? []) query.append("mint", String(item)) for (const item of args.owner_id ?? []) query.append("owner_id", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.query != null) query.append("query", String(args.query)) if (args.sort_method != null) query.append("sort_method", String(args.sort_method)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) const url = `${baseUrl.replace(/\/+$/, '')}/coins${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(coinsResponse, await res.json()) };