import {z} from 'zod' export type GetApiV1ConfigWalletAddressArgs = {walletAddress: string}; export type GetApiV1ConfigWalletAddressOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, OriginHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const configuration = z.object({token: z.string(), domainIds: z.array(z.number().int()), protocolIds: z.array(z.number().int()), poolAddresses: z.array(z.string()), includeRewardsApy: z.boolean(), totalGasFee: z.string(), totalDeposits: z.string(), totalWithdrawals: z.string(), lastRebalanceTimestamp: z.string().nullable().optional(), rebalanceFrequency: z.number().int(), requiredTvl: z.string(), requiredLiquidityMultiplier: z.string(), spendingCapRaw: z.string().nullable().optional(), spendingCapFormatted: z.string().nullable().optional(), hasSignedConfig: z.boolean()}); export const getConfigResponse = z.object({walletAddress: z.string(), rebalancerAddress: z.string(), rebalancingManagerAddress: z.string(), rebalancerExists: z.boolean(), configurations: z.array(configuration)}); 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 rebalancer configuration * * GET /v1/config/{walletAddress} */ export const getApiV1ConfigWalletAddress = async (args: GetApiV1ConfigWalletAddressArgs, options?: GetApiV1ConfigWalletAddressOptions) => { const {baseUrl = "https://api.metalend.tech", fetch: fetchFn = fetch, ApiKeyAuth, OriginHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) if (OriginHeader !== undefined) headers.set("Origin", OriginHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/config/${encodeURIComponent(String(args.walletAddress))}${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(getConfigResponse, await res.json()) };