Skip to content

pattool/Stablecoin-DSC

Repository files navigation

Decentralized Stablecoin (DSC) — Vyper / Moccasin

A decentralized, overcollateralized algorithmic stablecoin protocol built in Vyper 0.4.1 using the Moccasin framework.
Inspired by MakerDAO/DAI. Extended and hardened with additional security features with a security-first design and comprehensive test coverage beyond the original course material from Cyfrin Updraft.


Overview

Users deposit crypto collateral (WETH or WBTC) to mint DSC, a USD-pegged stablecoin. It is backed exclusively by exogenous crypto collateral (WETH and WBTC), and governed by on-chain rules — no admin can print DSC out of thin air. The protocol enforces a minimum 200% collateralization ratio. Undercollateralized positions can be liquidated by anyone.

  • Collateral: Exogenous (WETH, WBTC)
  • Stability Mechanism: Algorithmic / Decentralized
  • Peg: USD (via Chainlink price feeds)

How It Works

  1. Deposit WETH or WBTC as collateral
  2. Mint DSC up to 50% of your collateral value (200% collateralization ratio)
  3. If your health factor drops below 1.0, your position becomes liquidatable
  4. Liquidators repay your debt and receive your collateral plus a bonus

Smart Contracts

Contract Description
decentralized_stable_coin.vy ERC20 DSC token using Snekmate libraries
dsc_engine.vy Core protocol engine: deposits, minting, liquidations, security

Security Features

  • Emergency Pause — Owner can halt all state-changing operations instantly
  • Chainlink Staleness Check — Rejects price data older than 1 hour
  • Invalid Price Check — Rejects zero or negative price feed responses
  • Two-Step Ownership Transfer — Prevents accidental loss of contract ownership; new owner must explicitly accept
  • Health Factor Enforcement — Every mint and redemption checks collateralization ratio

Protocol Features

  • Dynamic Liquidation Bonus — Bonus scales with risk:
    • Health Factor ≥ 0.8 → 10% bonus
    • Health Factor ≥ 0.5 → 15% bonus
    • Health Factor < 0.5 → 20% bonus
  • Deposit & Mint in one transaction — Gas efficient combined operation
  • Redeem & Burn in one transaction — Full exit in a single call
  • Minting Fee — Small protocol fee on DSC minting

Tech Stack

Tool Purpose
Vyper 0.4.1 Smart contract language
Moccasin (mox) Vyper framework (deployment, testing)
Titanoboa EVM interpreter for Python-native testing
Chainlink On-chain price feeds (ETH/USD, BTC/USD)
Snekmate Audited Vyper libraries (ERC20, Ownable)
Hypothesis Property-based fuzz testing

Test Suite

  • Unit tests — Full coverage of all contract functions and edge cases
  • Fuzz tests — Stateful property-based testing with Hypothesis
  • Coverage — 98% on dsc_engine.vy

Key fuzz invariant tested:

The total USD value of collateral held by the protocol must always be ≥ the total DSC supply.


Installation

git clone https://github.com/pattool/Stablecoin-DSC
cd mox-stablecoin-cu
mox install

If you run into environment issues, use uv:

# macOS / Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
uv venv
uv sync
source .venv/bin/activate
# Windows
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

Full uv docs: docs.astral.sh/uv


Quickstart

# Deploy to a local in-memory network (no setup needed)
mox run deploy

# Compile
mox compile

# Run tests
mox test

# Run tests with print output
mox test -s

# Run a specific test
mox test -k test_name -s

License

This project is licensed under either of

at your option.

About

Smart contract for a decentralized stablecoin with collateralized value

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors