Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Morpho leverage extension #185

Merged
merged 23 commits into from
Oct 7, 2024
Merged

Morpho leverage extension #185

merged 23 commits into from
Oct 7, 2024

Conversation

ckoopmann
Copy link
Collaborator

No description provided.

* Collateral balance * (net borrow limit - existing borrow balance) / net borrow limit
*
* Net borrow limit is calculated as:
* Collateral Balance * Collateral Price in Borrow units * morpho LLTV * (1 - unutilized leverage %)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why the borrow limit increases when there is more capacity to borrow ie the unutilized leverage percentage increases. This sounds counter intuitive to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we delever we:

  1. Withdraw collateral
  2. Trade collateral for borrow tokens
  3. Repay borrow tokens

In this case the, the borrow limit is a bit of a misnomer actually as it describes the maximum amount of collateral tokens that can be withdrawn in step 1 without resulting in an "unhealthy" position. (and triggering a revertion here).
The more unutilized leverage the higher that "maximum withdrawal amount" (as it should be called in the delver case) is.

In the case of levering we:

  1. Borrow
  2. Swap From Borrow To Collateral
  3. Deposit Collateral

In this scenario the max borrow limit is aptly named as it describes the maximum amount of tokens that can be borrowed in step 1.

@MerlinEgalite Let me know if:

  1. That makes it more intuitive
  2. Our math here seems wrong to you when calculating said limit
  3. There is any way to defer the position health check maybe to the end of the whole levering / delevering process (i.e. after repaying the loan / depositing collateral) in which case we could increase this limit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the resulting value is used as a an upper limit here for the rebalance "chunk" size.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clear to me thx

Copy link
Contributor

@edkim edkim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me. I wasn't able to spot any issues. Excellent work 🔥

// Issue 1 SetToken
issueQuantity = ether(1);

await morphoLeverageModule.sync(setToken.address, { gasLimit: 10000000 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why you have to call sync here? I thought there would be no deposit or borrow positions in Morpho before calling engage.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I didn't need to actually.
Removed it here: 3104684

@ckoopmann ckoopmann merged commit 7202ed7 into master Oct 7, 2024
5 checks passed
@ckoopmann ckoopmann deleted the morpho-leverage-extension branch October 7, 2024 11:18
Copy link

github-actions bot commented Oct 7, 2024

🎉 This PR is included in version 0.30.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants