Whitepaper (PDF): StableGuard-Whitepaper.pdf
StableGuard is a research‑grade, modular stablecoin system designed to demonstrate production‑style architecture, gas optimizations, and robust safety checks without requiring mainnet deployment. It includes an on‑chain price oracle with fallback logic, collateral accounting, liquidation via direct and Dutch auction flows, and optional arbitrage/repeg modules. The Repeg module continuously monitors deviation from the target price and applies bounded buy/sell pressure (optionally coordinating with arbitrage) under slippage and size guards to realign the peg; thresholds, cooldowns, and circuit‑breakers prevent oscillations and abuse. Dutch auctions complement direct liquidation by discovering a market‑clearing price with a decaying curve and commit‑reveal flow, reducing MEV and manipulation compared to fixed‑bonus burns while settling unsafe positions under stress.
Portfolio‑friendly: runs locally with mocks; no fork or deployment required. ETH is represented as
address(0)for native flows and is supported directly by the oracle in this repository.
- Overview
- Why It Matters
- At a Glance
- Non‑Production Scope
- Architecture
- Modules
- Functional Scope
- Key Flows
- Flows: Deep Dive
- Security & Safety
- Protocol Invariants
- Risk Model & Parameters
- Design Decisions
- Events & Telemetry
- Performance Notes
- Repository Structure
- Getting Started
- Running Tests
- Development Notes
- Limitations & Roadmap
- Threats & Mitigations
- Production Hardening Checklist
- Glossary
StableGuard is an ERC20‑based stable token backed by user collateral. The system focuses on:
- Gas‑efficient storage packing and assembly in critical paths.
- Clear access controls and rate limiting.
- Deterministic local testing with Chainlink‑style mocks.
- Extensible modules for liquidation (direct + Dutch auction), arbitrage, and repeg.
- Realistic architecture: mirrors production patterns (owner controls, timelock, module wiring) without external dependencies.
- Robust pricing: integrates an oracle with validated feeds, fallback prices, and decimal normalization.
- Safety by design: strict validation (addresses, ratios, thresholds), non‑reentrancy, and permissioned operations.
- Demonstrates advanced patterns: packed structs, low‑level assembly guards, and MEV‑aware auctions.
StableGuard: core ERC20 stable, orchestrates mint/burn and enforces ratios.PriceOracle: validated price feeds, fallback prices, 18‑dec normalization.CollateralManager: deposit/withdraw, per‑token LTV and thresholds.LiquidationManager: direct liquidation with security caps and validations.DutchAuctionManager: commit‑reveal auctions, descending price curve, MEV guards.RepegManager: bounded buy/sell pressure to re‑align peg with safeguards.ArbitrageManager: detects on‑chain price dislocations and coordinates execution.Timelock: delays sensitive owner actions; queue/execute/cancel lifecycle.
- Portfolio/demo focus: local mocks, no forks or deployments, no external infra.
- Missing mainnet hardening: governance/multisig, redundant oracles, audits, monitoring.
- Parameters are illustrative: tune thresholds, cooldowns, and limits per risk appetite.
- Use this as a learning/prototyping base, not a production deployment.
graph TD;
U[User] --> SG[StableGuard - ERC20 Stablecoin];
SG --> PO[PriceOracle];
SG --> CM[CollateralManager];
SG --> LM[LiquidationManager];
SG --> DAM[DutchAuctionManager];
SG --> RM[RepegManager];
SG --> TL[Timelock];
AM[ArbitrageManager] --> PO;
AM --> DEX[Uniswap V2 Router];
SG --> AM;
CM --> PO;
LM --> CM;
LM --> SG;
StableGuard: core token, orchestrates deposit/mint, burn/withdraw, calls liquidation/auction managers.PriceOracle: validated price retrieval via aggregator feeds, fallback prices, and decimal handling.CollateralManager: per‑user collateral tracking, ETH and ERC20 handling, controlled by StableGuard.LiquidationManager: checks safety, performs emergency/direct liquidation; notifies StableGuard.DutchAuctionManager: MEV‑aware auction flow to sell collateral against debt.ArbitrageManager: detects and executes arbitrage using DEX prices vs oracle.RepegManager: monitors deviation and manages repeg operations (configurable).Timelock: delayed execution for emergency operations.
- StableGuard (
src/StableGuard.sol) - PriceOracle (
src/PriceOracle.sol) - CollateralManager (
src/CollateralManager.sol) - LiquidationManager (
src/LiquidationManager.sol) - DutchAuctionManager (
src/DutchAuctionManager.sol) - ArbitrageManager (
src/ArbitrageManager.sol) - RepegManager (
src/RepegManager.sol) - Timelock (
src/Timelock.sol)
Interfaces live under src/interfaces. Tests use internal local mocks for deterministic behavior.
- Collateralized stablecoin with native ETH (
address(0)) and ERC20 support. - End-to-end mint/burn flows with oracle-backed USD valuation.
- Liquidation via direct and Dutch auction paths with safety checks.
- Repeg and arbitrage utilities to manage peg deviations.
- Timelock for delayed, owner-gated emergency operations.
- Rate limiting for user/global operations to resist abuse.
sequenceDiagram
participant U as User
participant SG as StableGuard
participant CM as CollateralManager
participant PO as PriceOracle
U->>SG: depositAndMint(token, depositAmount, mintAmount)
SG->>CM: deposit(user, token, amount)
CM->>PO: getTokenValueInUsd(token, amount)
PO-->>CM: usdValue
SG->>SG: update position (debt)
SG-->>U: mint stable tokens
sequenceDiagram
participant U as User
participant SG as StableGuard
participant CM as CollateralManager
U->>SG: burnAndWithdraw(token, burnAmount, withdrawAmount)
SG->>SG: burn stable tokens, reduce debt
SG->>CM: withdraw(user, token, amount)
CM-->>U: return ETH/ERC20
sequenceDiagram
participant SG as StableGuard
participant LM as LiquidationManager
participant DAM as DutchAuctionManager
SG->>LM: check safety / trigger direct liquidation
LM-->>SG: processDirectLiquidation callback updates debt
SG->>DAM: startDutchAuction(user, token, debtAmount)
DAM-->>SG: auction completion callback updates debt
- Arbitrage checks oracle vs DEX prices; executes when profitable within slippage/limit guards.
- RepegManager monitors deviation and triggers operations based on configured thresholds/cooldowns.
flowchart LR
A[Deposit/Mint Request] --> B{Token Supported?}
B -->|No| X[Revert: Unsupported token]
B -->|Yes| C[Get USD Value via PriceOracle]
C --> D[Compute New Debt]
D --> E[ratio = collateralValue * 10000 / debt]
E --> F{ratio >= minCollateralRatio?}
F -->|No| X[Revert: Insufficient collateral]
F -->|Yes| G[Update Position & Mint]
- Safety checks compare the collateralization ratio against
config.minCollateralRatio(basis points). - Liquidation safety compares against
config.liquidationThreshold.
sequenceDiagram
participant SG as StableGuard
participant PO as PriceOracle
SG->>PO: getTokenPrice(token)
PO->>PO: latestRoundData() from aggregator
PO-->>SG: 18-decimal USD price (or fallback)
- Validates aggregator data (round, answer, freshness) and decimals.
- Falls back to
fallbackPriceif Chainlink data invalid; emits telemetry events.
- Direct liquidation burns debt and transfers required collateral + bonus.
- Auction liquidation triggers
DutchAuctionManager.startDutchAuctionand settles via callbacks.
sequenceDiagram
participant O as Owner
participant TL as Timelock
O->>TL: queueTransaction(target, value, signature, data, executeTime)
TL-->>O: TransactionQueued(txHash)
O->>TL: executeTransaction(target, value, signature, data, executeTime)
TL-->>O: TransactionExecuted(txHash)
O->>TL: cancelTransaction(target, value, signature, data, executeTime)
TL-->>O: TransactionCancelled(txHash)
sequenceDiagram
participant C as Caller
participant AM as ArbitrageManager
participant PO as PriceOracle
participant DEX as Uniswap V2 Router
C->>AM: executeArbitrage()
opt Configuration checks
AM->>AM: enabled == true
AM->>AM: circuit breaker not tripped
AM->>AM: rate limit / cooldown enforced
end
AM->>PO: getTokenPrice(STABLE_TOKEN)
opt Price validation
PO-->>AM: latestRoundData + decimals
AM->>AM: validate freshness and decimals
AM->>AM: use fallback if invalid
end
AM->>DEX: getAmountsOut(path)
AM->>AM: compute profitBps vs minProfitBps
AM-->>C: emit ArbitrageOpportunityDetected(profitBps)
alt Profit ≥ minProfitBps
AM->>DEX: swap (respect maxTradeSize, maxSlippageBps)
AM-->>C: emit ArbitrageExecuted(profitBps)
AM->>AM: update success metrics
else Not profitable
AM-->>C: emit ArbitrageSkipped(insufficientProfit)
AM->>AM: update failure metrics (optional)
end
sequenceDiagram
participant C as Caller/Owner
participant RM as RepegManager
participant PO as PriceOracle
participant AM as ArbitrageManager
participant DEX as Uniswap V2 Router
C->>RM: checkAndTriggerRepeg()
RM->>PO: getTokenPrice(STABLE_TOKEN)
RM->>RM: isRepegNeeded() + parameters
RM->>AM: (optional) executeArbitrage()
RM->>DEX: apply buy/sell pressure
RM-->>C: emit RepegEvent / RepegOperationExecuted
sequenceDiagram
participant B as Bidder
participant DAM as DutchAuctionManager
B->>DAM: commitBid(commitHash, auctionId)
DAM-->>B: BidCommitted
B->>DAM: revealAndBid(commitId, auctionId, maxPrice, nonce)
DAM->>DAM: verify commit & MEV/flashloan guards
DAM-->>B: BidRevealed
DAM->>DAM: _executeBid (price, payment, transfer)
DAM-->>B: AuctionEvent(Bid)
- Access control:
onlyOwner,onlyStableGuard,onlyAuthand module‑specific modifiers. - Validation: non‑zero addresses, duplicate module prevention, bounds for ratios and penalties.
- Reentrancy protection:
ReentrancyGuardused across core modules. - Rate limiting: user/global operation caps, cooldowns, and windows in StableGuard.
- Timelock: delayed execution for sensitive owner actions.
- Oracle safety: freshness checks, deviation limits, fallback prices, validated feeds.
- Owner‑gated admin,
onlyStableGuardfor core flows, and module‑specific auth ensure least privilege.
- Strict bounds for ratios/penalties, non‑zero addresses, duplicate‑module prevention, and per‑token sanity checks.
- Non‑reentrancy across state‑mutating paths; ETH/ERC20 flows guarded to prevent reentrancy vectors.
- Per‑user and global windows, cooldowns, and burst caps constrain throughput and mitigate spam/MEV griefing.
- Sensitive operations require delay and explicit queue/execute; cancellations enforce safety under uncertainty.
- Max age/heartbeat, deviation limits, validated fallbacks, and decimal normalization reduce price‑path risks.
- Commit‑reveal in auctions, flashloan checks, block caps, and price‑impact guards reduce manipulation and sniping.
-
StableGuard
- Access:
onlyOwner,validModules; non‑reentrancy on core flows. - Safety: collateral ratio checks (
minCollateralRatio,liquidationThreshold). - Rate limiting: user/global windows, cooldowns, burst caps; events for exceed/updates.
- Admin: atomic
updateModuleswith duplicate address prevention; config bounds. - ETH handling: receive ETH for forwards; forwards tokens/ETH after withdrawals.
- Access:
-
CollateralManager
- Access:
onlyAuth(owner or guard),onlyStableGuardfor deposit/withdraw. - Validation:
validAmount,validUser, oracle token support. - Reentrancy: guarded; ETH requires
msg.value == amount; ERC20 via transfers. - Admin:
addCollateralTypebounds;emergencyWithdrawowner‑only; restrictedreceive().
- Access:
-
PriceOracle
- Access: owner‑only configuration;
validTokengating for queries. - Freshness: max age/heartbeat/grace; strict/normal modes; deviation checks.
- Fallbacks: validated fallback price; events on fallback/use/updates.
- Decimals: enforce
0 < decimals <= 18; normalize to 18 decimals.
- Access: owner‑only configuration;
-
LiquidationManager
- Access:
onlyOwneradmin;onlyGuardoperational;setStableGuardowner‑only. - MEV protections: rate limiting, block caps, flashloan detection, price deviation validation.
- Safety: collateral checks, optimal token selection, threshold comparisons.
- Access:
-
DutchAuctionManager
- Access:
onlyOwner,onlyStableGuard; per‑auction validation. - MEV protections: commit‑reveal, minimum bid delay, flashloan checks, price impact caps.
- State: expiry handling, batch cleaning with incentives; non‑reentrancy on bid flows.
- Access:
-
RepegManager
- Access:
onlyAuthorized(owner or StableGuard); emergency pause; circuit breaker. - Liquidity: provider tracking, reserves, minimum liquidity checks.
- Safety: config validation (thresholds, cooldowns, windows), cached prices, adaptive thresholds.
- Access:
-
Timelock
- Access:
onlyOwner; delay bounds; queue/execute/cancel lifecycle. - Safety: grace period enforcement; non‑reentrancy on execute.
- Access:
- Only the configured
StableGuardcan callCollateralManager.deposit/withdraw. - PriceOracle returns 18-decimal USD prices; decimals > 0 and ≤ 18.
configureToken:priceFeed != address(0),fallbackPrice > 0, owner-only.- ETH is supported as
address(0)when configured in PriceOracle; CollateralManager enforcesmsg.valuefor ETH. - Collateralization ratio must always satisfy
minCollateralRatiofor mint operations. - Liquidation triggers require position unsafe against
liquidationThreshold.
-
StableGuard
PackedConfig(basis points):minCollateralRatio(e.g., 15000 = 150%).liquidationThreshold(e.g., 12000 = 120%).emergencyThreshold.
-
CollateralManager
CollateralType(per-token):ltv,liquidationThreshold,liquidationPenalty(basis points).decimals,priceFeed,fallbackPrice.
-
Safety checks:
ratio = collateralValue * 10000 / debt.- Safe if
ratio >= minCollateralRatio. - Liquidation if
collateralValue * 10000 < debt * liquidationThreshold.
-
LiquidationManager
PackedConfig:minRatio,liqThreshold,bonus(basis points) andstableGuard.- MEV & security:
MIN_LIQUIDATION_DELAY,MAX_LIQUIDATIONS_PER_BLOCK,FLASHLOAN_PROTECTION_BLOCKS,MAX_PRICE_DEVIATION.
-
DutchAuctionManager
Config:duration,minPriceFactor,liquidationBonus.- MEV & security:
COMMIT_DURATION,REVEAL_DURATION,MIN_BID_DELAY,MAX_PRICE_IMPACT,FLASHLOAN_PROTECTION_BLOCKS.
-
Collateralization:
minCollateralRatio: ≥ 150% recommended for conservative setups.liquidationThreshold: 120%–140% depending on asset volatility.liquidationPenalty: 5%–20% to incentivize liquidators without excess.
-
Oracle:
maxAge/heartbeat: 30s–5m for liquid assets; stricter under stress.maxDeviation: 1%–3% vs previous price; tighter for stable assets.
-
Repeg:
deviationThreshold: 0.1%–0.5%;cooldown: minutes; size caps per window.slippageTolerance: 0.5%–1% depending on DEX liquidity.
-
Rate limits:
- Per‑user/global caps tuned to expected throughput; enforce cooldowns.
-
Auctions:
commit/revealdurations: tens of seconds to minutes;minBidDelay≥ 1 block.
-
Timelock:
- Delay: hours to days; longer for governance‑level operations.
-
RepegManager
RepegConfig:targetPrice,deviationThreshold,repegCooldown,arbitrageWindow,incentiveRate,maxRepegPerDay,enabled.- Limits:
MAX_DEVIATION,MAX_INCENTIVE.
-
ArbitrageManager
ArbitrageConfig:maxTradeSize,minProfitBps,maxSlippageBps,enabled.- Security: circuit breaker thresholds (
FAILURE_THRESHOLD,RECOVERY_TIME), gas limits (MAX_EXTERNAL_CALL_GAS,MAX_SWAP_GAS), rate limiting (OPERATION_COOLDOWN).
-
Timelock:
MINIMUM_DELAY,MAXIMUM_DELAY,GRACE_PERIODfor delayed execution.
- ETH as
address(0): In this repository, native ETH is represented byConstants.ETH_TOKEN = address(0). ThePriceOracleexplicitly supportsaddress(0)for ETH with a valid feed and fallback price.- Owner‑only configuration;
priceFeed != address(0);fallbackPrice > 0;decimals > 0. - Tests reflect this behavior (configuring
address(0)is valid; invalid feeds/prices still revert).
- Owner‑only configuration;
- Modular wiring: StableGuard holds module references and updates them atomically with duplication checks.
- Deterministic testing: Chainlink feed mocked via
MockAggregatorV3; no external RPC or fork needed.
-
Peg monitoring: detect deviation → emit opportunity event → (optional) arbitrage call → repeg execution.
-
Liquidation: unsafe position detection → direct or auction path → execution and settlement events.
-
Auctions: start → bids committed → bids revealed → winning execution → finalization.
-
Safety: rate limit exceeded/updated, oracle fallback used, config updates tracked for dashboards.
-
PriceOracle:
TokenConfigured,PriceUpdated,FallbackPriceUsed,FallbackPriceUpdated,FreshnessConfigUpdated,PriceDeviationDetected,StaleDataDetected. -
StableGuard:
PositionUpdated,PositionLiquidated,LiquidationTriggered,ModulesUpdated,RateLimitExceeded,RateLimitUpdated,GlobalRateLimitExceeded,RateLimitingPauseChanged,RepegMonitoring,RepegConfigUpdated,RepegLiquidityOperation. -
CollateralManager:
Deposit,Withdraw. -
LiquidationManager:
LiquidationEvent,MEVAttemptDetected,FlashloanDetected,RateLimitExceeded. -
DutchAuctionManager:
AuctionEvent,BidCommitted,BidRevealed,MEVAttemptDetected,FlashloanDetected. -
ArbitrageManager:
ArbitrageExecuted,ArbitrageOpportunityDetected,ConfigUpdated. -
RepegManager:
RepegEvent,ArbitrageExecuted,RepegOperationExecuted,BuyPressureExecuted,SellPressureExecuted. -
Timelock:
TransactionQueued,TransactionExecuted,TransactionCancelled,DelayChanged.
- Packed storage structs (
UserPosition,PackedConfig,PackedModules). - Inline assembly for critical updates and ratio calculations.
- Batched operations:
batchDepositAndMint,batchBurnAndWithdraw. - Caching of collateral values with short-lived cache window.
- Gas-aware external call patterns and minimized storage writes.
backend/
├─ src/
│ ├─ StableGuard.sol
│ ├─ PriceOracle.sol
│ ├─ CollateralManager.sol
│ ├─ LiquidationManager.sol
│ ├─ DutchAuctionManager.sol
│ ├─ ArbitrageManager.sol
│ ├─ RepegManager.sol
│ ├─ Timelock.sol
│ ├─ interfaces/
│ │ ├─ IPriceOracle.sol, ILiquidationManager.sol, ...
├─ test/
│ ├─ PriceOracle.t.sol
│ ├─ StableGuard.t.sol
│ ├─ CollateralManager.t.sol
│ ├─ LiquidationManager.t.sol
│ ├─ DutchAuctionManager.t.sol
│ ├─ ArbitrageManager.t.sol
│ ├─ RepegManager.t.sol
│ └─ Timelock.t.sol
├─ foundry.toml
├─ foundry.lock
├─ .gitignore
├─ LICENSE
├─ StableGuard-Whitepaper.pdf
└─ README.md
- Foundry toolchain (
forge,cast): https://book.getfoundry.sh/getting-started/installation - Git
Dependencies are managed with Foundry and installed into lib/ when developing locally:
forge-std(Foundry standard library)openzeppelin-contracts
Install after cloning:
forge install foundry-rs/forge-std
forge install OpenZeppelin/openzeppelin-contractsNotes:
- Versions are pinned in
foundry.lock;forge installrespects these pins. lib/is not committed; collaborators should runforge installafter cloning.
- Clone the repository.
- Build the contracts:
forge build# 1) Initialize Git (required by forge install)
git init
# 2) Install dependencies into lib/
forge install foundry-rs/forge-std
forge install OpenZeppelin/openzeppelin-contracts
# 3) Build and run tests
forge build
forge test -vv- Recommended: latest stable Foundry; if versions diverge, update to current release.
- Format code with
forge fmtto keep consistent style. - If artifacts look stale, run
forge cleanbefore building/testing.
- Error:
fatal: not a git repositorywhen runningforge install.- Fix: run
git initat the project root and retryforge install.
- Fix: run
- Stale artifacts or cache.
- Fix:
forge clean && forge build.
- Fix:
- Remappings not found.
- Fix: check
foundry.toml(uses@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/) and runforge remappingsto inspect.
- Fix: check
- Forge-only flow (no submodules) but
.gitmodulesshows up.- Fix: remove
.gitmodulesand ensurelib/is listed in.gitignore.
- Fix: remove
- Run all tests with verbose output:
forge test -vv- Run a single test file:
forge test -vv -m PriceOracle
forge test -vv --match-path test/StableGuard.t.sol- Focus a specific test case:
forge test -vvv -m testInvalidConfigurationsNotes:
- Tests include internal mocks in test files for deterministic pricing.
- No environment variables or RPC endpoints are required.
- Optional: include gas report output
forge test --gas-report- Unit tests cover core modules (StableGuard, CollateralManager, PriceOracle, Liquidation, DutchAuction, Repeg, Timelock).
- Deterministic mocks provide stable oracle and token behaviors; no RPC/env required.
- Invariants focus on collateralization, access control, timelock semantics, and auction correctness.
- Gas report highlights packed storage, batched operations, and cache usage in hot paths.
- Owner & initialization: setters like
setStableGuardon managers are owner‑gated; StableGuard updates module references viaupdateModules. - Ratios & thresholds: validated to sensible bounds (e.g., min collateral ratio ≥ 110%).
- Decimals & price normalization: oracle converts aggregator answers to 18‑decimals USD values.
- ETH vs ERC20: CollateralManager enforces
msg.valuefor ETH and transfers for ERC20.
- Prices normalized to 18 decimals (USD) for arithmetic consistency across tokens.
- ETH is represented as
address(0)in flows where native value is supported. - Rounding follows conservative bias in ratio checks to avoid over‑minting.
- Portfolio focus: local mocks only; no deployment, fork, or live feeds.
- Oracle scope: designed for demo; extend with more feeds and resilience if taken to production.
- Auction realism: Dutch auction parameters are simplified for clarity.
- Potential future work:
- Formal verification / property tests.
- Advanced MEV protections.
- Multi‑asset risk parameters and dynamic LTV.
- Price manipulation (oracle/DEX): mitigated via freshness/deviation checks, validated fallbacks.
- MEV/sniping in auctions: commit‑reveal, bid delays, price‑impact caps.
- Reentrancy/unauthorized access: guards on state paths, strict
only*modifiers. - Rate‑based DoS: per‑user/global limits, cooldowns, and windows.
- Configuration hazards: timelocked admin, bounds validation, circuit breakers.
- Governance: multisig owner, audited timelock with enforced delays.
- Oracles: redundant feeds, cross‑validation, failure modes tested.
- Parameters: dynamic limits based on volatility; on‑chain circuit breakers.
- Monitoring: off‑chain telemetry, alerts for deviation, rate‑limit hits, auction states.
- Testing: extended property/invariant suites, fuzzing, and formal verification where applicable.
- Peg: target price the stable aims to maintain.
- Slippage: execution price movement due to trade size/liquidity.
- Circuit breaker: emergency pause or threshold‑based halt.
- Commit‑reveal: two‑phase bidding to hide intent and reduce MEV.
- MEV: extractable value from transaction ordering/manipulation.
- LTV: loan‑to‑value; collateralization ratio in basis points.
- Heartbeat: expected max age for a price update in the oracle.
- Fallback price: validated backup when primary feed is unavailable.