import {z} from 'zod' export type KrakenEmbedTransactionType = 'simple_order' | 'simple_order_failed' | 'earn_reward'; export type KrakenEmbedTransactionStatus = 'unspecified' | 'in_progress' | 'successful' | 'failed'; export type KrakenEmbedSortingOrder = 'descending' | 'ascending'; export type KrakenEmbedPortfolioTransactionRefIdType = 'simple_order_quote' | 'simple_order_quote_failed'; export type KrakenEmbedPortfolioTransactionRefId = {type: KrakenEmbedPortfolioTransactionRefIdType, ref_id: string}; export type GetApiV1KrakenEmbedMasterAccountTransactionsArgs = {cursor?: string | undefined, types?: Array | undefined, page_size?: number | undefined, assets?: Array | undefined, from_time?: string | undefined, until_time?: string | undefined, statuses?: Array | undefined, ids?: Array | undefined, sorting?: KrakenEmbedSortingOrder | undefined, quote?: string | undefined, ref_ids?: Array | undefined, 'privy-app-id': string}; export type GetApiV1KrakenEmbedMasterAccountTransactionsOptions = {baseUrl?: string | undefined, appSecretAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const krakenEmbedTransactionType = z.enum(["simple_order", "simple_order_failed", "earn_reward"]); export const krakenEmbedTransactionStatus = z.enum(["unspecified", "in_progress", "successful", "failed"]); export const getApiV1KrakenEmbedMasterAccountTransactionsResponse = z.object({errors: z.array(z.unknown()).optional(), error: z.array(z.unknown()).optional(), result: z.object({transactions: z.array(z.object({id: z.string(), time: z.string(), type: krakenEmbedTransactionType, status: krakenEmbedTransactionStatus.nullable().optional(), ref_id: z.string(), ref_id2: z.string().nullable().optional(), spend: z.object({ledger_id: z.string().nullable().optional(), time: z.string().nullable().optional(), amount: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}), quoted_amount: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}).nullable().optional(), fee: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}).nullable().optional(), quoted_fee: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}).nullable().optional(), total: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}), quoted_total: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}).nullable().optional(), balance: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}).nullable().optional()}).nullable(), receive: z.object({ledger_id: z.string().nullable().optional(), time: z.string().nullable().optional(), amount: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}), quoted_amount: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}).nullable().optional(), fee: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}).nullable().optional(), quoted_fee: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}).nullable().optional(), total: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}), quoted_total: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}).nullable().optional(), balance: z.object({asset: z.string().max(16), asset_class: z.literal("currency").nullable(), amount: z.string().min(1).max(64)}).nullable().optional()}).nullable(), details: z.union([z.object({type: z.literal("simple_order"), quote_id: z.string(), trade_type: z.enum(["unspecified", "buy", "sell", "convert"]), parent_transaction: z.string().nullable()}), z.object({type: z.literal("simple_order_failed"), receive_asset: z.object({asset: z.string(), class: z.literal("currency")}), spend_asset: z.object({asset: z.string(), class: z.literal("currency")}), trade_type: z.enum(["unspecified", "buy", "sell", "convert"]), failure_reason: z.enum(["other", "insufficient_funds", "card_issue", "user_account_issue"])}), z.object({type: z.literal("earn_reward")})]).nullable()})), stats: z.object({transactions_seen: z.number().int().gte(0).nullable()}).nullable(), next_cursor: z.string().nullable()}).nullable()}); 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 Transactions * * GET /v1/kraken_embed/master_account/transactions */ export const getApiV1KrakenEmbedMasterAccountTransactions = async (args: GetApiV1KrakenEmbedMasterAccountTransactionsArgs, options?: GetApiV1KrakenEmbedMasterAccountTransactionsOptions) => { 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.cursor != null) query.append("cursor", String(args.cursor)) for (const item of args.types ?? []) query.append("types", String(item)) if (args.page_size != null) query.append("page_size", String(args.page_size)) for (const item of args.assets ?? []) query.append("assets", String(item)) if (args.from_time != null) query.append("from_time", String(args.from_time)) if (args.until_time != null) query.append("until_time", String(args.until_time)) for (const item of args.statuses ?? []) query.append("statuses", String(item)) for (const item of args.ids ?? []) query.append("ids", String(item)) if (args.sorting != null) query.append("sorting", String(args.sorting)) if (args.quote != null) query.append("quote", String(args.quote)) for (const item of args.ref_ids ?? []) query.append("ref_ids", String(item)) 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/transactions${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(getApiV1KrakenEmbedMasterAccountTransactionsResponse, await res.json()) };