Skip to content

Commit

Permalink
added protocol collapse usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivendra Singh committed Sep 13, 2023
1 parent 9109854 commit 766eee3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
2. Exchange ETH/BTC <-> $$$

2. Stability Mechanism (Minting): Algorithmic (Decentralized)
1. People will only be able to mint stable coin only with enough collatoral (will be coded)
1. People will only be able to mint stable coin only with enough collatoral (200%) (will be coded)

3. Collateral Type: Exogenous (Crypto)
1. wETH
2. wBTC
2. wBTC

4. Incase a user becomes undercollaterised, they can be liquidated by other users.
1. The liquidator will earn 10% of the debtToCover (the debt of the undercollaterised user he's liquidating) as a liquidation bonus.

5. Incase the protocol becomes undercollaterised due to a drastic drop in the underlying collateral value wherein dscMinted > collateralValue, the protocol should auto-redeem all users collateral, burn all minted coins and render itself collapsed!
50 changes: 50 additions & 0 deletions src/DSCEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ contract DSCEngine is ReentrancyGuard {
error DSCEngine__NotEnoughDSCToBurn(address user, uint256 amountToBurn);
error DSCEngine__HealthFactorOk(address user);
error DSCEngine__HealthFactorNotImproved(address user);
error DSCEngine__DSCNotCollapsed(uint256 totalDscMinted, uint256 totalCollateralValueOfProtocol);

///////////////////
//// Types ////
Expand All @@ -81,6 +82,7 @@ contract DSCEngine is ReentrancyGuard {
uint256 public constant LIQUIDATION_BONUS = 10;
address[] public s_collateralTokens;
address[] public s_tokenPriceFeeds;
address[] s_users;

///////////////////
//// Events //////
Expand All @@ -101,6 +103,8 @@ contract DSCEngine is ReentrancyGuard {

event DSCBurnt(address indexed user, uint256 indexed dscBurnt);

event DSCProtocolCollapsed();

//////////////////////
//// MODIFIERS //////
/////////////////////
Expand Down Expand Up @@ -184,6 +188,8 @@ contract DSCEngine is ReentrancyGuard {
tokenCollateralAddress
] += amountCollateral;

s_users.push(msg.sender);

// emit event
emit CollateralDeposited(
msg.sender,
Expand Down Expand Up @@ -460,6 +466,50 @@ contract DSCEngine is ReentrancyGuard {
return (dscMinted, collateralDepositedValue);
}


/*
* @title CollapseDSC
* @author Shivendra Singh
* @notice In case of total collateral value being MORE than the DSC Minted, the * protocol should render itself as collapsed. In such an event, the protocol *will burn all minted DSC and redeem all deposited collateral of each and every * user.
*/

function _collapseDsc() internal {
uint256 totalCollateralValueOfProtocol = _getTotalCollateralValueOfProtocol();
uint256 totalDSCMinted = i_dsc.totalSupply();

if(totalCollateralValueOfProtocol > totalDSCMinted) {
revert DSCEngine__DSCNotCollapsed(totalDSCMinted, totalCollateralValueOfProtocol);
}

for(uint256 u = 0; u < s_users.length; u++){
address user = s_users[u];
uint256 usersTotalDscMinted = s_DSCMinted[user];

_burnDSC(usersTotalDscMinted, user);

for(uint256 t = 0 ; t < s_collateralTokens.length; t++){
address collateralToken = s_collateralTokens[t];
uint256 collateralTokenAmount = s_collateralDeposited[user][collateralToken];

if(collateralTokenAmount > 0 ) {
_redeemCollateral(collateralToken, collateralTokenAmount, address(this), user);
}
}
}

emit DSCProtocolCollapsed();
}

function _getTotalCollateralValueOfProtocol() internal view returns (uint256) {
uint256 totalCollateralValueOfProtocol;

for(uint256 u = 0; u < s_users.length; u++) {
totalCollateralValueOfProtocol += getAccountCollateralValueInUsd(s_users[u]);
}

return totalCollateralValueOfProtocol;
}

//////////////////////
// GETTER FUNCTIONS //
//////////////////////
Expand Down

0 comments on commit 766eee3

Please sign in to comment.