import {z} from 'zod' export type GetApiApiV3AccountsAddressJettonsArgs = {address: string}; export type GetApiApiV3AccountsAddressJettonsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const apiJettonVerification = z.enum(["BLACKLISTED", "UNKNOWN", "COMMUNITY", "WHITELISTED"]); export const apiJettonMarketStats = z.object({holders_count: z.number().int(), price_usd: z.number(), price_change_7d: z.number(), price_change_5m: z.number(), price_change_1h: z.number(), price_change_6h: z.number(), price_change_24h: z.number(), volume_usd_24h: z.number(), tvl_usd: z.number(), fdmc: z.number(), mcap: z.number(), trust_score: z.number().int().optional(), volume_buy_usd_24h: z.number().optional(), volume_sell_usd_24h: z.number().optional(), unique_traders_24h: z.number().int().optional(), lp_locked_percent: z.number().optional()}); export const apiJettonLabelRelation = z.object({label: z.string(), label_id: z.number().int(), created_at: z.string(), expires_at: z.string().optional()}); export const apiJettonScaledUiMultiplier = z.object({numerator: z.string(), denominator: z.string()}); export const apiJetton = z.object({created_at: z.string().optional(), address: z.string(), total_supply: z.string().optional(), name: z.string().optional(), symbol: z.string().optional(), decimals: z.number().int(), mintable: z.boolean().optional(), buy_fee: z.number().int().optional(), sell_fee: z.number().int().optional(), verification: apiJettonVerification.optional(), contract_interface: z.string().optional(), image_url: z.string().optional(), market_stats: apiJettonMarketStats.optional(), labels: z.array(apiJettonLabelRelation).optional(), scaled_ui_multiplier: apiJettonScaledUiMultiplier.optional()}); export const apiJettonBalance = z.object({balance: z.string(), jetton_address: z.string(), jetton_wallet: z.string(), jetton: apiJetton.optional()}); export const apiAccountJettons = z.object({items: z.array(apiJettonBalance)}); 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 all jettons owned by the account * * GET /api/v3/accounts/{address}/jettons */ export const getApiApiV3AccountsAddressJettons = async (args: GetApiApiV3AccountsAddressJettonsArgs, options?: GetApiApiV3AccountsAddressJettonsOptions) => { const {baseUrl = "https://tokens.swap.coffee", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("X-Api-Key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/accounts/${encodeURIComponent(String(args.address))}/jettons${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(apiAccountJettons, await res.json()) };