# Looping Strategy

<https://docs.dittonetwork.io/overview/protocol-overview>

## **Overview**

Ditto Automation Labs is designing the first iteration of its automated vault system, standardized on **ERC-7540** (asynchronous deposits and withdrawals), extending ERC-4626. This design ensures that deposits and withdrawals can be processed asynchronously, enabling smooth management of liquidity in complex looped strategies.

The vault will be controlled by a **VaultOperator**, set to the multisig safe of the **Ditto Vaults Curator**. This multisig issues a **session via the Ditto Validator module**, granting the right to trigger automations (activations, deactivations, rebalances) on the vault in a controlled, permissioned manner.

The product’s positioning remains the same: a **hands-free yield automation platform** with execution guarantees powered by Ditto’s decentralized operator network.

### **Core Development Items**

[PoC Spec](https://www.notion.so/PoC-Spec-280d3e1d9514802da9caff5a05282cbc?pvs=21)

1. **Position Valuation**
   * A **smart contract** to deliver the current value of the vault’s position on a lending borrowing protocol.
   * Alternatively, an automation that periodically refetches and updates values.
   * Especially relevant for **cross-chain liquidity allocations**, ensuring share accounting remains correct across deposits and withdrawals.
2. **Strategy Selector Script**
   * A **JavaScript service** that fetches APYs from supported protocols.
   * Compares the profitability of different pool configurations.
   * Decides whether to rebalance, deactivate, or activate assets.
   * **Loop logic**: calculates the target loop number dynamically, subject to a minimum improvement threshold (e.g., skip looping if boost <0.2%).
3. **Router Smart Contract**
   * Handles **position winding and unwinding**.
   * Uses lending pools, uni pools, slippage parameters, and target loop numbers.
   * More efficient than flashloans (0.05% cost of flashloan).
   * Can also **unwind** without flashloans, making exit safer and cheaper.
4. **Withdrawal Processor**
   * Monitors the size of the withdrawal queue and unwinds the required amount marking them available to claim according to current position value and share-to-asset rate.

### **Oracle & Valuation**

To ensure accurate vault accounting:

* Ditto proposes using an **on-chain lens oracle** to track **total position value** at any moment.
* This enables:
  * Fair deposit share issuance.
  * Precise share burns on withdrawals.
  * Transparent, tamper-proof vault valuation for all participants.

### **Guards & Security**

Ditto introduces a dual-guard system to protect vaults and ensure execution integrity:

* **Off-chain guard**
  * Consensus of top-tier operators validating automated actions.
  * Provides an extra layer of human and institutional oversight.
* **On-chain guard**
  * Smart session permissions restrict what automations can execute.
  * Whitelisting of protocols ensures assets are only deployed into approved strategies.

Together, these guarantee **security, accountability, and resilience**.

### **User Journeys**

#### 1. **Asset Managers / Curators**

* Use Ditto Execution Engine and ERC-4626 vaults as **building blocks** to design yield strategies.
* Can **set up new vaults** or upgrade existing ones by configuring:
  * Number of loops / target LTV.
  * Strategy parameters (assets and protocols whitelisted).
* Management is simplified through **modular automation shortcuts** for deposits, withdrawals, rebalances, and emergency exits.

#### 2. **End Users**

* Deposit into vaults.
* Monitor balance, APY, liquidation thresholds, and vault activity through the dashboard.
* Withdraw via the queue mechanism, with full transparency on status and timing.

### **Future Availability**

Currently, Ditto offers **custom private vaults** built in collaboration with selected partners during the **alpha phase**.

After alpha:

* The **Ditto Validator Module** and **Ditto Automation Vault framework** will be opened to **curators and asset managers**.
* This will allow the wider community to **deploy their own strategies** on top of Ditto’s infrastructure.
* Curators will leverage the same automation stack (activator, deactivator, rebalance, compounding, guards) with **plug-and-play tools**.

## Math

#### Theory

$$
\begin{aligned}&\textbf{Parameters:} \\\[4pt]\&P\_0 \quad \text{(initial principal)} \ \&r\_s \quad \text{(supply APY)} \ \&r\_b \quad \text{(borrow APY)} \ &\ell \quad \text{(loan-to-value ratio)} \ \&n \quad \text{(number of loops)} \\\[12pt]
&\textbf{Loop Dynamics:} \\\[4pt]\&S\_k = P\_0 \cdot \ell^{,k-1}, \quad (k=1,2,\dots) \ \&B\_k = P\_0 \cdot \ell^{,k} \ &\Pi^{\text{sup}}\_k = r\_s \cdot S\_k \ \&C^{\text{bor}}*k = r\_b \cdot B\_k \ &\Pi\_k = \Pi^{\text{sup}}*k - C^{\text{bor}}*k = S\_k ,(r\_s - \ell r\_b) \\\[12pt]
&\textbf{Cumulative Profit:} \\\[4pt]&\Pi*{1:n} = P\_0 ,(r\_s - \ell r\_b),\frac{1-\ell^n}{1-\ell} \\\[12pt]
&\textbf{Final APY:} \\\[4pt]&\mathrm{APY}*n = \frac{\Pi*{1:n}}{P\_0}= (r\_s - \ell r\_b),\frac{1-\ell^n}{1-\ell} \\\[12pt]
&\textbf{“Finish (no borrow)” Variant:} \\\[4pt]&\Pi^{\text{finish}}*{1:n}= \frac{P\_0}{1-\ell}\Big\[r\_s,(1-\ell^{,n+1}) - r\_b\ell,(1-\ell^n)\Big] \\\[6pt]&\mathrm{APY}^{\text{finish}}*n= \frac{\Pi^{\text{finish}}*{1:n}}{P\_0}= \frac{r\_s(1-\ell^{,n+1}) - r\_b\ell(1-\ell^n)}{1-\ell} \\\[12pt]
&\textbf{Theoretical Maximum:} \\\[4pt]&\lim*{n\to\infty} \mathrm{APY}*n= \lim*{n\to\infty} \mathrm{APY}^{\text{finish}}\_n= \frac{r\_s - \ell r\_b}{1-\ell}\end{aligned}
$$

### Looping vs Flashloan:

* Take deposit + flashloan = max looped supply
* Borrow max loop borrow amount when boost < threshhold
* Repay flashloan and lose 0.05% on Aave v3 so direct looping is recommended.

### Total supply

$$
\textbf{Total Supply (as $n \to \infty$):} \\\[6pt]
S\_{\text{total},\infty}
\= \lim\_{n \to \infty} P\_0 ,\frac{1-\ell^{,n}}{1-\ell}
\= \frac{P\_0}{1-\ell}
$$

### Total borrow

$$
\textbf{Total Borrow (as $n \to \infty$):} \\\[6pt]
B\_{\text{total},\infty}
\= \lim\_{n \to \infty} P\_0 ,\frac{\ell,(1-\ell^{,n})}{1-\ell}
\= \frac{P\_0 ,\ell}{1-\ell}
$$

## **Core Features**

**1. Automated Liquidity Activation**

* Deposits from users enter a liquidity queue.
* Once the queue surpasses a defined threshold ($10,000), the Ditto Execution Engine automatically:
  * Activates the queued liquidity.
  * Allocates it into the respective strategy loops.
  * Updates the vault state with new locked amounts.

**2. Withdrawal Queue**

* Users can initiate withdrawals through the vault portal.
* Withdrawals are placed in a queue system.
* Once the withdrawal threshold is reached, the Ditto Execution Engine executes asset deactivation, releasing the necessary liquidity.
* Users see:
  * Their individual withdrawal request status.
  * The total withdrawal queue.
  * Estimated execution timeline.

**3. Ditto Execution Engine Automations**

The Ditto Execution Engine powers all operational aspects of the vaults:

* Asset Activation: moving deposits into strategies once thresholds are hit.
* Autocompounding: reinvesting strategy rewards to maximize APY.
* Emergency Unlooping: exit strategy operation in case of critical events.
* Asset Deactivation: processing withdrawal queues by freeing liquidity.

**4. Transparent Manager Dashboard**

The portal provides full visibility into vault activity, risk exposure, and user balances:

* Current APY per vault.
* Amount of funds locked in strategies.
* Liquidation thresholds and their current status.
* Latest rebalances and strategy operations.
* Auto-compounds and activations history.
* Latest withdrawals processed.
* Real-time withdrawal status and queue insights.

## **User journey**

1. Deposit:
   * User selects a vault.
   * Deposit is added to liquidity queue.
   * User claims his shares.
2. Activation:
   * When the queue threshold is reached, assets are looped into the chosen strategy.
3. Growth:
   * Rewards are autocompounded by the Ditto Execution Engine.
   * Users track APY, activations, and portfolio performance through the portal.
4. Withdrawal:
   * User requests withdrawal via the portal.
   * Request enters the withdrawal queue.
   * Once the threshold is reached, assets are deactivated and released.
   * User monitors live withdrawal progress until assets are received.

## **Value Proposition**

* Automated yield optimization without user intervention.
* Flexible risk exposure through configurable vault strategies (risk parameter - `target` **`LTV`**).
* Transparency-first design: all operations, queues, and thresholds are visible to users.
* Secure withdrawal mechanism with clear expectations.
* Failsafe management via emergency unlooping powered by the Ditto Execution Engine.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.dittonetwork.io/automation-modules-for-vaults/looping-strategy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
