import {z} from 'zod' export type KrakenEmbedIncludeCurrentDayPnlQueryParam = 'true' | 'false'; export type GetApiV1KrakenEmbedMasterAccountSummaryArgs = {quote?: string | undefined, 'include[current_day_pnl]'?: KrakenEmbedIncludeCurrentDayPnlQueryParam | undefined, 'privy-app-id': string}; export type GetApiV1KrakenEmbedMasterAccountSummaryOptions = {baseUrl?: string | undefined, appSecretAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const krakenEmbedCurrentDayPnl = z.object({since: z.string(), pnl: z.string().min(1).max(64)}); export const krakenEmbedPortfolioSummaryPayload = z.object({timestamp: z.string(), currency: z.string().max(16), portfolio_value: z.string().min(1).max(64), withheld_value: z.string().min(1).max(64), open_orders: z.string().min(1).max(64), available_balance: z.string().min(1).max(64), lots_upnl: z.string().min(1).max(64).nullable().optional(), cost_basis: z.string().min(1).max(64).nullable().optional(), current_day_pnl: krakenEmbedCurrentDayPnl.nullable().optional()}); export const krakenEmbedGetPortfolioSummaryResult = z.object({errors: z.array(z.string()).optional(), error: z.array(z.string()).optional(), result: krakenEmbedPortfolioSummaryPayload.nullable()}); export const krakenEmbedGetPortfolioSummaryResponse = z.object({data: krakenEmbedGetPortfolioSummaryResult}); 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 Master Account Portfolio Summary * * GET /v1/kraken_embed/master_account/summary */ export const getApiV1KrakenEmbedMasterAccountSummary = async (args: GetApiV1KrakenEmbedMasterAccountSummaryArgs, options?: GetApiV1KrakenEmbedMasterAccountSummaryOptions) => { 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.quote != null) query.append("quote", String(args.quote)) if (args['include[current_day_pnl]'] != null) query.append("include[current_day_pnl]", String(args['include[current_day_pnl]'])) 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/master_account/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(krakenEmbedGetPortfolioSummaryResponse, await res.json()) };