A Research-Grade Environment for Stress-Testing DeFi Protocols with Solidity Mini-Systems + Python Simulation Engine + Streamlit Visualization
DeFi-Risk-Scenario-Lab is a full-stack analytical environment designed to simulate, visualize, and explain the behavior of DeFi protocols under market stress.
It integrates:
- Miniature Solidity protocol implementations (AMM, lending pool, yield system)
- A Python simulation backend modeling price crashes, liquidity dynamics, and liquidation cascades
- A Streamlit dashboard for real-time scenario exploration and visualization
- Quantitative risk metrics: pool value, protocol equity, position health, and cascading effects
This lab is intentionally designed for:
- DeFi protocol architects exploring parameter sensitivities
- Smart-contract security researchers analyzing failure modes
- Financial engineers performing scenario & stress testing
- Data scientists modeling systemic risks in on-chain economies
It serves as a sandbox for understanding risk propagation, not as a production DeFi system.
DeFi protocols, despite being autonomous and deterministic, remain vulnerable to:
- market shocks
- liquidity fragmentation
- leverage spirals
- oracle-dependent liquidation loops
- fee misconfigurations
- AMM inventory risk
- cross-protocol contagion
Traditional DeFi audits focus on code correctness, not economic correctness. This lab fills that gap by simulating:
Examples this lab answers:
- How fast does an AMM’s value deteriorate during a crash?
- At what volatility levels does a lending pool enter liquidation spiral?
- What parameter combinations (LTV, fees) create instability?
- How do swap flows interact with falling collateral prices?
The two figures you generated are the first outputs of this investigation.
DeFi-Risk-Scenario-Lab/
│
├── contracts/ # Solidity mini-protocols
│ ├── amm/
│ ├── lending/
│ └── yield/
│
├── simulation/ # Python simulation engine
│ ├── scenarios.py
│ ├── protocols.py
│ ├── engines.py
│ ├── metrics.py
│ └── state.py
│
├── app/
│ └── streamlit_app.py # UI Dashboard
│
├── tests/
└── README.md
The architecture is designed to be modular, interpretable, and extensible.
Lightweight smart contracts representing essential mechanics:
-
SimpleAMM.sol
- Uniswap v2-style x*y=k invariant
- Configurable swap fee
- No LP token complexity (for clarity)
-
SimpleLendingPool.sol
- Collateral-value-based health factor
- LTV (loan-to-value) limits
- Liquidation threshold
- Deterministic liquidation logic
-
RewardFarm.sol
- Linear emission
- Useful for modeling yield contraction during crashes
These contracts provide the ground-truth rule sets that the Python engine mirrors mathematically.
The simulation engine consists of:
Creates price/shock paths:
- Linear crashes
- Multi-phase dumps
- Liquidity drains (future feature)
- Volatility spikes (planned)
Re-implement Solidity logic in Python for speed:
- AMM swaps adjust reserve state each timestep
- Lending pool health evaluated continuously
- Liquidations executed deterministically
Computes:
- pool value = reserve₀ + reserve₁ × price
- protocol equity
- liquidation count
- drawdown (planned)
- impermanent loss (planned)
Every timestep records structured simulation state:
MarketState
AMMState
LendingState
Metrics
The UI lets you:
- configure crash depth, start, horizon
- tweak LTV, liquidation threshold
- adjust AMM fee behavior
- execute simulation
- visualize how AMM value and price evolve
This turns analytical modeling into interactive experimentation.
This chart shows:
- A stable price plateau during the “calm” phase
- A linear downward crash starting at timestep 10
- A final stabilization at a lower price
This is a controlled price crash model, ideal for studying deterministic protocol response. It isolates the following variables:
- Price levels
- Rate of decline
- Crash starting point
- Shock duration
This makes the scenario analytically clean and easy to interpret.
This chart shows:
- A stable pool value until the crash begins
- A gradual decline in AMM value as reserves rebalance
- The characteristic impermanent loss curve induced by volatile price movement
This figure demonstrates several canonical AMM behaviors:
As price melts, the pool becomes overexposed to the losing asset.
Even if price later stabilizes, the AMM’s reserve ratio has shifted such that:
Pool Value < HODL Value
Your simulation applies a constant trade size each timestep, modeling:
- arbitrage
- passive flow
- user swap pressure
This increases divergence between AMM and baseline value.
Because the AMM is a function of price, a predictable decline appears:
V_pool(t) = reserve₀(t) + reserve₁(t) × price(t)
Your simulation correctly exhibits:
- concave decay
- linear-like reserve depletion
- no sudden discontinuities (as expected from AMM math)
The simulation is driven by two classical DeFi equations:
x * y = k
When a swap comes in:
amount_in_with_fee = amount_in × (1 – fee)
new_reserve_in = reserve_in + amount_in_with_fee
new_reserve_out = k / new_reserve_in
amount_out = reserve_out - new_reserve_out
This determines:
- price impact
- reserve composition
- pool value trajectory
health = collateral_value / debt
Liquidation condition:
health < liquidation_threshold
→ trigger liquidation
In this simulation:
- liquidations reduce collateral
- reduce debt
- increment liquidation counter
Even though your current chart doesn’t show liquidations yet, the system is ready for them.
pip install streamlitstreamlit run app/streamlit_app.pyDashboard appears at:
http://localhost:8501
This repo is structured for future expansion:
- GBM stochastic volatility
- Flash crashes
- Multi-asset contagion
- Leverage loops
- Cross-protocol interactions
- Oracle lag models
- Liquidation timeline
- Health factor trajectory
- Impermanent loss curve
- AMM arbitrage efficiency
This lab is meant for:
Testing parameter sensitivity before launching mainnet protocols.
Understanding economic attack surfaces.
Modeling deterministic stress behaviors.
Running reproducible simulations of on-chain risk.