import {z} from 'zod' export type GetApiV1TreasuryRebalancesArgs = {page?: number | undefined, limit?: number | undefined}; export type GetApiV1TreasuryRebalancesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const rebalanceDesiredFromLegResponse = z.object({account_id: z.string().nullable(), amount: z.string().nullable(), currency: z.string().nullable(), network: z.string().nullable()}); export const rebalanceDesiredToLegResponse = z.object({account_id: z.string().nullable(), amount: z.string().nullable(), currency: z.string().nullable(), network: z.string().nullable()}); export const rebalanceDesiredOverlay = z.object({from: rebalanceDesiredFromLegResponse, to: rebalanceDesiredToLegResponse}); export const rebalanceEstimatedFromLeg = z.object({account_id: z.string().nullable(), amount: z.string().nullable(), currency: z.string().nullable(), network: z.string().nullable()}); export const rebalanceEstimatedToLeg = z.object({account_id: z.string().nullable(), amount: z.string().nullable(), currency: z.string().nullable(), network: z.string().nullable()}); export const rebalanceEstimatedOverlay = z.object({from: rebalanceEstimatedFromLeg, to: rebalanceEstimatedToLeg}); export const rebalanceActualFromLeg = z.object({account_id: z.string().nullable(), amount: z.string().nullable(), currency: z.string().nullable(), network: z.string().nullable()}); export const rebalanceActualToLeg = z.object({account_id: z.string().nullable(), amount: z.string().nullable(), currency: z.string().nullable(), network: z.string().nullable()}); export const rebalanceActualOverlay = z.object({from: rebalanceActualFromLeg, to: rebalanceActualToLeg}); export const rebalanceStepEstimatedFromLeg = z.object({account_id: z.string().nullable(), amount: z.string().nullable(), currency: z.string().nullable(), network: z.string().nullable()}); export const rebalanceStepEstimatedToLeg = z.object({account_id: z.string().nullable(), amount: z.string().nullable(), currency: z.string().nullable(), network: z.string().nullable()}); export const rebalanceStepEstimatedOverlay = z.object({from: rebalanceStepEstimatedFromLeg, to: rebalanceStepEstimatedToLeg}); export const rebalanceStepActualFromLeg = z.object({account_id: z.string().nullable(), amount: z.string().nullable(), currency: z.string().nullable(), network: z.string().nullable()}); export const rebalanceStepActualToLeg = z.object({account_id: z.string().nullable(), amount: z.string().nullable(), currency: z.string().nullable(), network: z.string().nullable()}); export const rebalanceStepActualOverlay = z.object({from: rebalanceStepActualFromLeg, to: rebalanceStepActualToLeg}); export const stepFee = z.object({fee_amount: z.string(), fee_currency: z.string(), fee_type: z.string(), fee_metadata: z.record(z.string(), z.unknown()).optional()}); export const statusReason = z.object({error_code: z.string(), error_message: z.string()}); export const rebalanceStep = z.object({id: z.string(), rebalance_id: z.string(), step_sequence: z.number().int(), step_type: z.enum(["transfer", "swap"]), estimated: rebalanceStepEstimatedOverlay, actual: rebalanceStepActualOverlay, transaction_hash: z.string().nullable().optional(), unsigned_transaction: z.string().nullable().optional(), fees: z.array(stepFee), provider_key: z.enum(["circle_mint", "kraken", "openfx", "turnkey", "zuba"]).nullable().optional(), status: z.enum(["created", "signature_requested", "signed", "submitted", "confirmed", "completed", "failed"]), status_reasons: z.array(statusReason).nullable().optional(), created_at: z.string(), updated_at: z.string(), signature_requested_at: z.string().nullable().optional(), signed_at: z.string().nullable().optional(), submitted_at: z.string().nullable().optional(), confirmed_at: z.string().nullable().optional(), completed_at: z.string().nullable().optional(), failed_at: z.string().nullable().optional()}); export const rebalance = z.object({id: z.string(), workspace_id: z.string(), organization_reference_id: z.string().nullable().optional(), direction: z.literal("rebalance"), desired: rebalanceDesiredOverlay, estimated: rebalanceEstimatedOverlay, actual: rebalanceActualOverlay, balance_status: z.enum(["unreserved", "awaiting_funds", "reserved"]).nullable(), balance_reserved_at: z.string().nullable(), steps: z.array(rebalanceStep), created_at: z.string(), updated_at: z.string(), expires_at: z.string().nullable()}); export const pagination = z.object({page: z.number(), limit: z.number(), total: z.number(), total_pages: z.number(), has_next: z.boolean(), has_prev: z.boolean()}); export const listRebalancesResponse = z.object({data: z.array(rebalance), pagination: pagination}); 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 rebalances * * GET /v1/treasury/rebalances */ export const getApiV1TreasuryRebalances = async (args: GetApiV1TreasuryRebalancesArgs, options?: GetApiV1TreasuryRebalancesOptions) => { const {baseUrl = "https://api.tesser.xyz", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/treasury/rebalances${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(listRebalancesResponse, await res.json()) };