import {z} from 'zod' export type GetApiApiSupplyMintAddressArgs = {mintAddress: string}; export type GetApiApiSupplyMintAddressOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const additionalTokenAllocation = z.object({amount: z.string(), recipient: z.string(), claimed: z.boolean(), tokenAccountAddress: z.string().optional()}); export const initialTokenAllocation = z.object({amount: z.string(), claimed: z.boolean()}); export const daoTreasuryTokens = z.object({amount: z.string(), vaultAddress: z.string().optional()}); export const supplyAllocation = z.object({teamPerformancePackage: z.object({amount: z.string(), address: z.string().optional()}).optional(), futarchyAmmLiquidity: z.object({amount: z.string(), vaultAddress: z.string().optional()}).optional(), meteoraLpLiquidity: z.object({amount: z.string(), poolAddress: z.string().optional(), vaultAddress: z.string().optional()}).optional(), additionalTokenAllocation: additionalTokenAllocation.optional(), initialTokenAllocation: initialTokenAllocation.optional(), daoTreasuryTokens: daoTreasuryTokens.optional(), daoAddress: z.string().optional(), launchAddress: z.string().optional(), version: z.string().optional()}); export const tokenSupplyInfo = z.object({mint: z.string(), totalSupply: z.string(), circulatingSupply: z.string(), decimals: z.number().int(), rawTotalSupply: z.string(), allocation: supplyAllocation.optional()}); export const supplyResponse = z.object({result: z.string(), data: tokenSupplyInfo}); 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 }; /** * Supply breakdown * * GET /api/supply/{mintAddress} */ export const getApiApiSupplyMintAddress = async (args: GetApiApiSupplyMintAddressArgs, options: GetApiApiSupplyMintAddressOptions) => { const {baseUrl, 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/supply/${encodeURIComponent(String(args.mintAddress))}${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(supplyResponse, await res.json()) };