The Solidity Smart Contracts in this DeFi project use "Openzeppelin" and "Chainlink" to inherit functionalities, "Chainlink PriceFeed's" for ETH and BTC are used, built on Foundry development framework.
This DeFi (decentralized finance) project is an implementation of a CDP ( Collateralized Debt Position ) protocol which allows users to lock crypto assets in a smart contract to borrow or mint stablecoins, the CDP Algorithmically governs the minting and burning of the stablecoin.
In this project the stablecoin is named "DecentralizedStableCoin" (DSC) and the CDP protocol which controles/governs this DSC is called "DSCEngine". This "DSCEngine" which regulates the minting/burning of "DecentralizedStableCoin", accepts approved crypto as collateral, users can deposit wETH and wBTC in exchange for a DSC that will be pegged to the USD, the CDP decides how much DSC each user may mint, enforces safety rules, and allows others to liquidate unsafe positions. Similar to MakerDAO.
Attributes of DSC Stablecoin -
-
Relative stability = This Stablecoin is pegged/anchored to USD.
-
Stability mechanism = The minting and buring is Algorithmic.
-
collateral = Exogenous, minted against crypto currency.
1. wETH (wrapped ETH i.e ERC20 version of ETH) 2. wBTC (wrapped BTC i.e ERC20 version of BTC)
Attributes of DSCEngine -
- Accept collateral : users deposit approved crypto (e.g., wETH/wBTC) into the contract. The engine records how much each user deposited.
- Value collateral : it uses external price feeds so collateral is measured in USD value.
- Allow minting : if you’ve deposited enough collateral, the engine lets you mint (create) DSC up to a safe limit.
- Allow burning & redeeming : you can burn DSC to lower your debt and withdraw your collateral.
- Enforce safety (health factor) : the engine continuously checks whether your collateral still sufficiently covers your DSC. If your position becomes under-collateralized, you risk liquidation.
- Liquidation : third parties can repay part of an unsafe user’s DSC debt in exchange for that user’s collateral plus a bonus (incentive to keep the system solvent).
- Accounting & protections : tracks per-user collateral and minted DSC, emits events when collateral is deposited/redeemed, and has safety checks (no zero amounts, only allowed tokens, protections against reentrancy and failed transfers).
Typical user flows -
- Deposit & borrow : you deposit allowed crypto → engine values it in USD → you mint DSC up to the allowed amount.
- Repay & withdraw : you burn DSC to reduce your debt → once safe, you withdraw your collateral.
- If price falls : your health factor may drop → if it crosses the unsafe threshold, liquidators can act to repay your debt and claim collateral (plus a small bonus).
DecentralizedStableCoin (DSC) -
burn(uint256 _amount) public override onlyOwner
- DSCEngine controlled burn of _amount DSC from msg.sender; checks _amount > 0 and sufficient balance, then calls ERC20Burnable.burn. Reverts on invalid amount or insufficient balance.
mint(address _to, uint256 _amount) external onlyOwner returns (bool)
- DSCEngine controlled minting of _amount DSC to _to; checks _amount > 0 and _to ≠ address(0), mints tokens and returns true.
DSCEngine -
depositCollateralAndMintDsc (address tokenCollateralAddress, uint256 amountCollateral, uint256 amountDscToMint) external
- deposits amountCollateral of tokenCollateralAddress (via depositCollateral) then mints amountDscToMint DSC (via mintDsc) in one txn.
redeemCollateralForDsc (uint256 amountDscToBurn, address tokenCollateralAddress, uint256 amountCollateral) external
- Burns amountDscToBurn DSC from caller and redeems amountCollateral of tokenCollateralAddress to caller; enforces health factor after operations.
redeemCollateral (address tokenCollateralAddress, uint256 amountCollateral) external moreThanZero isAllowedToken nonReentrant
- Redeems amountCollateral of tokenCollateralAddress to caller (withdraw collateral). Reverts if caller’s health factor becomes unhealthy.
burnDsc(uint256 amount) external moreThanZero
- Burns amount DSC from caller (reduces their debt). Rechecks health factor and reverts if it becomes unhealthy.
liquidate (address collateral, address user, uint256 debtToCover) external moreThanZero isAllowedToken nonReentrant
- Liquidator pays debtToCover DSC to cover user’s debt, receives token collateral + liquidation bonus. Requires user to be under-collateralized (health < MIN). Updates balances, burns covered DSC, ensures user's health factor improves and liquidator remains healthy.
mintDsc (uint256 amountDscToMint) public moreThanZero nonReentrant
- Mints amountDscToMint DSC to caller if collateral/backing supports it. Increases caller’s debt, checks health factor, then calls DSC mint. Reverts on insufficient collateral or mint failure.
depositCollateral (address tokenCollateralAddress, uint256 amountCollateral) public moreThanZero isAllowedToken nonReentrant
- Records and transfers amountCollateral of tokenCollateralAddress from caller into the contract; emits CollateralDeposited. Reverts on failed ERC20 transferFrom.
Interact with DSCEngine deployed on Sepolia through Etherscan -
DSCEngine - https://sepolia.etherscan.io/address/0x67bb469c36213d482e7238d052014f48daa90e53#code
DSC - https://sepolia.etherscan.io/address/0xde6e14511c42eE113419303D5f1e047dF9258CdB#code