import {z} from 'zod' export type GetApiV2StakingPoolAccountIdArgs = {account_id: unknown, 'Accept-Language'?: string | undefined}; export type GetApiV2StakingPoolAccountIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const poolImplementation = z.object({description: z.string(), name: z.string(), socials: z.array(z.string()), url: z.string()}); export const poolImplementationType = z.enum(["whales", "tf", "liquidTF", "ffvault"]); export const poolInfo = z.object({address: z.string(), apy: z.number(), current_nominators: z.number().int(), cycle_end: z.number().int(), cycle_length: z.number().int().optional(), cycle_start: z.number().int(), implementation: poolImplementationType, liquid_jetton_master: z.string().optional(), max_nominators: z.number().int(), min_stake: z.number().int(), name: z.string(), nominators_stake: z.number().int(), total_amount: z.number().int(), validator_stake: z.number().int(), verified: z.boolean()}); export const getApiV2StakingPoolAccountIdResponse = z.object({implementation: poolImplementation, pool: poolInfo}); 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 }; /** * Stacking pool info * * GET /v2/staking/pool/{account_id} */ export const getApiV2StakingPoolAccountId = async (args: GetApiV2StakingPoolAccountIdArgs, options?: GetApiV2StakingPoolAccountIdOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/staking/pool/${encodeURIComponent(String(args.account_id))}${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(getApiV2StakingPoolAccountIdResponse, await res.json()) };