Skip to content

viveknath13/StableCoin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Defi

. Defi is Decentralized finance means no centralized authority . Decentralized finance (DeFi) refers to blockchain-connected platforms and applications that can replace existing centralized financial services networks, allowing people to transact directly
with each other without intermediaries like banks.

Stable coin

A stablecoin is a non-volatile crypto asset. A stablecoin is a crypto asset whose buying power stays relatively stable.

A simple example of unstable buying power is Bitcoin (BTC). The number of apples one could buy with 1BTC 6 months ago is drastically different from how many one could buy today. Stablecoins, through a variety of mechanisms we'll discuss later, endeavor to be more similar to a crypto dollar where the number of apples you buy is relatively stable over time.

The 3 functions of money:

  1. Storage of Value
  2. Unit of Account
  3. Medium of Exchange

Storage of Value: Money retains value over time, allowing individuals to save and defer consumption until a later date. This function is crucial because it means money can be saved, retrieved, and spent in the future without losing its purchasing power (assuming stable inflation).

Unit of Account: Money provides a standard numerical unit of measurement for the market value of goods, services, and other transactions. It enables consumers and businesses to make informed decisions because prices and costs can be compared. This function helps in record-keeping and allows for the consistent measurement of financial performance.

Medium of Exchange: Money serves as an intermediary in trade, making transactions more efficient than bartering. In a barter system, two parties must have exactly what the other wants, known as a double coincidence of wants. Money eliminates this issue by providing a common medium that everyone accepts in exchange for goods and services.

In Web3, we need a Web3 version of this. This is where stablecoins shine. Assets like BTC and ETH do well as stores of value and as mediums of exchange, but fail as a reasonable unit of account due to their buying power volatility.

Categories and Properties

This is the second place I strongly disagree with most media! When someone searches for types of stablecoins you'll often see them grouped into common buckets:

  • Fiat-Collateralized
  • Crypto-Collateralized
  • Commodity-Collateralized
  • Algorithmic

This again is a serviceable understanding of stablecoin categories, but the reality is much more complicated. I prefer to categorize stablecoins as:

  1. Relative Stability - Pegged/Anchored or Floating
  2. Stability Method - Governed or Algorithmic
  3. Collateral Type - Endogenous or Exogenous

Relative Stability: Something is only stable relative to its value in something else. The most common type of stablecoins are pegged or anchored stablecoins. Their value is determined by their anchor to another asset such as the US Dollar. Tether, DAI and USDC are examples of stablecoins which are pegged to USD.

These stablecoins general possess a mechanism which makes them nearly interchangable with the asset to which they're pegged. For example, USDC claims that for each USDC minted, there's an equivalent US Dollar (or equal asset) in a bank account somewhere.

DAI on the other hand, uses permissionless over-colleralization - but we'll get to that later!

As mentioned, stablecoins don't have to be pegged. Even when pegged to a relatively stable asset like the US Dollar, forces such as inflation can reduce buying power over time. A proposed solution (that's albeit much more complex) are floating value stablecoins, where, through clever math and algorithms the buying power of the asset is kept stable overtime without being anchors to another particular asset. Stability Method: Another major delineating factor of stablecoins is the stability method employed. This is the mechanism that keeps the asset's value stable. How is the asset pegged to another?

This usually works by having the stablecoin mint and burn in very specific ways and is usually determined by who or what is doing the mint/burn.

This process can exist on a spectrum between governed and algorithmic.

  • Governed: This denotes an entity which ultimately decides if stablecoins in a protocol should be minted or burned. This could something very centralized and controller, like a single person, or more democratic such as governed via DAO. Governed stablecoins are generally considered to be quite centralized. This can be tempered by DAO participations, but we'll get more into how that affects things later
    • Examples of governed stablecoins include:
      • USDC
      • Tether
      • USDT
  • Algorithmic: Conversely, algorithmic stablecoins maintain their stability through a permissionless algorithm with no human intervention. I would consider a stablecoin like DAI as being an example of an algorithmic stablecoin for this reason. All an algorithmic stablecoin is, is one which the minting and burning is dictated by autonomous code.
    • Examples of algorithmic stablecoins include:
      • DAI
      • FRAX
      • RAI
      • UST - RIP, we'll talk more about this later.

Invariants

  • what is invariants / properties ?

Invariants and properties are fundamental concepts in smart contract testing, especially in the context of fuzzing. They represent conditions that should always hold true regardless of the inputs or state changes in your contract. Invariants are conditions that must remain true throughout the entire execution of a smart contract, regardless of what functions are called or in what order.

DSCEngineTest

Purpose of the Code DSCEngineTest.t.sol is a testing file that checks if the DSCEngine (Decentralized Stable Coin Engine) contract works correctly. The DSCEngine is part of a system that creates a cryptocurrency whose value stays stable, similar to how a dollar maintains relatively stable value. The tests make sure that all the functions in the DSCEngine work as expected before deploying it to a real blockchain.

Inputs The test file doesn't take direct inputs from users. Instead, it:

Sets up a test environment with mock (fake) tokens like WETH and WBTC Creates a test user with a starting balance of 10 ether Uses predefined constants like COLLATERAL_AMOUNT (10 ether) to test the system The test file produces test results that show whether each function in the DSCEngine works correctly. It uses assertions (like assertEq) to check if the actual results match the expected results. When tests pass, it means the contract is working as designed. If tests fail, it indicates there's a bug that needs fixing.

How It Achieves Its Purpose The test file works through these steps:

Setup: In the setUp() function, it deploys all necessary contracts (DSCEngine, DecentralizedStableCoin) and configures them with mock tokens and price feeds.

Individual Tests: It contains several test functions, each checking a specific aspect of the DSCEngine:

Testing that the constructor validates inputs correctly Testing price conversion functions Testing collateral deposit functionality Testing error conditions (like trying to deposit zero collateral) Modifiers: It uses a modifier called DepositCollateral to reuse the collateral deposit logic across multiple tests.

Important Logic Flows Token Price Conversion: The tests check if the contract correctly converts between USD values and token amounts. For example, if ETH is worth $2,000, then $200 should equal 0.1 ETH.

Collateral Validation: The tests verify that the system rejects invalid operations, like:

Depositing zero collateral Using tokens that aren't approved as collateral Account Information: After depositing collateral, the tests check if the system correctly tracks:

How much collateral a user has deposited How much stablecoin they've minted The core idea being tested is that users can deposit valuable tokens (like ETH) as collateral, and in return, they can mint a stablecoin whose value remains steady at $1. The tests ensure that all the calculations and validations in this process work correctly.

This testing approach helps developers catch bugs before deploying the contract to a real blockchain, where mistakes could cost real money.

About

This is a stable coin

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors