# Rebalancer

<figure><img src="/files/z0yowD6lMfocndCvWyCM" alt=""><figcaption></figcaption></figure>

Assume the **AVAX-USDC** farm on Trader Joe has $22M liquidity and the price of **AVAX** is $18. That translates to $11M **USDC** and 611,111 (11M/18) **AVAX**. Assuming we have $1M worth of LP on Trader Joe which comes from a 3X LYF position on **Homora**, our debt in **USDC** is 1/6M and our debt in **AVAX** is 1/36M \~ **27.78K AVAX** or **$500K USD.**

Say after some time we accrue $10K in farm token rewards and $2K **USDC** plus $4000/18$ **AVAX** in interest. Therefore

<figure><img src="/files/GFeqQOuOUKoRCs9ol9pE" alt=""><figcaption></figcaption></figure>

Say the price of **AVAX** changes to $20. After the $10K in rewards is added to our **LP**, assume the farm’s liquidity becomes $23.20M so that it has N\_a=11.60M **USDC** and N\_b=580K **AVAX**. The amount of tokens in our **LP** is

<figure><img src="/files/3fGU7evGAsRiRZdPVG2O" alt=""><figcaption></figcaption></figure>

The current leverage becomes 3.29. In order to bring the leverage back to 3 and achieve delta-neutral, we need to remove some **LP tokens**, swap some **USDC** to **AVAX** and repay some of our debts.

The amount of **LP** we need to remove is proven to be

<figure><img src="/files/OFudJsKwI2YIoxcZ2DY8" alt=""><figcaption></figcaption></figure>

We need to remove 5.731% **LP** in our position during rebalance. After that the amount of tokens in our **LP** is

<figure><img src="/files/k0dFwYavW1MJGIoeKjvW" alt=""><figcaption></figcaption></figure>

The amount of tokens left in the pool is

<figure><img src="/files/7a1nvRJgp7U4WTC5aoci" alt=""><figcaption></figcaption></figure>

The optimal amount of **USDC** to be swapped to **AVAX** is proven to be

<figure><img src="/files/JQJFZ63OnwmXuRNYV2st" alt=""><figcaption></figcaption></figure>

which amounts to 3% of the **LP** position.

The swap satisfies

<figure><img src="/files/FoBQRch8MvUmmr6VvLrp" alt=""><figcaption></figcaption></figure>

Therefore

<figure><img src="/files/CPGPkZX8KaN25SMWOXEW" alt=""><figcaption></figcaption></figure>

The amount of **USDC** to repay Delta\_d\_a is the amount withdrawn minus the amount swapped

<figure><img src="/files/Y3t64tMOxT29VQDtyBtE" alt=""><figcaption></figcaption></figure>

Therefore the final amount of **USDC** in debt is

<figure><img src="/files/mXRkmpWyNpzXWGteaeWn" alt=""><figcaption></figcaption></figure>

The final amount of **USDC** in the **LP** after the swap is proven to be

<figure><img src="/files/IjtSeBgUG0t4x6Z7RVJR" alt=""><figcaption></figcaption></figure>

And the amount of repaid **AVAX** is

<figure><img src="/files/Jgm3aIdSVM8Pe0XjtTPj" alt=""><figcaption></figcaption></figure>

The final amount of **AVAX** in debt is

<figure><img src="/files/fMMviQAUP3TyCSUL8SUC" alt=""><figcaption></figcaption></figure>

And the final amount of **AVAX** in the **LP** after the swap is

<figure><img src="/files/q96CpbuL2UdinyHPVJ7d" alt=""><figcaption></figcaption></figure>

which matches the debt amount(up to rounding errors) in **AVAX** to reach delta-neutral.

The swap price of **AVAX** in terms of **USDC** is

<figure><img src="/files/7oeweYYZ72qttKqjcOP6" alt=""><figcaption></figcaption></figure>

which indicates a slippage (including the swap fee) of 0.8%.

Also the pool price of **AVAX** in terms of **USDC** after the swap is

<figure><img src="/files/aFvCq2x0rKU2RS307KB8" alt=""><figcaption></figcaption></figure>

As a result the final leverage is

<figure><img src="/files/eCU50UBgf4ywqGFZVKtW" alt=""><figcaption></figcaption></figure>

The actual calculations are much more accurate and only have single digit errors (in Solidity `uint`). In a word our contract is able to rebalance the pseudo delta-neutral position by removing the optimal amount of **LP** and swapping the minimal amount of tokens, with less than [1,800,000 in gas](https://snowtrace.io/tx/0xe6212b62e78d5800f2f3e86ae043cc560d5a1618480d7487e9d168725f2fa38e).


---

# 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.aperture.finance/docs/legacy-products/crab-market-leveraged-farming-retired/rebalancer.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.
