Fatskills
Practice. Master. Repeat.
Study Guide: Blockchain and Web3 Development: DeFi and Tokenomics Stablecoins FiatCollateralized OverCollateralized Algorithmic
Source: https://www.fatskills.com/cryptocurrency-bitcoin-blockchain-and-more/chapter/blockchain-and-web3-development-blockchain-and-web3-development-defi-and-tokenomics-stablecoins-fiatcollateralized-overcollateralized-algorithmic

Blockchain and Web3 Development: DeFi and Tokenomics Stablecoins FiatCollateralized OverCollateralized Algorithmic

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~5 min read

What This Is

A stablecoin is a cryptocurrency that aims to keep its price pegged to a stable asset—most often a fiat currency like USD. By anchoring value, stablecoins let users enjoy the speed and programmability of crypto while avoiding the wild price swings of Bitcoin or ETH. In a decentralized finance (DeFi) stack, a stablecoin is the “cash” you use to swap on Uniswap, lend on Aave, or pay for gas on a Layer‑2 roll‑up.


Key Terms & Code Snippets

  • Fiat‑Collateralized (FC) Stablecoin: A token backed 1:1 by real‑world dollars held in a bank. Example: USDC.
  • Over‑Collateralized (OC) Stablecoin: A token backed by crypto assets worth more than the issued supply (e.g., DAI). The extra collateral cushions price drops.
  • Algorithmic Stablecoin: No collateral; price is kept stable by supply‑adjustment algorithms (e.g., FRAX’s hybrid model).
  • ERC‑20 decimals: Stablecoins usually use 6 decimals (USDC) or 18 decimals (DAI).
    solidity uint8 public constant decimals = 6; // USDC‑style
  • mint / burn functions: Core of any stablecoin contract; only an authorized “minter” can create or destroy tokens.
    solidity function mint(address to, uint256 amount) external onlyMinter {
    _mint(to, amount); } function burn(address from, uint256 amount) external onlyMinter {
    _burn(from, amount); }
  • priceOracle interface: OC & algorithmic coins need an on‑chain price feed (Chainlink, Band, or a custom TWAP).
    solidity interface IPriceOracle {
    function latestAnswer() external view returns (int256); }
  • collateralRatio (CR): For OC coins, CR = totalCollateralValue / totalSupply. Must stay above a safety threshold (e.g., 150%).
  • liquidation function: When CR falls below the minimum, a keeper can seize collateral and mint new stablecoins to restore balance.
    solidity function liquidate(address vault) external {
    // 1️⃣ check CR, 2️⃣ seize collateral, 3️⃣ mint stablecoins to liquidator }
  • rebase (Algorithmic): Adjusts every holder’s balance proportionally to bring market price back to the peg.
    solidity function rebase(int256 supplyDelta) external onlyRebaser {
    totalSupply = totalSupply + supplyDelta;
    // balances are scaled via a global index }
  • ERC‑4626 Vault: A standard way to wrap collateral (e.g., ETH) into a yield‑bearing token that can be used as OC backing.
  • EIP‑2612 permit: Allows gas‑less approvals for stablecoins, useful for DeFi routers.
    solidity function permit(
    address owner,
    address spender,
    uint256 value,
    uint256 deadline,
    uint8 v,
    bytes32 r,
    bytes32 s ) external;


Step‑by‑Step / Process Flow

  1. Design the token contract – Fork OpenZeppelin’s ERC20PresetMinterPauser, add a priceOracle variable and a mint guard that checks the peg (for OC/algorithmic).
  2. Write the collateral manager – Create a separate contract that tracks deposits (e.g., ETH, WBTC) and computes collateralRatio.
  3. Compile & test – Use Hardhat:
    bash
    npx hardhat compile
    npx hardhat test test/Stablecoin.t.sol
  4. Deploy to a testnet – Deploy both contracts to Goerli via Infura:
    js
    const Stablecoin = await ethers.getContractFactory("MyStablecoin");
    const stable = await Stablecoin.deploy(oracle.address);
    await stable.deployed();
  5. Integrate with a front‑end – Use Ethers.js to call mint when a user deposits collateral:
    js
    await stable.mint(userAddress, amount);
  6. Add a keeper bot – Write a small Node script that watches the price feed; if CR < 150%, call liquidate(vaultId). Deploy the bot on a serverless platform (AWS Lambda, Cloudflare Workers).

Common Mistakes

  • Mistake: Using tx.origin for access control (e.g., only the “owner” can mint).
    Correction: Use msg.sender with onlyOwner modifiers; tx.origin can be spoofed through a malicious contract, breaking the peg security.

  • Mistake: Hard‑coding the oracle address and forgetting to update it after a feed upgrade.
    Correction: Store the oracle in a mutable address public priceOracle; and provide an onlyOwner setOracle() function. This keeps the system flexible and audit‑friendly.

  • Mistake: Forgetting to round down when converting from 18‑decimal collateral to a 6‑decimal stablecoin, leading to “dust” loss.
    Correction: Use SafeMath (or Solidity 0.8+ built‑in checks) and explicit scaling: amount * 1e6 / 1e18.

  • Mistake: Allowing unlimited mint calls from any address (no role restriction).
    Correction: Leverage OpenZeppelin’s AccessControl and grant the MINTER_ROLE only to the collateral manager contract.

  • Mistake: Ignoring slippage when performing a rebase; the market price may overshoot the peg.
    Correction: Implement a bounded rebase (e.g., max ±5 % per epoch) and combine with a secondary stabilization pool.


Blockchain Developer Interview / Practical Insights

  1. “Explain the difference between fiat‑collateralized and over‑collateralized stablecoins.”

    Interviewers look for understanding of risk profiles: FC relies on trust in custodial banks; OC relies on smart‑contract‑enforced collateral ratios and liquidation mechanisms.

  2. “How would you protect a liquidation function from front‑running?”

    Expect answers like using a commit‑reveal scheme, adding a minimum profit margin, or leveraging EIP‑1559 base‑fee to make the transaction cost‑predictable.

  3. “What are the gas‑implications of an ERC‑4626 vault vs a naïve ERC‑20 mint/burn?”

    Auditors check that vaults batch accounting (single totalAssets update) to save gas, whereas per‑user mint/burn can be expensive at scale.

  4. “Why might an algorithmic stablecoin still need a collateral buffer?”

    Because pure supply‑adjustment can’t react instantly to market shocks; a small collateral reserve (e.g., FRAX’s 10 % USDC) provides a safety net and is a red flag for auditors.


Quick Check Questions

  1. Scenario: A DApp calls stablecoin.transferFrom(msg.sender, address(this), amount) without first approving the contract.
    Answer: The transaction will revert with “ERC20: transfer amount exceeds allowance.” Users must approve the contract (or use permit) before transferFrom.

  2. Scenario: Your OC stablecoin’s collateralRatio drops to 120 % while the minimum is 150 %. A keeper calls liquidate() but the transaction runs out of gas.
    Answer: The liquidation fails, leaving the system under‑collateralized; design the function to be gas‑efficient (use unchecked loops, limit batch size) and consider a fallback emergency shutdown.

  3. Scenario: You see a contract that mints new stablecoins whenever priceOracle.latestAnswer() < 0.99 * peg.
    Answer: This is a price‑oracle‑driven mint that can be gamed if the oracle is compromised; a robust design adds a time‑weighted average price (TWAP) and a governance delay before minting.


Last‑Minute Cram Sheet (10 one‑liners)

  1. ⚠️ Never trust tx.origin for auth – use msg.sender + role‑based access.
  2. FC stablecoins = custodial; OC = on‑chain collateral; Algo = supply‑only.
  3. Typical decimals: USDC = 6, DAI = 18, FRAX = 18.
  4. Collateral ratio = (oracle price × collateral amount) / stablecoin supply.
  5. EIP‑2612 permit saves a gas‑heavy approve transaction.
  6. Rebase contracts must update a global index to avoid looping over all balances.
  7. Chainlink price feeds have 8 decimals; scale to 18 with * 1e10.
  8. Liquidation bots should use call with a gas stipend (.call{gas: 100_000}) to avoid OOG.
  9. ERC‑4626 vaults are the “yield‑bearing ERC‑20” standard – use them for OC backing.
  10. Most attacks on stablecoins come from oracle manipulation, flash‑loan price spikes, or re‑entrancy in liquidation.


ADVERTISEMENT