What is the Optimizer?
The Optimizer is a stateless service that calculates optimal capital allocation across DeFi lending protocols. It looks at current APRs, gas costs, and constraints to find the distribution of capital with the highest net return.How It Works
Two Integration Patterns
Automatic (Agentic)
Agents use the optimizer automatically on a continuous loop. You activate the agent and it handles optimization internally:- Agent calls optimizer with current allocations
- Optimizer returns optimal allocation + action plan
- Agent executes the rebalancing transactions
- Repeat on regular optimization cycles
Manual (IaaS)
Call the optimizer directly for custom implementations. You receive the optimal allocation and decide whether and when to execute. See the IaaS Integration Guide for a complete walkthrough.Stateless Design
The optimizer is completely stateless:- No storage of historical data
- Each call is independent
- Same inputs return same outputs (for same market conditions)
- No side effects
Optimization Algorithm
Factors Considered
| Factor | Description |
|---|---|
| Protocol APRs | Real-time APRs from each protocol for the specific token, weighted by allocation size |
| Gas Costs | Estimated gas for each rebalancing transaction. Optimization only proceeds if APR improvement exceeds gas costs |
| Protocol Liquidity | Available liquidity in each protocol. Won’t allocate more than the protocol can efficiently handle |
| Slippage | Price impact of large deposits/withdrawals, especially for smaller protocols |
| Constraints | User-defined constraints (min protocols, max per protocol, exclusions). See Constraints |
| Transaction Minimization | Prefers fewer, larger transactions over many small ones to save gas |
Optimization Steps
- Fetch Data — Get current APRs, gas prices, liquidity
- Apply Constraints — Filter out invalid allocations
- Calculate Scores — Score each possible allocation
- Select Optimal — Choose highest net return allocation
- Generate Plan — Create minimal set of transactions
- Validate — Ensure plan respects all constraints
Optimizer Output
The optimizer returns three things:- Optimization Result — Optimal allocations, APR improvement, gas estimate, break-even days
- Action Plan — Step-by-step deposit/withdraw instructions to reach the optimal allocation
- Execution Calldata — Ready-to-execute transaction data (contract addresses, function calls, parameters)
Best Practices
- Don’t over-optimize — Calling too frequently wastes gas. Let APR differences accumulate. Good: every 6-24 hours. Bad: every 5 minutes.
- Set APR thresholds — Only rebalance if improvement exceeds a threshold (e.g., 0.3%)
- Use constraints — Always set constraints to match your risk tolerance
- Consider gas prices — During high gas periods, higher APR improvement is needed to justify rebalancing