Elemental-Finance / Elemental-Smart-Contracts

GNU General Public License v3.0
0 stars 0 forks source link

Stability Pool One Way Swap Feature/Compounding vault #3

Open HOVOH opened 1 year ago

HOVOH commented 1 year ago

Overview: Add the ability for an external actor to one-way swap DebtToken (wUSD) -> Collateral (WFTM/sFTM/etc) based on oracle prices (no slippage) so we can recycle the same funds for the liquidations.

Motivation:

In the current implementation, you put DebtToken in the Stability Pool(SP) and when a liquidation occurs they will be used to buy the collateral at a discount (liquidation penalty). So stability depositor are buying Collateral at 90% market price for example. Then the FTM sits in the stability pool until it is withdrawn by the depositor. So the user ends up being exposed to FTM which he might not like, 2x long FTM if he minted the wUSD instead of buying it.

If DebtToken in the SP are depleted, the secondary liquidation mechanism is used and redistribute the debt and collateral to the existing vault. Slightly lowering the LTV of all accounts, but it is considered a non-UX friendly feature.

By allowing the swap of collateral back to debt token in the stability pool, external actors can swap the collateral from liquidations back to debt token and automatically compound their liquidations gains back to the debt token thus limiting their exposition to the collateral token. Most importantly it, improves liquidity in the stability pool by recycling deposits back to debt tokens.

Technical specification

To avoid touching the SP and break the audit track, we will use a vault approach with a strategy that will deposit into the SP. This will give the ability to users to either chose the compounding or not. It will also be possible to contribute this feature back to Gravita :)

Yearn team has done something similar for Liquity, but Gravita's Stability Pool has been rewritten completely. Here is the strategy for reference: https://etherscan.io/address/0xFf72f7C5f64ec2fd79B57d1A69C3311C1bB3EEF1#code

If you're more familiar with Yearn's architecture, go for it, otherwise Beefy's single strat vault is simple and pretty much all we need.

image

User Stories External actors want to swap debt token to collateral tokens SP depositor want to compound their gains in collateral back to debt tokens Administrators want to configure the price feed used to get swap rate

Swap behaviour

The swap behaviour will be based on a pro-rata using oracle prices from the PriceFeed contract.

A swap fee will be taken from the amount in from the swapper.

The debt token is always estimated at 1$.

If there is not enough liquidity, the function should revert. I.e: amount out > collateral balance

Example: Collateral PriceFeed result = 0.5$ Swap feed = 0.2% (0.002) Amount in (debt token)= 100 (100$)

fee amount = swap fee * amount in swap in = amount in - (fee amount) = 100 - (0.002 * 100) = 99.8 amount out = swap in / collateral price = 99.8/ 0.5 = 199,6

transfer fee amount of debt token to the treasury address transfer amount out of collateral token to the swapper

Withdraw behaviour

Users can only deposit the debt token, but if noone has traded in the pool after a liquidation, there will be some collateral. The withdraw function should handle this case and redistribute the collateral based on a pro-rata of the redeemed shares.

Task list

HOVOH commented 11 months ago

Test suite requirements:

BaseStrategy: This contract is a template strategy, it implements RBAC.

Create a mock contract implementing the BaseStrategy contract with emtpty functions. You can make the deployer account the vault or not to test for permissions more easily.

tests: By external address I mean an address that is not supposed to be able to call it.

SPVault:

SPStrategy: There is a mock contract called StabilityPoolTester which can help you test the strategy tests: