//#region extensions/crypto/src/services/budget-service.d.ts /** * Budget Enforcement Service — per-operation gas+slippage budget tracking. * * Inspired by Lemon's BudgetTracker/BudgetEnforcer pattern. * Tracks cumulative costs across multi-step compound operations and * halts execution when a budget threshold is exceeded. * * Designed to solve the "agent spends too much gas on a failed swap chain" * problem: if step 1 of a 3-step compound action already burned $X in gas, * stop before step 2 if the remaining budget is insufficient. * * Usage: * const session = budgetService.startSession({ maxGasUsd: 5, maxSlippagePercent: 2 }); * budgetService.recordCost(session.id, { gasUsd: 0.42, slippageUsd: 1.20, stepLabel: 'swap ETH→USDC' }); * const check = budgetService.checkBudget(session.id); * if (!check.ok) { /* halt operation * / } * budgetService.endSession(session.id); */ interface BudgetLimits { /** Max total gas cost in USD across all steps. Default: 10. */ maxGasUsd?: number; /** Max total slippage in USD across all steps. Default: no limit. */ maxSlippageUsd?: number; /** Max slippage as a percentage of trade value. Default: 5. */ maxSlippagePercent?: number; /** Max total cost (gas + slippage + fees) in USD. Default: 25. */ maxTotalCostUsd?: number; /** Max number of on-chain transactions. Default: 10. */ maxTransactions?: number; } interface CostRecord { timestamp: number; stepLabel: string; gasUsd: number; slippageUsd: number; feesUsd: number; tradeValueUsd: number; txHash?: string; } interface BudgetSession { id: string; userId: string; limits: Required; costs: CostRecord[]; status: 'active' | 'completed' | 'exceeded' | 'cancelled'; startedAt: number; endedAt?: number; label?: string; } interface BudgetCheck { ok: boolean; totalGasUsd: number; totalSlippageUsd: number; totalFeesUsd: number; totalCostUsd: number; transactionCount: number; remainingGasUsd: number; remainingTotalUsd: number; remainingTransactions: number; warnings: string[]; blockers: string[]; } declare class BudgetService { private sessions; private userActiveSessions; /** * Start a new budget tracking session for a compound operation. */ startSession(opts: { userId: string; limits?: BudgetLimits; label?: string; }): BudgetSession; /** * Record a cost incurred by a step in the operation. */ recordCost(sessionId: string, cost: { stepLabel: string; gasUsd?: number; slippageUsd?: number; feesUsd?: number; tradeValueUsd?: number; txHash?: string; }): void; /** * Check whether the session is still within budget. * Returns detailed breakdown with warnings and blockers. */ checkBudget(sessionId: string): BudgetCheck; /** * End a budget session. */ endSession(sessionId: string, status?: 'completed' | 'cancelled'): BudgetSession | null; /** * Get the active budget session for a user (if any). */ getActiveSession(userId: string): BudgetSession | null; /** * Get a session by ID. */ getSession(sessionId: string): BudgetSession | null; /** * Format a budget check result as a human-readable string. */ formatBudgetCheck(check: BudgetCheck): string; private getAuditDir; private persistSession; } declare function getBudgetService(): BudgetService; declare function resetBudgetService(): void; //#endregion export { BudgetCheck, BudgetLimits, BudgetSession, CostRecord, getBudgetService, resetBudgetService }; //# sourceMappingURL=budget-service.d.mts.map