import {z} from 'zod' export type GetApiUsersIdCostBasisArgs = {id: number, method?: ('fifo' | 'lifo') | undefined}; export type GetApiUsersIdCostBasisOptions = {baseUrl: string, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const costBasisHoldingDto = z.object({chainId: z.object({}).nullable(), chainKey: z.object({}).nullable(), assetAddress: z.string(), symbol: z.object({}).nullable(), decimals: z.object({}).nullable(), quantity: z.string(), costBasisUsd: z.string(), averageUnitCostUsd: z.string().nullable(), hasEstimatedBasis: z.boolean(), hasUnknownBasis: z.boolean(), lotCount: z.number(), currentUnitPriceUsd: z.string().nullable(), valuationPriceSource: z.enum(["registry", "registry_stale", "none"]), marketValueUsd: z.string().nullable(), unrealizedGainLossUsd: z.string().nullable()}); export const costBasisDisposalDto = z.object({actionId: z.string(), actionType: z.string(), chainId: z.object({}).nullable(), chainKey: z.object({}).nullable(), assetAddress: z.string(), symbol: z.object({}).nullable(), quantity: z.string(), proceedsUsd: z.string().nullable(), costBasisUsd: z.string().nullable(), realizedGainLossUsd: z.string().nullable(), acquiredAt: z.number(), disposedAt: z.number(), longTerm: z.boolean(), estimatedBasis: z.boolean(), basisUnknown: z.boolean()}); export const costBasisUnmatchedDisposalDto = z.object({actionId: z.string(), chainId: z.object({}).nullable(), chainKey: z.object({}).nullable(), assetAddress: z.string(), symbol: z.object({}).nullable(), quantity: z.string(), proceedsUsd: z.string().nullable(), disposedAt: z.number(), reason: z.enum(["no_open_lots", "insufficient_lots"])}); export const costBasisSkippedDto = z.object({legId: z.string(), reason: z.string()}); export const costBasisResponseDto = z.object({method: z.enum(["fifo", "lifo"]), holdings: z.array(costBasisHoldingDto), disposals: z.array(costBasisDisposalDto), unmatchedDisposals: z.array(costBasisUnmatchedDisposalDto), skipped: z.array(costBasisSkippedDto)}); 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 }; /** * Cost basis for the caller’s active wallet: holdings valued at read time, realized disposals, unmatched disposals, and skipped legs * * GET /users/{id}/cost-basis */ export const getApiUsersIdCostBasis = async (args: GetApiUsersIdCostBasisArgs, options: GetApiUsersIdCostBasisOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.method != null) query.append("method", String(args.method)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/users/${encodeURIComponent(String(args.id))}/cost-basis${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(costBasisResponseDto, await res.json()) };