import {z} from 'zod' export type GetApiV2RewardsRoundRewardsArgs = {election_id?: number | undefined, block?: number | undefined, unixtime?: number | undefined, shallow?: boolean | undefined}; export type GetApiV2RewardsRoundRewardsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const nominatorRewardEntry = z.object({address: z.string(), effective_stake: z.number().int(), reward: z.number().int(), stake: z.number().int(), weight: z.number()}); export const validatorRewardEntry = z.object({effective_stake: z.number().int(), nominators: z.array(nominatorRewardEntry).optional(), nominators_count: z.number().int().optional(), nominators_stake: z.number().int().optional(), owner_address: z.string().optional(), pool: z.string().optional(), pool_type: z.enum(["nominator-pool-v1.0", "single-nominator-pool-v1.0", "single-nominator-pool-v1.1", "other"]).optional(), public_key: z.string(), rank: z.number().int(), reward: z.number().int(), total_stake: z.number().int().optional(), validator_address: z.string().optional(), validator_reward_share: z.number().optional(), validator_stake: z.number().int().optional(), weight: z.number()}); export const roundRewardsResponse = z.object({election_id: z.number().int(), end_block: z.number().int(), error: z.string().optional(), next_election_id: z.number().int().optional(), prev_election_id: z.number().int().optional(), response_time_ms: z.number().int(), round_end: z.string(), round_start: z.string(), start_block: z.number().int(), total_bonuses: z.number().int(), total_stake: z.number().int(), validators: z.array(validatorRewardEntry)}); 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 per-validator reward distribution for a finished round * * GET /v2/rewards/round-rewards */ export const getApiV2RewardsRoundRewards = async (args: GetApiV2RewardsRoundRewardsArgs, options?: GetApiV2RewardsRoundRewardsOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.election_id != null) query.append("election_id", String(args.election_id)) if (args.block != null) query.append("block", String(args.block)) if (args.unixtime != null) query.append("unixtime", String(args.unixtime)) if (args.shallow != null) query.append("shallow", String(args.shallow)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/rewards/round-rewards${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(roundRewardsResponse, await res.json()) };