import {z} from 'zod' export type GetApiApiV1ReferralsClientsArgs = {referrer_account: string, days?: number | undefined, limit?: number | undefined, offset?: number | undefined, segment?: string | undefined, search?: string | undefined, include_identities?: boolean | undefined}; export type GetApiApiV1ReferralsClientsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const clientSegment = z.enum(["active", "at_risk", "dormant", "never_traded"]); export const referralClient = z.object({amps_all_time: z.number(), client: z.string(), commission_usd: z.number(), external_address: z.string().optional(), joined_unix_ms: z.number().int(), last_trade_unix_ms: z.number().int(), maker_volume_usd: z.number(), primary_subaccount: z.string().optional(), segment: clientSegment, source_code: z.string(), subaccounts: z.array(z.string()).optional(), taker_volume_usd: z.number(), trades: z.number().int().gte(0), wallet_name: z.string().optional()}); export const clientSegments = z.object({active: z.number().int().gte(0), at_risk: z.number().int().gte(0), dormant: z.number().int().gte(0), never_traded: z.number().int().gte(0)}); export const referralClientsResponse = z.object({clients: z.array(referralClient), referrer_account: z.string(), segments: clientSegments, total_count: z.number().int().gte(0)}); 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 a referrer's referred clients with lifecycle segments * * GET /api/v1/referrals/clients */ export const getApiApiV1ReferralsClients = async (args: GetApiApiV1ReferralsClientsArgs, options?: GetApiApiV1ReferralsClientsOptions) => { const {baseUrl = "https://api.mainnet.aptoslabs.com/decibel", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.referrer_account != null) query.append("referrer_account", String(args.referrer_account)) if (args.days != null) query.append("days", String(args.days)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.segment != null) query.append("segment", String(args.segment)) if (args.search != null) query.append("search", String(args.search)) if (args.include_identities != null) query.append("include_identities", String(args.include_identities)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/referrals/clients${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(referralClientsResponse, await res.json()) };