# Rebalancer

<figure><img src="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FsE6W9LrZQ1GDr2hQ3mLu%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.23.14.png?alt=media&#x26;token=c25c79e9-fcff-48b6-93ae-45fc7394a9ea" 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="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FxTcc8LZDDbQedn6S7HnJ%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.36.04.png?alt=media&#x26;token=7c7688cb-8fff-49bc-b55b-53f2f545df09" 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="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FHBROT8Yb2mUGiSjWJsb7%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.37.56.png?alt=media&#x26;token=a99d7c27-efac-47ac-8346-483e150a8bc2" 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="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FuaKqXs7mPBzYF31cWcw1%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.38.57.png?alt=media&#x26;token=a1c3a3ec-e0e2-44b4-8982-cc24dec10a59" 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="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FFjCzZBUp3FIFH46amqsx%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.39.43.png?alt=media&#x26;token=b7f7cb1b-cb8e-4deb-99d4-c0dfe83e3020" alt=""><figcaption></figcaption></figure>

The amount of tokens left in the pool is

<figure><img src="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FC0nVbvlKQJw22eOHwacP%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.40.07.png?alt=media&#x26;token=e94c3b88-2d3c-4f08-ab5a-f00ad804b829" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FEsxEXr0QknFam3FFZToV%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.40.25.png?alt=media&#x26;token=29113391-6e0f-4112-a9f0-4b28082ec4db" alt=""><figcaption></figcaption></figure>

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

The swap satisfies

<figure><img src="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FFaDsdCOwDobamaPgpInN%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.40.52.png?alt=media&#x26;token=e6c54f4b-876a-4bc2-8de3-1e9915c7876e" alt=""><figcaption></figcaption></figure>

Therefore

<figure><img src="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FdhyoY2VFkte1B0bi6IRB%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.41.08.png?alt=media&#x26;token=c8d27fc9-d3f7-4586-af44-117a527d1a48" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FOwkDCMZYa5wMXGyFuQAe%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.41.30.png?alt=media&#x26;token=4af50c14-8baa-4100-aef7-6f2048020742" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FR0a6d7dVou17snTryBUl%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.42.11.png?alt=media&#x26;token=9ca1321c-b62a-4d40-adff-939ab6154240" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FBEllEQOtXuXoWUu80RLz%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.42.26.png?alt=media&#x26;token=f1a97b51-36a4-42b0-b2f6-a4fe826013f3" alt=""><figcaption></figcaption></figure>

And the amount of repaid **AVAX** is

<figure><img src="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FLG2PdavX5BnxHL45FkkJ%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.42.38.png?alt=media&#x26;token=7e2809f4-be4e-474b-92a5-988d64f64c8f" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FkvitmeHijgURoubg6LEM%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.42.52.png?alt=media&#x26;token=28cb5e94-c8b2-4c58-9647-bc3a284b90b3" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FYLtvTmAnoxhuTx8Oil6h%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.43.07.png?alt=media&#x26;token=e17b0802-4379-4d25-a5de-73fcfd044994" 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="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2F6jP4wPwPCjyhCzXEfKeH%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.43.28.png?alt=media&#x26;token=bc3a9337-2f24-49e2-9f14-ce2e89eed9f7" 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="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FG7xpfd73jcKDSE3aTORP%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.43.51.png?alt=media&#x26;token=414b773e-2c30-4fed-8d34-2afdb80c3cbe" alt=""><figcaption></figcaption></figure>

As a result the final leverage is

<figure><img src="https://1625763057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxgY3fQZ4YZGgisqsPOVx%2Fuploads%2FVjvq3VmXC0NsILY7e5Xs%2F%E6%88%AA%E5%B1%8F2022-12-06%20%E4%B8%8B%E5%8D%884.44.08.png?alt=media&#x26;token=57abc418-2732-4beb-864b-50d441ab7d8a" 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).
