File size: 13,358 Bytes
97623a6
 
 
 
 
d9e3f9e
 
4a8bd50
d9e3f9e
 
 
 
97623a6
d9e3f9e
4a8bd50
97623a6
 
 
 
 
4a8bd50
97623a6
 
4a8bd50
97623a6
 
d9e3f9e
97623a6
 
 
 
 
 
 
 
 
 
 
 
 
4a8bd50
97623a6
 
 
 
 
 
 
 
 
4a8bd50
97623a6
 
 
 
 
 
 
 
4a8bd50
97623a6
 
 
 
 
 
 
 
4a8bd50
97623a6
 
 
 
 
d9e3f9e
97623a6
 
 
 
 
 
d9e3f9e
97623a6
 
4a8bd50
97623a6
 
4a8bd50
97623a6
 
4a8bd50
97623a6
 
4a8bd50
97623a6
4a8bd50
 
 
 
d9e3f9e
97623a6
 
4a8bd50
 
 
97623a6
 
 
 
 
 
 
 
 
 
 
 
 
4a8bd50
 
 
 
 
 
97623a6
4a8bd50
 
97623a6
4a8bd50
 
 
 
97623a6
 
 
 
4a8bd50
 
97623a6
4a8bd50
97623a6
 
4a8bd50
 
 
 
97623a6
 
 
 
4a8bd50
 
97623a6
 
 
d9e3f9e
97623a6
 
 
 
d9e3f9e
97623a6
 
d9e3f9e
97623a6
 
d9e3f9e
97623a6
 
4a8bd50
97623a6
 
 
 
 
 
 
 
 
 
4a8bd50
97623a6
d9e3f9e
97623a6
 
 
 
 
 
 
 
 
4a8bd50
97623a6
 
 
4a8bd50
97623a6
4a8bd50
 
d9e3f9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97623a6
d9e3f9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a8bd50
d9e3f9e
 
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
import requests
from decimal import Decimal
from datetime import datetime, timedelta
from typing import List, Dict, Any
import logging
import asyncio
from flask import Flask, render_template_string, request

# Flask setup
app = Flask(__name__)

# Logging setup
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

# Fraud Detection Constants
BLACKLISTED_ADDRESSES = ["0x0000000000000000000000000000000000000000", "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"]  # Example blacklisted addresses
FRAUDULENT_THRESHOLD = 10  # Flag transactions where the value is higher than this threshold in ETH
TRANSACTION_FREQUENCY_LIMIT = 50  # Maximum number of transactions per day to be considered "normal"
TIME_WINDOW = timedelta(hours=24)  # Time window for detecting high-frequency transactions (24 hours)

class ValidationError(Exception):
    pass

class FraudDetection:
    """Handles fraud detection by analyzing wallet transactions."""

    @staticmethod
    def detect_high_frequency_transactions(transactions: List[Dict[str, Any]]) -> bool:
        """Detect high-frequency transactions within a short time period (e.g., 24 hours)."""
        recent_transactions = []
        current_time = datetime.now()
        for tx in transactions:
            timestamp = datetime.utcfromtimestamp(int(tx['timeStamp']))
            if current_time - timestamp < TIME_WINDOW:
                recent_transactions.append(tx)

        if len(recent_transactions) > TRANSACTION_FREQUENCY_LIMIT:
            return True  # Flag as high-frequency fraud
        return False

    @staticmethod
    def detect_large_token_transfers(transactions: List[Dict[str, Any]]) -> List[str]:
        """Detect large transfers (fraudulent tokens above a threshold)."""
        flagged_transactions = []
        for tx in transactions:
            amount = Decimal(tx["value"]) / Decimal(10 ** int(tx["tokenDecimal"]))
            if amount > FRAUDULENT_THRESHOLD:
                flagged_transactions.append(tx)
        return flagged_transactions

    @staticmethod
    def detect_suspicious_addresses(transactions: List[Dict[str, Any]]) -> List[str]:
        """Detect transactions involving blacklisted or suspicious addresses."""
        flagged_addresses = []
        for tx in transactions:
            if tx["to"].lower() in BLACKLISTED_ADDRESSES or tx["from"].lower() in BLACKLISTED_ADDRESSES:
                flagged_addresses.append(tx)
        return flagged_addresses

    @staticmethod
    def detect_unusual_nft_sales(nft_transactions: List[Dict[str, Any]]) -> List[str]:
        """Detect unusual NFT transfers that may indicate fraudulent activity (high-value sales)."""
        flagged_nfts = []
        for tx in nft_transactions:
            if int(tx['value']) > FRAUDULENT_THRESHOLD:  # Assuming `value` here is the token value
                flagged_nfts.append(tx)
        return flagged_nfts

    @staticmethod
    def run_fraud_detection(wallet_data: Dict[str, Any]) -> Dict[str, Any]:
        """Run all fraud detection checks and return flagged issues."""
        transactions = wallet_data.get("transactions", [])
        nft_transactions = wallet_data.get("nft_collections", [])

        fraud_report = {
            "high_frequency_transactions": False,
            "large_token_transfers": [],
            "suspicious_addresses": [],
            "unusual_nft_sales": []
        }

        # Detect high-frequency transactions
        fraud_report["high_frequency_transactions"] = FraudDetection.detect_high_frequency_transactions(transactions)

        # Detect large token transfers
        fraud_report["large_token_transfers"] = FraudDetection.detect_large_token_transfers(transactions)

        # Detect suspicious addresses
        fraud_report["suspicious_addresses"] = FraudDetection.detect_suspicious_addresses(transactions)

        # Detect unusual NFT sales
        fraud_report["unusual_nft_sales"] = FraudDetection.detect_unusual_nft_sales(nft_transactions)

        return fraud_report


class WalletAnalyzer:
    """Analyzes Ethereum wallet contents using Etherscan API."""

    API_KEY = "Your_Etherscan_API_Key"
    ETHERSCAN_URL = "https://api.etherscan.io/api"

    @staticmethod
    def _validate_address(address: str) -> bool:
        """Validate Ethereum address."""
        if len(address) != 42 or not address.startswith("0x"):
            return False
        return True

    async def _fetch_data(self, params: dict) -> dict:
        """Fetch data from Etherscan API."""
        params["apikey"] = self.API_KEY
        response = requests.get(self.ETHERSCAN_URL, params=params)
        return response.json()

    async def _get_eth_balance(self, address: str) -> str:
        """Fetch ETH balance of the given address."""
        params = {
            "module": "account",
            "action": "balance",
            "address": address,
            "tag": "latest"
        }
        return await self._fetch_data(params)["result"]

    async def _get_token_holdings(self, address: str) -> List[Dict[str, Any]]:
        """Fetch token holdings of the given address."""
        params = {
            "module": "account",
            "action": "tokentx",
            "address": address,
            "startblock": 0,
            "endblock": 99999999,
            "page": 1,
            "offset": 10,
            "sort": "desc"
        }
        return await self._fetch_data(params)["result"]

    async def _get_nft_holdings(self, address: str) -> List[Dict[str, Any]]:
        """Fetch NFT holdings of the given address."""
        params = {
            "module": "account",
            "action": "tokennfttx",
            "address": address,
            "startblock": 0,
            "endblock": 99999999,
            "page": 1,
            "offset": 10,
            "sort": "desc"
        }
        return await self._fetch_data(params)["result"]

    async def get_portfolio_data(self, address: str) -> Dict[str, Any]:
        """Get complete portfolio including ETH, tokens, and NFTs."""
        if not self._validate_address(address):
            raise ValidationError(f"Invalid Ethereum address: {address}")

        logger.info(f"Fetching portfolio data for {address}")

        # Get ETH balance
        eth_balance = await self._get_eth_balance(address)

        # Get token data
        token_holdings = await self._get_token_holdings(address)

        # Get NFT data
        nft_collections = await self._get_nft_holdings(address)

        # Get transactions data for fraud detection
        params = {
            "module": "account",
            "action": "txlist",
            "address": address,
            "startblock": 0,
            "endblock": 99999999,
            "page": 1,
            "offset": 10,
            "sort": "desc",
        }
        transaction_data = await self._fetch_data(params)

        # Prepare wallet data
        wallet_data = {
            "address": address,
            "last_updated": datetime.now().isoformat(),
            "eth_balance": float(eth_balance),
            "tokens": token_holdings,
            "nft_collections": nft_collections,
            "transactions": transaction_data.get("result", [])
        }

        # Run fraud detection
        fraud_report = FraudDetection.run_fraud_detection(wallet_data)
        wallet_data["fraud_report"] = fraud_report

        return wallet_data


# Flask Routes to Handle Requests
@app.route('/')
def index():
    return render_template_string('''
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Ethereum Wallet Analyzer</title>
            <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
            <style>
                .result-section {
                    margin-top: 30px;
                }
                .alert-danger {
                    background-color: #f8d7da;
                    color: #721c24;
                }
                .alert-success {
                    background-color: #d4edda;
                    color: #155724;
                }
                .loading {
                    display: none;
                }
            </style>
        </head>
        <body>
            <div class="container">
                <h2 class="mt-5">Ethereum Wallet Fraud Detection</h2>
                <form action="/analyze_wallet" method="POST" class="mt-4">
                    <div class="mb-3">
                        <label for="address" class="form-label">Enter Ethereum Address</label>
                        <input type="text" name="address" id="address" class="form-control" required>
                    </div>
                    <button type="submit" class="btn btn-primary">Analyze Wallet</button>
                </form>

                <div id="loading" class="loading text-center mt-4">
                    <div class="spinner-border text-primary" role="status"></div>
                    <p>Loading...</p>
                </div>
            </div>

            <script>
                const form = document.querySelector('form');
                const loading = document.getElementById('loading');

                form.addEventListener('submit', function() {
                    loading.style.display = 'block';
                });
            </script>
        </body>
        </html>
    ''')

@app.route('/analyze_wallet', methods=['POST'])
def analyze_wallet():
    address = request.form['address']
    
    wallet_analyzer = WalletAnalyzer()
    loop = asyncio.new_event_loop()  # Create a new event loop to run async code
    asyncio.set_event_loop(loop)
    
    # Get wallet data
    portfolio_data = loop.run_until_complete(wallet_analyzer.get_portfolio_data(address))
    
    return render_template_string('''
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Ethereum Wallet Analyzer - Results</title>
            <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
            <style>
                .result-section {
                    margin-top: 30px;
                }
                .alert-danger {
                    background-color: #f8d7da;
                    color: #721c24;
                }
                .alert-success {
                    background-color: #d4edda;
                    color: #155724;
                }
            </style>
        </head>
        <body>
            <div class="container">
                <h2 class="mt-5">Ethereum Wallet Fraud Detection Results</h2>
                <p><strong>Address:</strong> {{ portfolio_data['address'] }}</p>
                <p><strong>ETH Balance:</strong> {{ portfolio_data['eth_balance'] }} ETH</p>

                <div class="result-section">
                    <h4>Fraud Detection Results:</h4>

                    <!-- High Frequency Transactions -->
                    {% if portfolio_data['fraud_report']['high_frequency_transactions'] %}
                        <div class="alert alert-danger">
                            High Frequency Transactions Detected!
                        </div>
                    {% else %}
                        <div class="alert alert-success">
                            No High Frequency Transactions Detected.
                        </div>
                    {% endif %}

                    <!-- Large Token Transfers -->
                    {% if portfolio_data['fraud_report']['large_token_transfers'] %}
                        <div class="alert alert-danger">
                            Large Token Transfers Detected!
                        </div>
                    {% else %}
                        <div class="alert alert-success">
                            No Large Token Transfers Detected.
                        </div>
                    {% endif %}

                    <!-- Suspicious Addresses -->
                    {% if portfolio_data['fraud_report']['suspicious_addresses'] %}
                        <div class="alert alert-danger">
                            Suspicious Addresses Detected!
                        </div>
                    {% else %}
                        <div class="alert alert-success">
                            No Suspicious Addresses Detected.
                        </div>
                    {% endif %}

                    <!-- Unusual NFT Sales -->
                    {% if portfolio_data['fraud_report']['unusual_nft_sales'] %}
                        <div class="alert alert-danger">
                            Unusual NFT Sales Detected!
                        </div>
                    {% else %}
                        <div class="alert alert-success">
                            No Unusual NFT Sales Detected.
                        </div>
                    {% endif %}
                </div>

                <a href="/" class="btn btn-primary mt-3">Analyze Another Wallet</a>
            </div>
        </body>
        </html>
    ''', portfolio_data=portfolio_data)

if __name__ == "__main__":
    app.run(debug=True)