File size: 13,269 Bytes
6993919 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | """
Tests for DEXPoolManipulationAnalyzer.
"""
import pytest
from app.dex_pool_manipulation_analyzer import (
DEXPoolManipulationAnalyzer,
RiskCategory,
RiskSignal,
format_risk_report,
is_valid_address,
)
@pytest.fixture
def analyzer():
return DEXPoolManipulationAnalyzer(chain="ethereum", dex="uniswap_v3")
@pytest.fixture
def sample_pool_metadata():
return {
"chain": "ethereum",
"dex": "uniswap_v3",
"version": "v3",
"token0": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"token1": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
"token0_symbol": "WETH",
"token1_symbol": "USDC",
"fee_tier": 500, # 0.05%
"tick_spacing": 10,
"total_liquidity_usd": 5000000.0,
"owner": "0x1234567890123456789012345678901234567890",
"created_at": 1700000000,
}
@pytest.fixture
def sample_positions():
return [
{
"owner": "0x1111...1111",
"tick_lower": -100,
"tick_upper": 100,
"liquidity": 500_000,
"usd_value": 1_000_000,
},
{
"owner": "0x2222...2222",
"tick_lower": -50,
"tick_upper": 50,
"liquidity": 300_000,
"usd_value": 600_000,
},
{
"owner": "0x3333...3333",
"tick_lower": -200,
"tick_upper": 200,
"liquidity": 100_000,
"usd_value": 200_000,
},
{
"owner": "0x4444...4444",
"tick_lower": -150,
"tick_upper": 150,
"liquidity": 50_000,
"usd_value": 100_000,
},
{
"owner": "0x5555...5555",
"tick_lower": -80,
"tick_upper": 80,
"liquidity": 30_000,
"usd_value": 60_000,
},
{
"owner": "0x6666...6666",
"tick_lower": -300,
"tick_upper": 300,
"liquidity": 20_000,
"usd_value": 40_000,
},
]
@pytest.fixture
def concentrated_positions():
"""Positions where top 1 owner controls most liquidity."""
return [
{
"owner": "0xBEEF...BEEF",
"tick_lower": -60,
"tick_upper": 60,
"liquidity": 900_000,
"usd_value": 1_800_000,
},
{
"owner": "0xBEEF...BEEF",
"tick_lower": -40,
"tick_upper": 40,
"liquidity": 50_000,
"usd_value": 100_000,
},
{
"owner": "0xCAFE...CAFE",
"tick_lower": -100,
"tick_upper": 100,
"liquidity": 30_000,
"usd_value": 60_000,
},
{
"owner": "0xDEAD...DEAD",
"tick_lower": -200,
"tick_upper": 200,
"liquidity": 20_000,
"usd_value": 40_000,
},
]
@pytest.fixture
def sandwich_swaps():
"""Simulated sandwich attack pattern."""
return [
{
"tx_hash": "0xaaa",
"block": 100,
"timestamp": 1000,
"amount_in": 5,
"amount_out": 4950,
"price_before": 1000.0,
"price_after": 1005.0,
},
{
"tx_hash": "0xbbb",
"block": 100,
"timestamp": 1001,
"amount_in": 50,
"amount_out": 49500,
"price_before": 1005.0,
"price_after": 1001.0,
},
{
"tx_hash": "0xccc",
"block": 100,
"timestamp": 1002,
"amount_in": 5,
"amount_out": 4980,
"price_before": 1001.0,
"price_after": 1000.5,
},
{
"tx_hash": "0xddd",
"block": 200,
"timestamp": 2000,
"amount_in": 10,
"amount_out": 10000,
"price_before": 1050.0,
"price_after": 1055.0,
},
{
"tx_hash": "0xeee",
"block": 200,
"timestamp": 2001,
"amount_in": 8,
"amount_out": 7950,
"price_before": 1055.0,
"price_after": 1051.0,
},
]
class TestAddressValidation:
def test_valid_evm_address(self):
assert is_valid_address("0x1234567890abcdef1234567890abcdef12345678")
def test_valid_solana_address(self):
assert is_valid_address("7e8qUqNYBg4QvfVj5H4GqYBKF9HbDQ4XQcBJnvcMqrZN")
def test_invalid_address(self):
assert not is_valid_address("not_an_address")
assert not is_valid_address("")
assert not is_valid_address("0xshort")
class TestPoolConfig:
def test_build_from_metadata(self, analyzer, sample_pool_metadata):
pool = analyzer._build_pool_config("0xpool...pool", sample_pool_metadata)
assert pool.address == "0xpool...pool"
assert pool.chain == "ethereum"
assert pool.dex == "uniswap_v3"
assert pool.fee_tier == 500
assert pool.total_liquidity_usd == 5_000_000.0
def test_defaults_for_empty_metadata(self, analyzer):
pool = analyzer._build_pool_config("0xpool...pool", {})
assert pool.chain == "ethereum"
assert pool.total_liquidity_usd == 0.0
class TestConcentrationAnalysis:
def test_no_positions(self, analyzer, sample_pool_metadata):
pool = analyzer._build_pool_config("0xpool", sample_pool_metadata)
signal, pct = analyzer._analyze_concentration([], pool)
assert signal is None
assert pct == 0.0
def test_well_distributed(self, analyzer, sample_pool_metadata, sample_positions):
pool = analyzer._build_pool_config("0xpool", sample_pool_metadata)
parsed = analyzer._parse_positions(sample_positions)
signal, _pct = analyzer._analyze_concentration(parsed, pool)
# Top 5: 500+300+100+50+30 = 980k out of 1000k = 98% — should flag
assert signal is not None
assert signal.category == RiskCategory.LIQUIDITY_CONCENTRATION
def test_concentrated_ownership(self, analyzer, sample_pool_metadata, concentrated_positions):
pool = analyzer._build_pool_config("0xpool", sample_pool_metadata)
parsed = analyzer._parse_positions(concentrated_positions)
signal, pct = analyzer._analyze_concentration(parsed, pool)
assert signal is not None
assert pct > 90 # Single owner controls >90%
class TestSandwichDetection:
def test_detect_sandwich(self, analyzer, sample_pool_metadata, sandwich_swaps):
pool = analyzer._build_pool_config("0xpool", sample_pool_metadata)
parsed = analyzer._parse_swaps(sandwich_swaps)
signal, profit = analyzer._analyze_sandwich_vulnerability(parsed, pool)
assert signal is not None
assert signal.category == RiskCategory.SANDWICH_VULNERABILITY
assert profit >= 0
def test_no_swaps_no_signal(self, analyzer, sample_pool_metadata):
pool = analyzer._build_pool_config("0xpool", sample_pool_metadata)
signal, profit = analyzer._analyze_sandwich_vulnerability([], pool)
assert signal is None
assert profit == 0.0
class TestPriceManipulation:
def test_high_impact_detected(self, analyzer, sample_pool_metadata):
pool = analyzer._build_pool_config("0xpool", sample_pool_metadata)
swaps = [
{
"tx_hash": "0xa",
"block": 1,
"timestamp": 1000,
"amount_in": 100,
"amount_out": 50,
"price_before": 100.0,
"price_after": 110.0,
},
{
"tx_hash": "0xb",
"block": 2,
"timestamp": 1001,
"amount_in": 50,
"amount_out": 20,
"price_before": 110.0,
"price_after": 115.0,
},
]
parsed = analyzer._parse_swaps(swaps)
signal = analyzer._analyze_price_manipulation(parsed, pool)
assert signal is not None
assert signal.category == RiskCategory.PRICE_MANIPULATION
def test_low_impact_no_signal(self, analyzer, sample_pool_metadata):
pool = analyzer._build_pool_config("0xpool", sample_pool_metadata)
swaps = [
{
"tx_hash": "0xa",
"block": 1,
"timestamp": 1000,
"amount_in": 1,
"amount_out": 999,
"price_before": 1000.0,
"price_after": 1000.1,
},
]
parsed = analyzer._parse_swaps(swaps)
signal = analyzer._analyze_price_manipulation(parsed, pool)
assert signal is None
class TestRiskScoring:
def test_no_signals_zero_score(self, analyzer):
score = analyzer._calculate_risk_score([])
assert score == 0.0
def test_high_severity_signals(self, analyzer):
signals = [
RiskSignal(
category=RiskCategory.LIQUIDITY_CONCENTRATION,
severity=0.8,
description="High concentration",
),
RiskSignal(
category=RiskCategory.SANDWICH_VULNERABILITY,
severity=0.8,
description="High sandwich risk",
),
RiskSignal(category=RiskCategory.FAKE_LIQUIDITY, severity=0.7, description="Fake liquidity"),
]
score = analyzer._calculate_risk_score(signals)
assert 0 < score <= 100
assert score > 30 # Should be significant
class TestPriceImpact:
def test_impact_increases_with_amount(self, analyzer):
impact_small = analyzer._simulate_price_impact(1, 1_000_000)
impact_large = analyzer._simulate_price_impact(100, 1_000_000)
assert impact_large > impact_small
assert impact_small < 1.0 # 1 ETH in $1M pool should be small
def test_impact_capped(self, analyzer):
impact = analyzer._simulate_price_impact(1_000_000, 1_000)
assert impact <= 99.99
def test_zero_liquidity(self, analyzer):
impact = analyzer._simulate_price_impact(1, 0)
assert impact > 100
class TestFullAnalysis:
@pytest.mark.asyncio
async def test_healthy_pool(self, analyzer, sample_pool_metadata, sample_positions):
swaps = [
{
"tx_hash": "0xa",
"block": 1,
"timestamp": 1000,
"amount_in": 0.1,
"amount_out": 100,
"price_before": 1000.0,
"price_after": 1000.01,
},
{
"tx_hash": "0xb",
"block": 2,
"timestamp": 1001,
"amount_in": 0.2,
"amount_out": 200,
"price_before": 1000.01,
"price_after": 1000.03,
},
]
report = await analyzer.analyze_pool(
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
recent_swaps=swaps,
positions=sample_positions,
pool_metadata=sample_pool_metadata,
)
assert report.risk_score >= 0
assert report.analysis_time_ms >= 0
assert len(report.signals) >= 0
assert report.pool.address is not None
@pytest.mark.asyncio
async def test_risky_pool(self, analyzer, concentrated_positions, sandwich_swaps):
meta = {
"chain": "ethereum",
"dex": "uniswap_v3",
"version": "v3",
"token0_symbol": "SHIT",
"token1_symbol": "USDC",
"fee_tier": 0,
"total_liquidity_usd": 5000.0,
"owner": "0xDEAD...DEAD",
"created_at": 1000,
}
report = await analyzer.analyze_pool(
"0xDEAD00000000000000000000000000000000BEEF",
recent_swaps=sandwich_swaps,
positions=concentrated_positions,
pool_metadata=meta,
)
assert report.risk_score > 25
assert report.price_impact_1eth > 0.01
assert len(report.recommendations) > 0
class TestReportFormat:
def test_format(self, analyzer, sample_pool_metadata):
pool = analyzer._build_pool_config("0xpool", sample_pool_metadata)
# Build minimal report
from app.dex_pool_manipulation_analyzer import PoolRiskReport, RiskCategory, RiskSignal
report_obj = PoolRiskReport(
pool=pool,
risk_score=45.5,
signals=[RiskSignal(RiskCategory.LIQUIDITY_CONCENTRATION, 0.5, "Test signal")],
price_impact_1eth=0.05,
price_impact_10eth=0.5,
price_impact_100eth=5.0,
top_5_concentration_pct=85.0,
liquidity_depth_1pct=100000.0,
sandwich_profit_estimate=0.01,
recommendations=["Test recommendation"],
analysis_time_ms=42,
)
result = format_risk_report(report_obj)
assert result["risk_score"] == 45.5
assert result["risk_level"] == "high"
assert len(result["signals"]) == 1
assert len(result["recommendations"]) == 1
assert len(result["metrics"]["price_impact"]) == 3
|