|
| 1 | +from fastapi import FastAPI, HTTPException |
| 2 | +from fastapi.responses import HTMLResponse |
| 3 | +from pydantic import BaseModel |
| 4 | +from typing import Optional, Dict, Any |
| 5 | +import json |
| 6 | +import os |
| 7 | + |
| 8 | +from core.config import NETWORK, MIN_PROFIT_USD |
| 9 | +from core.helpers import estimate_profit, parse_health_factor |
| 10 | + |
| 11 | +app = FastAPI(title="DeFiLiquidatorAI API", description="AI-powered liquidation bot", version="1.0.0") |
| 12 | + |
| 13 | +class LiquidationRequest(BaseModel): |
| 14 | + user_address: str |
| 15 | + network: Optional[str] = "polygon" |
| 16 | + min_profit_usd: Optional[float] = 5.0 |
| 17 | + |
| 18 | +@app.get("/", response_class=HTMLResponse) |
| 19 | +async def root(): |
| 20 | + return """ |
| 21 | + <!DOCTYPE html> |
| 22 | + <html> |
| 23 | + <head> |
| 24 | + <title>DeFiLiquidatorAI</title> |
| 25 | + <style> |
| 26 | + body { font-family: Arial; margin: 40px; background: #0a0a0a; color: #fff; } |
| 27 | + .container { max-width: 800px; margin: auto; background: #1a1a1a; padding: 20px; border-radius: 10px; } |
| 28 | + input, button { padding: 10px; margin: 5px; width: 100%; } |
| 29 | + button { background: #4CAF50; color: white; border: none; cursor: pointer; } |
| 30 | + .result { margin-top: 20px; padding: 10px; background: #2a2a2a; border-radius: 5px; } |
| 31 | + </style> |
| 32 | + </head> |
| 33 | + <body> |
| 34 | + <div class="container"> |
| 35 | + <h1>🤖 DeFiLiquidatorAI</h1> |
| 36 | + <p>AI-powered liquidation bot for Aave v3</p> |
| 37 | + |
| 38 | + <h3>Liquidate Position</h3> |
| 39 | + <input type="text" id="address" placeholder="User address (0x...)" /> |
| 40 | + <select id="network"> |
| 41 | + <option value="polygon">Polygon</option> |
| 42 | + <option value="base">Base</option> |
| 43 | + <option value="arbitrum">Arbitrum</option> |
| 44 | + <option value="ethereum">Ethereum</option> |
| 45 | + </select> |
| 46 | + <input type="number" id="minProfit" placeholder="Min profit USD" value="5.0" /> |
| 47 | + <button onclick="liquidate()">Execute Liquidation</button> |
| 48 | + <div id="result" class="result"></div> |
| 49 | + </div> |
| 50 | + |
| 51 | + <script> |
| 52 | + async function liquidate() { |
| 53 | + const address = document.getElementById('address').value; |
| 54 | + const network = document.getElementById('network').value; |
| 55 | + const minProfit = parseFloat(document.getElementById('minProfit').value); |
| 56 | + const resultDiv = document.getElementById('result'); |
| 57 | + |
| 58 | + resultDiv.innerHTML = "⏳ Checking position..."; |
| 59 | + |
| 60 | + try { |
| 61 | + const response = await fetch('/api/liquidate', { |
| 62 | + method: 'POST', |
| 63 | + headers: { 'Content-Type': 'application/json' }, |
| 64 | + body: JSON.stringify({ user_address: address, network: network, min_profit_usd: minProfit }) |
| 65 | + }); |
| 66 | + const data = await response.json(); |
| 67 | + resultDiv.innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`; |
| 68 | + } catch(e) { |
| 69 | + resultDiv.innerHTML = "❌ Error: " + e.message; |
| 70 | + } |
| 71 | + } |
| 72 | + </script> |
| 73 | + </body> |
| 74 | + </html> |
| 75 | + """ |
| 76 | + |
| 77 | +@app.get("/health") |
| 78 | +async def health(): |
| 79 | + return {"status": "ok", "network": NETWORK, "min_profit_usd": MIN_PROFIT_USD} |
| 80 | + |
| 81 | +@app.post("/api/liquidate") |
| 82 | +async def liquidate(request: LiquidationRequest): |
| 83 | + # This is a mock response - actual liquidation would call the smart contract |
| 84 | + return { |
| 85 | + "status": "simulated", |
| 86 | + "user": request.user_address, |
| 87 | + "network": request.network, |
| 88 | + "min_profit_usd": request.min_profit_usd, |
| 89 | + "message": "Liquidation simulation. In production, this would execute the actual transaction.", |
| 90 | + "estimated_profit": 45.50, |
| 91 | + "health_factor": 0.97 |
| 92 | + } |
0 commit comments