import {z} from 'zod' export type GetApiV1KrakenEmbedUsersUserIdEarnSummaryArgs = {user_id: string, currency: string, 'privy-app-id': string}; export type GetApiV1KrakenEmbedUsersUserIdEarnSummaryOptions = {baseUrl?: string | undefined, appSecretAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const krakenEmbedEarnAmount = z.object({native: z.string().min(1).max(64), converted: z.string().min(1).max(64)}); export const krakenEmbedUpcomingReward = z.object({date: z.string(), asset: z.string(), accumulated_amount: krakenEmbedEarnAmount, estimated_pending_amount: krakenEmbedEarnAmount.nullable()}); export const krakenEmbedGetEarnSummaryResult = z.object({auto_earn_eligible: z.boolean(), auto_earn_enabled: z.boolean(), auto_earn_last_changed: z.string().nullable().optional(), payout_period: z.string(), total_allocated_converted: z.string().min(1).max(64), total_rewarded_converted_true_rates: z.string().min(1).max(64), total_rewarded_converted_current_rate: z.string().min(1).max(64), num_earning_assets: z.number().int().gte(0), upcoming_rewards: z.array(krakenEmbedUpcomingReward)}); export const krakenEmbedGetEarnSummaryKrakenResponse = z.object({errors: z.array(z.unknown()).optional(), error: z.array(z.unknown()).optional(), result: krakenEmbedGetEarnSummaryResult.nullable()}); export const krakenEmbedGetEarnSummaryResponse = z.object({data: krakenEmbedGetEarnSummaryKrakenResponse}); 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 Earn Summary * * GET /v1/kraken_embed/users/{user_id}/earn_summary */ export const getApiV1KrakenEmbedUsersUserIdEarnSummary = async (args: GetApiV1KrakenEmbedUsersUserIdEarnSummaryArgs, options?: GetApiV1KrakenEmbedUsersUserIdEarnSummaryOptions) => { const {baseUrl = "https://api.privy.io", fetch: fetchFn = fetch, appSecretAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.currency != null) query.append("currency", String(args.currency)) if (args['privy-app-id'] != null) headers.set("privy-app-id", String(args['privy-app-id'])) if (appSecretAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${appSecretAuth.username}:${appSecretAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/kraken_embed/users/${encodeURIComponent(String(args.user_id))}/earn_summary${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(krakenEmbedGetEarnSummaryResponse, await res.json()) };