Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,15 @@ from decimal import Decimal
|
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
from typing import List, Dict, Any
|
| 5 |
import logging
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
logger = logging.getLogger(__name__)
|
|
|
|
| 8 |
|
| 9 |
# Fraud Detection Constants
|
| 10 |
BLACKLISTED_ADDRESSES = ["0x0000000000000000000000000000000000000000", "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"] # Example blacklisted addresses
|
|
@@ -17,7 +24,7 @@ class ValidationError(Exception):
|
|
| 17 |
|
| 18 |
class FraudDetection:
|
| 19 |
"""Handles fraud detection by analyzing wallet transactions."""
|
| 20 |
-
|
| 21 |
@staticmethod
|
| 22 |
def detect_high_frequency_transactions(transactions: List[Dict[str, Any]]) -> bool:
|
| 23 |
"""Detect high-frequency transactions within a short time period (e.g., 24 hours)."""
|
|
@@ -65,14 +72,14 @@ class FraudDetection:
|
|
| 65 |
"""Run all fraud detection checks and return flagged issues."""
|
| 66 |
transactions = wallet_data.get("transactions", [])
|
| 67 |
nft_transactions = wallet_data.get("nft_collections", [])
|
| 68 |
-
|
| 69 |
fraud_report = {
|
| 70 |
"high_frequency_transactions": False,
|
| 71 |
"large_token_transfers": [],
|
| 72 |
"suspicious_addresses": [],
|
| 73 |
"unusual_nft_sales": []
|
| 74 |
}
|
| 75 |
-
|
| 76 |
# Detect high-frequency transactions
|
| 77 |
fraud_report["high_frequency_transactions"] = FraudDetection.detect_high_frequency_transactions(transactions)
|
| 78 |
|
|
@@ -90,7 +97,7 @@ class FraudDetection:
|
|
| 90 |
|
| 91 |
class WalletAnalyzer:
|
| 92 |
"""Analyzes Ethereum wallet contents using Etherscan API."""
|
| 93 |
-
|
| 94 |
API_KEY = "Your_Etherscan_API_Key"
|
| 95 |
ETHERSCAN_URL = "https://api.etherscan.io/api"
|
| 96 |
|
|
@@ -146,18 +153,18 @@ class WalletAnalyzer:
|
|
| 146 |
return await self._fetch_data(params)["result"]
|
| 147 |
|
| 148 |
async def get_portfolio_data(self, address: str) -> Dict[str, Any]:
|
| 149 |
-
"""Get complete portfolio including ETH, tokens, and NFTs."""
|
| 150 |
if not self._validate_address(address):
|
| 151 |
raise ValidationError(f"Invalid Ethereum address: {address}")
|
| 152 |
|
| 153 |
logger.info(f"Fetching portfolio data for {address}")
|
| 154 |
-
|
| 155 |
# Get ETH balance
|
| 156 |
eth_balance = await self._get_eth_balance(address)
|
| 157 |
-
|
| 158 |
# Get token data
|
| 159 |
token_holdings = await self._get_token_holdings(address)
|
| 160 |
-
|
| 161 |
# Get NFT data
|
| 162 |
nft_collections = await self._get_nft_holdings(address)
|
| 163 |
|
|
@@ -173,7 +180,7 @@ class WalletAnalyzer:
|
|
| 173 |
"sort": "desc",
|
| 174 |
}
|
| 175 |
transaction_data = await self._fetch_data(params)
|
| 176 |
-
|
| 177 |
# Prepare wallet data
|
| 178 |
wallet_data = {
|
| 179 |
"address": address,
|
|
@@ -191,12 +198,155 @@ class WalletAnalyzer:
|
|
| 191 |
return wallet_data
|
| 192 |
|
| 193 |
|
| 194 |
-
#
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
wallet_analyzer = WalletAnalyzer()
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
|
|
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
from typing import List, Dict, Any
|
| 5 |
import logging
|
| 6 |
+
import asyncio
|
| 7 |
+
from flask import Flask, render_template_string, request
|
| 8 |
|
| 9 |
+
# Flask setup
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
# Logging setup
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
+
logging.basicConfig(level=logging.INFO)
|
| 15 |
|
| 16 |
# Fraud Detection Constants
|
| 17 |
BLACKLISTED_ADDRESSES = ["0x0000000000000000000000000000000000000000", "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"] # Example blacklisted addresses
|
|
|
|
| 24 |
|
| 25 |
class FraudDetection:
|
| 26 |
"""Handles fraud detection by analyzing wallet transactions."""
|
| 27 |
+
|
| 28 |
@staticmethod
|
| 29 |
def detect_high_frequency_transactions(transactions: List[Dict[str, Any]]) -> bool:
|
| 30 |
"""Detect high-frequency transactions within a short time period (e.g., 24 hours)."""
|
|
|
|
| 72 |
"""Run all fraud detection checks and return flagged issues."""
|
| 73 |
transactions = wallet_data.get("transactions", [])
|
| 74 |
nft_transactions = wallet_data.get("nft_collections", [])
|
| 75 |
+
|
| 76 |
fraud_report = {
|
| 77 |
"high_frequency_transactions": False,
|
| 78 |
"large_token_transfers": [],
|
| 79 |
"suspicious_addresses": [],
|
| 80 |
"unusual_nft_sales": []
|
| 81 |
}
|
| 82 |
+
|
| 83 |
# Detect high-frequency transactions
|
| 84 |
fraud_report["high_frequency_transactions"] = FraudDetection.detect_high_frequency_transactions(transactions)
|
| 85 |
|
|
|
|
| 97 |
|
| 98 |
class WalletAnalyzer:
|
| 99 |
"""Analyzes Ethereum wallet contents using Etherscan API."""
|
| 100 |
+
|
| 101 |
API_KEY = "Your_Etherscan_API_Key"
|
| 102 |
ETHERSCAN_URL = "https://api.etherscan.io/api"
|
| 103 |
|
|
|
|
| 153 |
return await self._fetch_data(params)["result"]
|
| 154 |
|
| 155 |
async def get_portfolio_data(self, address: str) -> Dict[str, Any]:
|
| 156 |
+
"""Get complete portfolio including ETH, tokens, and NFTs."""
|
| 157 |
if not self._validate_address(address):
|
| 158 |
raise ValidationError(f"Invalid Ethereum address: {address}")
|
| 159 |
|
| 160 |
logger.info(f"Fetching portfolio data for {address}")
|
| 161 |
+
|
| 162 |
# Get ETH balance
|
| 163 |
eth_balance = await self._get_eth_balance(address)
|
| 164 |
+
|
| 165 |
# Get token data
|
| 166 |
token_holdings = await self._get_token_holdings(address)
|
| 167 |
+
|
| 168 |
# Get NFT data
|
| 169 |
nft_collections = await self._get_nft_holdings(address)
|
| 170 |
|
|
|
|
| 180 |
"sort": "desc",
|
| 181 |
}
|
| 182 |
transaction_data = await self._fetch_data(params)
|
| 183 |
+
|
| 184 |
# Prepare wallet data
|
| 185 |
wallet_data = {
|
| 186 |
"address": address,
|
|
|
|
| 198 |
return wallet_data
|
| 199 |
|
| 200 |
|
| 201 |
+
# Flask Routes to Handle Requests
|
| 202 |
+
@app.route('/')
|
| 203 |
+
def index():
|
| 204 |
+
return render_template_string('''
|
| 205 |
+
<!DOCTYPE html>
|
| 206 |
+
<html lang="en">
|
| 207 |
+
<head>
|
| 208 |
+
<meta charset="UTF-8">
|
| 209 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 210 |
+
<title>Ethereum Wallet Analyzer</title>
|
| 211 |
+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
| 212 |
+
<style>
|
| 213 |
+
.result-section {
|
| 214 |
+
margin-top: 30px;
|
| 215 |
+
}
|
| 216 |
+
.alert-danger {
|
| 217 |
+
background-color: #f8d7da;
|
| 218 |
+
color: #721c24;
|
| 219 |
+
}
|
| 220 |
+
.alert-success {
|
| 221 |
+
background-color: #d4edda;
|
| 222 |
+
color: #155724;
|
| 223 |
+
}
|
| 224 |
+
.loading {
|
| 225 |
+
display: none;
|
| 226 |
+
}
|
| 227 |
+
</style>
|
| 228 |
+
</head>
|
| 229 |
+
<body>
|
| 230 |
+
<div class="container">
|
| 231 |
+
<h2 class="mt-5">Ethereum Wallet Fraud Detection</h2>
|
| 232 |
+
<form action="/analyze_wallet" method="POST" class="mt-4">
|
| 233 |
+
<div class="mb-3">
|
| 234 |
+
<label for="address" class="form-label">Enter Ethereum Address</label>
|
| 235 |
+
<input type="text" name="address" id="address" class="form-control" required>
|
| 236 |
+
</div>
|
| 237 |
+
<button type="submit" class="btn btn-primary">Analyze Wallet</button>
|
| 238 |
+
</form>
|
| 239 |
+
|
| 240 |
+
<div id="loading" class="loading text-center mt-4">
|
| 241 |
+
<div class="spinner-border text-primary" role="status"></div>
|
| 242 |
+
<p>Loading...</p>
|
| 243 |
+
</div>
|
| 244 |
+
</div>
|
| 245 |
+
|
| 246 |
+
<script>
|
| 247 |
+
const form = document.querySelector('form');
|
| 248 |
+
const loading = document.getElementById('loading');
|
| 249 |
+
|
| 250 |
+
form.addEventListener('submit', function() {
|
| 251 |
+
loading.style.display = 'block';
|
| 252 |
+
});
|
| 253 |
+
</script>
|
| 254 |
+
</body>
|
| 255 |
+
</html>
|
| 256 |
+
''')
|
| 257 |
+
|
| 258 |
+
@app.route('/analyze_wallet', methods=['POST'])
|
| 259 |
+
def analyze_wallet():
|
| 260 |
+
address = request.form['address']
|
| 261 |
+
|
| 262 |
wallet_analyzer = WalletAnalyzer()
|
| 263 |
+
loop = asyncio.new_event_loop() # Create a new event loop to run async code
|
| 264 |
+
asyncio.set_event_loop(loop)
|
| 265 |
+
|
| 266 |
+
# Get wallet data
|
| 267 |
+
portfolio_data = loop.run_until_complete(wallet_analyzer.get_portfolio_data(address))
|
| 268 |
+
|
| 269 |
+
return render_template_string('''
|
| 270 |
+
<!DOCTYPE html>
|
| 271 |
+
<html lang="en">
|
| 272 |
+
<head>
|
| 273 |
+
<meta charset="UTF-8">
|
| 274 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 275 |
+
<title>Ethereum Wallet Analyzer - Results</title>
|
| 276 |
+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
| 277 |
+
<style>
|
| 278 |
+
.result-section {
|
| 279 |
+
margin-top: 30px;
|
| 280 |
+
}
|
| 281 |
+
.alert-danger {
|
| 282 |
+
background-color: #f8d7da;
|
| 283 |
+
color: #721c24;
|
| 284 |
+
}
|
| 285 |
+
.alert-success {
|
| 286 |
+
background-color: #d4edda;
|
| 287 |
+
color: #155724;
|
| 288 |
+
}
|
| 289 |
+
</style>
|
| 290 |
+
</head>
|
| 291 |
+
<body>
|
| 292 |
+
<div class="container">
|
| 293 |
+
<h2 class="mt-5">Ethereum Wallet Fraud Detection Results</h2>
|
| 294 |
+
<p><strong>Address:</strong> {{ portfolio_data['address'] }}</p>
|
| 295 |
+
<p><strong>ETH Balance:</strong> {{ portfolio_data['eth_balance'] }} ETH</p>
|
| 296 |
+
|
| 297 |
+
<div class="result-section">
|
| 298 |
+
<h4>Fraud Detection Results:</h4>
|
| 299 |
+
|
| 300 |
+
<!-- High Frequency Transactions -->
|
| 301 |
+
{% if portfolio_data['fraud_report']['high_frequency_transactions'] %}
|
| 302 |
+
<div class="alert alert-danger">
|
| 303 |
+
High Frequency Transactions Detected!
|
| 304 |
+
</div>
|
| 305 |
+
{% else %}
|
| 306 |
+
<div class="alert alert-success">
|
| 307 |
+
No High Frequency Transactions Detected.
|
| 308 |
+
</div>
|
| 309 |
+
{% endif %}
|
| 310 |
+
|
| 311 |
+
<!-- Large Token Transfers -->
|
| 312 |
+
{% if portfolio_data['fraud_report']['large_token_transfers'] %}
|
| 313 |
+
<div class="alert alert-danger">
|
| 314 |
+
Large Token Transfers Detected!
|
| 315 |
+
</div>
|
| 316 |
+
{% else %}
|
| 317 |
+
<div class="alert alert-success">
|
| 318 |
+
No Large Token Transfers Detected.
|
| 319 |
+
</div>
|
| 320 |
+
{% endif %}
|
| 321 |
+
|
| 322 |
+
<!-- Suspicious Addresses -->
|
| 323 |
+
{% if portfolio_data['fraud_report']['suspicious_addresses'] %}
|
| 324 |
+
<div class="alert alert-danger">
|
| 325 |
+
Suspicious Addresses Detected!
|
| 326 |
+
</div>
|
| 327 |
+
{% else %}
|
| 328 |
+
<div class="alert alert-success">
|
| 329 |
+
No Suspicious Addresses Detected.
|
| 330 |
+
</div>
|
| 331 |
+
{% endif %}
|
| 332 |
+
|
| 333 |
+
<!-- Unusual NFT Sales -->
|
| 334 |
+
{% if portfolio_data['fraud_report']['unusual_nft_sales'] %}
|
| 335 |
+
<div class="alert alert-danger">
|
| 336 |
+
Unusual NFT Sales Detected!
|
| 337 |
+
</div>
|
| 338 |
+
{% else %}
|
| 339 |
+
<div class="alert alert-success">
|
| 340 |
+
No Unusual NFT Sales Detected.
|
| 341 |
+
</div>
|
| 342 |
+
{% endif %}
|
| 343 |
+
</div>
|
| 344 |
+
|
| 345 |
+
<a href="/" class="btn btn-primary mt-3">Analyze Another Wallet</a>
|
| 346 |
+
</div>
|
| 347 |
+
</body>
|
| 348 |
+
</html>
|
| 349 |
+
''', portfolio_data=portfolio_data)
|
| 350 |
|
| 351 |
+
if __name__ == "__main__":
|
| 352 |
+
app.run(debug=True)
|