import {z} from 'zod' export type CreateOrUpdateConfigRequest = {walletAddress: string, token: 'USDC' | 'MUSD' | 'USDT' | 'RLUSD' | 'USDG' | 'USDE' | 'PYUSD', domainIds: Array, protocolIds: Array<0 | 1 | 2 | 3>, poolAddresses: Array, signature: string, includeRewardsApy?: boolean | undefined, requiredTvl?: number | undefined, requiredLiquidityMultiplier?: number | undefined, spendingCapRaw?: string | undefined, collateralExposure?: (Array | null) | undefined}; export type UpdateApiV1ConfigArgs = {body: CreateOrUpdateConfigRequest}; export type UpdateApiV1ConfigOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, JwtAuth?: string | undefined, OriginHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const createOrUpdateConfigResponse = z.object({walletAddress: z.string(), rebalancerAddress: z.string(), configuredToken: z.string(), operation: z.enum(["CREATED", "UPDATED"])}); 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 }; /** * Update rebalancer configuration * * PUT /v1/config */ export const updateApiV1Config = async (args: UpdateApiV1ConfigArgs, options?: UpdateApiV1ConfigOptions) => { const {baseUrl = "https://api.metalend.tech", fetch: fetchFn = fetch, ApiKeyAuth, JwtAuth, OriginHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) if (JwtAuth !== undefined) headers.set("Authorization", JwtAuth) if (OriginHeader !== undefined) headers.set("Origin", OriginHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/config${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createOrUpdateConfigResponse, await res.json()) };