Skip to content

JoaquinSkorg/A-Defi-Stablecoin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Foundry DeFi Stablecoin

A decentralized, algorithmic stablecoin protocol built with Foundry and Solidity. Users deposit exogenous crypto collateral (WETH or WBTC) and mint DSC — a token pegged 1:1 to the US dollar — against it. The system is fully on-chain, requires no governance or central authority, and enforces solvency through a strict overcollateralization ratio and an open liquidation mechanism backed by Chainlink price feeds.


About

DSC (Decentralized Stablecoin) is an algorithmic, exogenous-collateral-backed token pegged 1:1 to the US dollar. Users deposit WETH or WBTC as collateral and mint DSC against it.

Key properties:

Property Description
Peg $1.00 USD — maintained via Chainlink price feeds
Stability mechanism Algorithmic (fully decentralized, no governance)
Collateral Exogenous crypto: WETH and WBTC
Overcollateralization 200% minimum — users can mint at most 50% of their collateral value

How It Works

Minting DSC

  1. Deposit WETH or WBTC as collateral into DSCEngine
  2. Mint DSC up to 50% of your collateral's USD value
  3. Your health factor must always stay at or above 1e18

Health Factor

$$\text{Health Factor} = \frac{\text{collateral value (USD)} \times 50}{100 \times \text{DSC minted}}$$

  • Health factor ≥ 1e18 → position is safe
  • Health factor < 1e18 → position is undercollateralized and can be liquidated
  • Health factor is type(uint256).max if no DSC has been minted

Liquidation

If a user's health factor drops below 1e18, anyone can liquidate them:

  • The liquidator repays some or all of the user's DSC debt
  • In return they receive the equivalent collateral plus a 10% bonus
  • The liquidator must maintain their own health factor after the liquidation

Getting Started

Installation

git clone -b main https://github.com/JoaquinSkorg/A-Defi-Stablecoin.git
cd foundry-A-defi-stablecoin-
forge install
forge build

Note: Install OpenZeppelin v4.8.3 :

forge install openzeppelin/openzeppelin-contracts@v4.8.3

Interacting with the Contract

Use cast to interact with a live or local deployment without writing scripts.

1. Wrap ETH into WETH

cast send 0xdd13E55209Fd76AfE204dBda4007C227904f0a81 "deposit()" \
  --value 0.1ether \
  --rpc-url $SEPOLIA_RPC_URL \
  --private-key $PRIVATE_KEY

2. Approve DSCEngine to spend your WETH

cast send 0xdd13E55209Fd76AfE204dBda4007C227904f0a81 "approve(address,uint256)" \
  <DSC_ENGINE_ADDRESS> 1000000000000000000 \
  --rpc-url $SEPOLIA_RPC_URL \
  --private-key $PRIVATE_KEY

3. Deposit collateral and mint DSC

cast send <DSC_ENGINE_ADDRESS> \
  "depositCollateralAndMintDsc(address,uint256,uint256)" \
  0xdd13E55209Fd76AfE204dBda4007C227904f0a81 \
  100000000000000000 \
  10000000000000000 \
  --rpc-url $SEPOLIA_RPC_URL \
  --private-key $PRIVATE_KEY

Deployment

Sepolia Testnet

Set up environment variables (add to .env):

SEPOLIA_RPC_URL=<your-alchemy-or-infura-url>
PRIVATE_KEY=<your-dev-wallet-private-key>
ETHERSCAN_API_KEY=<optional-for-verification>

Get testnet ETH from faucets.chain.link.

Deploy:

forge script script/DeployDSC.s.sol \
  --rpc-url $SEPOLIA_RPC_URL \
  --private-key $PRIVATE_KEY \
  --broadcast \
  --verify \
  --etherscan-api-key $ETHERSCAN_API_KEY

Additional Tools

Estimate gas costs:

forge snapshot
# outputs .gas-snapshot

Format code:

forge fmt

Static analysis with Slither:

slither . --config-file slither.config.json

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors