import {z} from 'zod' export type GetApiLeaderboardWalletPositionSeasonIdArgs = {wallet: string, seasonId: number}; export type GetApiLeaderboardWalletPositionSeasonIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const leaderboardMasteryTitleResponseDto = z.object({name: z.string(), type: z.string(), order: z.number()}); export const leaderboardUserPositionDetailsResponseDto = z.object({liquidity_xp: z.string().nullable(), trading_xp: z.string().nullable(), token_variety: z.string().nullable(), token_variety_names: z.string().nullable(), tvl_days_provisioned: z.number().nullable(), tvl: z.string().nullable(), tvl_increased: z.string().nullable(), mastery_titles: z.array(leaderboardMasteryTitleResponseDto).nullable(), season_id: z.number().nullable(), created_at: z.string().nullable(), id: z.number(), wallet_address: z.string().nullable(), total_xp: z.string(), distribution_percent: z.string()}); export const leaderboardUserPositionResponseDto = z.object({position: z.number().nullable(), details: leaderboardUserPositionDetailsResponseDto.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 user position in leaderboard * * GET /leaderboard/{wallet}/position/{seasonId} */ export const getApiLeaderboardWalletPositionSeasonId = async (args: GetApiLeaderboardWalletPositionSeasonIdArgs, options?: GetApiLeaderboardWalletPositionSeasonIdOptions) => { const {baseUrl = "https://swap-backend.stage.defi.ovh.gala.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/leaderboard/${encodeURIComponent(String(args.wallet))}/position/${encodeURIComponent(String(args.seasonId))}${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(leaderboardUserPositionResponseDto, await res.json()) };