PunkFinance / punk.protocol

Punk Protocol is a combined batch of Decentralized Finance(DeFi) products, starting from providing the world's first decentralized annuity using high yield strategies on the Ethereum network.
https://punk.finance
22 stars 9 forks source link

Leveraged Aave borrowing model #7

Closed andreihrs closed 2 years ago

andreihrs commented 3 years ago

Hello,

I implemented a model for leveraged borrowing that's forking ETH mainnet. I followed the ModelInterface you provided and added other helper functions in order to best utilize this strategy.

I have a borrow depth of 6, meaning that I repeat the lending-borrowing process 6 times.

Based on the Reverse Configuration Data or User Account Data from Aave Protocol Data Provider, and Aave Lending Pool, respectively, I calculate the borrowing rate at which I can borrow, based on the collateral factor I deposited inside the lending pool.

I use a variable interest rate mode for this leveraged borrowing process and the min health factor 1.10%. The loan-to-value ratio of the lending pool of the underlying token of the model determines the borrow rate max that I can use to borrow, and we make sure the liquidation threshold is always lower by 10% than the borrow rate.

When I'm withdrawing, I'm either fully deleveraging to withdraw the full amount from the pool (repeatedly repaying and withdrawing my borrowing/lending amounts), or partially deleveraging (meaning I don't fully pay all my loans or withdraw my deposits, until I get the necessary balance in the model that is needed to be withdrawn).

If the amount to be withdrawn from the model to the forge is more than half of the underlying balance in model, or the current health factor for the lending pool that is tied to the user is lower than the min health factor, then do a full deleverage.

There is another function that could be implemented, which is a rebalance of the model, based on the borrow rate and borrow depth (either choose a higher borrow rate or a bigger borrow depth for a riskier strategy, or a more conservative strategy with a lower borrow rate or a smaller borrow depth).

A few other bugs I noticed that I changed:

When I'm testing the contracts, I'm stuck when deploying the Forge contract because: Error: Contract Forge is not upgrade safe --> with the following errors:

 Linking external libraries like `Score` is not yet supported (the same for CommitmentWeight). I tried adding a flag to /// @custom:oz-upgrades-unsafe-allow external-library-linking, but this flag doesn't behave as expected and I raised an issue on OpenZeppelin.