> ## Documentation Index
> Fetch the complete documentation index at: https://docs.meteora.ag/llms.txt
> Use this file to discover all available pages before exploring further.

# Rebalance Crank

When an operator submits a `deposit_strategy` or `withdraw_strategy` transaction, we call this the **rebalance crank**.

<Frame>
  <img src="https://mintcdn.com/meteora/Dq-kAXO9I6PpNwH8/images/overview/other-products/dynamic-vault/rebalance-crank.png?fit=max&auto=format&n=Dq-kAXO9I6PpNwH8&q=85&s=641efc118572e9245d14979e3aa686b8" width="1326" height="982" data-path="images/overview/other-products/dynamic-vault/rebalance-crank.png" />
</Frame>

We define the state variables **before** the rebalance as:

* `vault.total_amount`: $t_1$
* `token_vault.amount`: $a_1$
* `strategy.current_liquidity`: $c_1$

And the state variables **after** the rebalance as:

* `vault.total_amount`: $t_2$
* `token_vault.amount`: $a_2$
* `strategy.current_liquidity`: $c_2$

The vault calculates the total accrued interest (profit) after rebalance as:

```math theme={"system"}
\text{profit} = (c_2 + a_2) - (c_1 + a_1)
```

Then, the vault updates the total amount:

```math theme={"system"}
t_2 = t_1 + \text{profit}
```

Or, equivalently:

```math theme={"system"}
t_2 = t_1 + (c_2 + a_2) - (c_1 + a_1)
```

The immediate release of profit generated to the LPs can result in opportunities for a sandwich attack, as seen in the scenario below:

<Frame>
  <img src="https://mintcdn.com/meteora/Dq-kAXO9I6PpNwH8/images/overview/other-products/dynamic-vault/sandwich-attack-scenario.png?fit=max&auto=format&n=Dq-kAXO9I6PpNwH8&q=85&s=517c3bc3043723e6a1061d2ede5326e8" width="1270" height="318" data-path="images/overview/other-products/dynamic-vault/sandwich-attack-scenario.png" />
</Frame>

An attacker is able to time their deposit before a rebalance and withdraw immediately after a rebalance in order to get the max profit. In the example above, the attacker is able to earn 10 tokens as a reward by executing the sandwich attack.

# Sandwich Attack Prevention

To prevent sandwich attacks, the system does **not** distribute 100% of the yield generated by lending platforms to LPs immediately after each rebalance. Instead, the yield is "dripped" (released gradually) to LPs over a pre-determined period.

## Key State Variables

* **`total_amount`**:\
  The total liquidity in the vault (vault balance + all strategy balances).

* **`last_updated_locked_profit`**:\
  The total profit that is currently locked, updated at every rebalance.

* **`last_report`**:\
  The timestamp of the last rebalance.

* **`locked_profit_degradation`**:\
  The rate at which locked profit is released, expressed as a percentage per second (e.g., 0.1% of locked profits released per second).

***

# How Yield Unlocking Works

When a user adds or removes liquidity, the system uses the **unlocked amount** (not the full `vault.total_amount`) to calculate how many LP tokens to mint or burn. This is done via the `get_unlock_amount` function.

## Calculation Steps

1. **Calculate Duration Since Last Rebalance:**

```math theme={"system"}
\text{duration} = \text{currentTime} - \text{lastReport}
```

2. **Calculate Locked Fund Ratio:**

```math theme={"system"}
\text{lockedFundRatio} = 1 - (\text{duration} \times \text{lockedProfitDegradation})
```

3. **Calculate Unlocked Amount:**

```math theme={"system"}
\text{unlockedAmount} = \text{totalAmount} - (\text{lastUpdatedLockedProfit} \times \text{lockedFundRatio})
```

***

**In summary:**\
Only the unlocked portion of profits is available to LPs at any given time, mitigating the risk of sandwich attacks by preventing instant profit extraction after a rebalance.

## Example: Drip duration \~ 5 minutes

<Frame>
  <img src="https://mintcdn.com/meteora/Dq-kAXO9I6PpNwH8/images/overview/other-products/dynamic-vault/drip-duration-example.png?fit=max&auto=format&n=Dq-kAXO9I6PpNwH8&q=85&s=70b468316b181e1c098db33b465e96c4" width="1210" height="672" data-path="images/overview/other-products/dynamic-vault/drip-duration-example.png" />
</Frame>

As seen in the example above, the rebalancing at t = 2 mins will gain a total of 100 token as yield from the lending platforms. However, as we are using the unlocked amount value to calculate the withdrawal amount, the attacker will not get any of the yield gained if he were to withdraw his tokens immediately after rebalancing. He will also only gain a fraction of the total yield if he withdraws within the next few minutes.
