import {z} from 'zod' export type GetApiV1PartnershipStakingMasterAddressAddressArgs = {master_address: string, address: string}; export type GetApiV1PartnershipStakingMasterAddressAddressOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const apiStakingTokenInfo = z.object({token_address: z.string(), asset_usd: z.number(), asset_raw: z.number()}); export const apiStakingPositionPeriods = z.object({period_id: z.number().int(), percentage: z.number().int(), lock_duration: z.number().int()}); export const apiStakingPositionInfo = z.object({id: z.number().int(), locked_asset_address: z.string(), locked_asset_amount_raw: z.number(), locked_native: z.number(), locked_usd: z.number(), start_lock_seconds_utc: z.number().int(), end_lock_seconds_utc: z.number().int(), points_amount: z.number(), period_id: z.number().int(), available_periods: z.array(apiStakingPositionPeriods), earned_rewards: z.array(apiStakingTokenInfo)}); export const apiStakingUserInfo = z.object({total_points: z.number(), locked_tokens: z.array(apiStakingTokenInfo), paid_rewards: z.array(apiStakingTokenInfo), positions: z.array(apiStakingPositionInfo)}); 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 aggregated information about staking for specific user. Requires associated x-api-key. * * GET /v1/partnership/staking/{master_address}/{address} */ export const getApiV1PartnershipStakingMasterAddressAddress = async (args: GetApiV1PartnershipStakingMasterAddressAddressArgs, options?: GetApiV1PartnershipStakingMasterAddressAddressOptions) => { const {baseUrl = "https://backend.swap.coffee", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("X-Api-Key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/partnership/staking/${encodeURIComponent(String(args.master_address))}/${encodeURIComponent(String(args.address))}${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(apiStakingUserInfo, await res.json()) };