import {z} from 'zod' export type GetApiV1UsersAddressTokensArgs = {search?: string | undefined, status?: ('all' | 'graduated' | 'active') | undefined, tokenName?: string | undefined, type?: ('held' | 'created') | undefined, address: string}; export type GetApiV1UsersAddressTokensOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const tokenPageInfoResponseDto = z.object({endCursor: z.string().nullable(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean(), startCursor: z.string().nullable()}); export const userTokenListItemResponseDto = z.object({additionalKey: z.string().nullable().optional(), category: z.string().nullable().optional(), collection: z.string().nullable().optional(), createdAt: z.string().optional(), creatorAddress: z.string().optional(), decimals: z.string().optional(), description: z.string().optional(), holdingPriceGala: z.union([z.number(), z.string()]).optional(), holdingPriceUsd: z.number().optional(), id: z.number().optional(), image: z.string().optional(), isFinalized: z.boolean().optional(), name: z.string().optional(), quantity: z.string().optional(), symbol: z.string().optional(), tokenName: z.string().optional(), totalUsdValue: z.string().optional(), type: z.string().nullable().optional(), updatedAt: z.string().optional(), vaultAddress: z.string().optional(), verify: z.boolean().optional()}); export const userTokenListResponseDto = z.object({pageInfo: tokenPageInfoResponseDto, token: z.array(userTokenListItemResponseDto)}); export const streamStatus = z.enum(["ACTIVE", "IDLE", "DISABLED"]); export const profileLaunchItemResponseDto = z.object({bondingCurvePercentage: z.number(), createdAt: z.string(), currentViewerCount: z.number(), image: z.string(), isFinalized: z.boolean(), muxStreamStatus: z.union([streamStatus, z.unknown()]), symbol: z.string(), tokenName: z.string(), vaultAddress: z.string(), volume: z.number(), volumeSum: z.number()}); export const profileLaunchesResponseDto = z.object({launches: z.array(profileLaunchItemResponseDto), pageInfo: tokenPageInfoResponseDto}); export const getApiV1UsersAddressTokensResponse = z.object({error: z.literal(false), message: z.string(), status: z.number(), data: z.union([userTokenListResponseDto, profileLaunchesResponseDto])}); 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 tokens for user * * GET /v1/users/{address}/tokens */ export const getApiV1UsersAddressTokens = async (args: GetApiV1UsersAddressTokensArgs, options?: GetApiV1UsersAddressTokensOptions) => { const {baseUrl = "https://lpad-backend.stage.defi.ovh.gala.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.status != null) query.append("status", String(args.status)) if (args.tokenName != null) query.append("tokenName", String(args.tokenName)) if (args.type != null) query.append("type", String(args.type)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/users/${encodeURIComponent(String(args.address))}/tokens${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(getApiV1UsersAddressTokensResponse, await res.json()) };