Spaces:
Sleeping
Sleeping
contract detection and new user overide
Browse files
app.py
CHANGED
|
@@ -38,6 +38,32 @@ print(f"Loaded {len(DUNE_API_KEYS)} Dune API Keys.")
|
|
| 38 |
# Initialize Limiter
|
| 39 |
limiter = Limiter(key_func=get_remote_address)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
app = FastAPI(title="Crypto Wallet Persona API", description="Async API with AI-powered Wallet Analysis.")
|
| 42 |
app.state.limiter = limiter
|
| 43 |
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
|
@@ -172,7 +198,12 @@ def process_wallet_analysis(job_id: str, wallet_address: str):
|
|
| 172 |
# We trust the execution result because we just ran it with the param.
|
| 173 |
row_data = rows[0]
|
| 174 |
|
| 175 |
-
# 2.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
if predictor is None:
|
| 177 |
raise Exception("Inference Model not loaded.")
|
| 178 |
|
|
@@ -181,15 +212,39 @@ def process_wallet_analysis(job_id: str, wallet_address: str):
|
|
| 181 |
val = row_data.get(feature)
|
| 182 |
model_input[feature] = float(val) if val is not None else 0.0
|
| 183 |
|
| 184 |
-
#
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
# 3. Generate AI Explanation
|
| 189 |
explanation = "AI Analysis unavailable."
|
| 190 |
if explainer:
|
| 191 |
explanation = explainer.generate_explanation(
|
| 192 |
-
|
| 193 |
model_input
|
| 194 |
)
|
| 195 |
|
|
@@ -197,8 +252,8 @@ def process_wallet_analysis(job_id: str, wallet_address: str):
|
|
| 197 |
final_result = {
|
| 198 |
"status": "completed",
|
| 199 |
"wallet_address": wallet_address,
|
| 200 |
-
"persona":
|
| 201 |
-
"confidence_scores":
|
| 202 |
"explanation": explanation,
|
| 203 |
"stats": model_input
|
| 204 |
}
|
|
|
|
| 38 |
# Initialize Limiter
|
| 39 |
limiter = Limiter(key_func=get_remote_address)
|
| 40 |
|
| 41 |
+
# --- Helper: Contract Check ---
|
| 42 |
+
def check_is_contract(wallet_address: str) -> bool:
|
| 43 |
+
"""
|
| 44 |
+
Checks if an address is a smart contract using a public RPC.
|
| 45 |
+
Returns True if contract, False if EOA (User).
|
| 46 |
+
"""
|
| 47 |
+
rpc_url = "https://eth.llamarpc.com"
|
| 48 |
+
payload = {
|
| 49 |
+
"jsonrpc": "2.0",
|
| 50 |
+
"method": "eth_getCode",
|
| 51 |
+
"params": [wallet_address, "latest"],
|
| 52 |
+
"id": 1
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
res = requests.post(rpc_url, json=payload, timeout=5)
|
| 57 |
+
if res.status_code == 200:
|
| 58 |
+
result = res.json().get("result")
|
| 59 |
+
# '0x' means no code (EOA). Anything longer means Contract.
|
| 60 |
+
return result != "0x"
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Contract check failed: {e}")
|
| 63 |
+
return False
|
| 64 |
+
|
| 65 |
+
return False
|
| 66 |
+
|
| 67 |
app = FastAPI(title="Crypto Wallet Persona API", description="Async API with AI-powered Wallet Analysis.")
|
| 68 |
app.state.limiter = limiter
|
| 69 |
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
|
|
|
| 198 |
# We trust the execution result because we just ran it with the param.
|
| 199 |
row_data = rows[0]
|
| 200 |
|
| 201 |
+
# 2. Heuristic Analysis & Prediction
|
| 202 |
+
|
| 203 |
+
# A. Contract Check
|
| 204 |
+
is_contract = check_is_contract(wallet_address)
|
| 205 |
+
|
| 206 |
+
# B. Feature Extraction
|
| 207 |
if predictor is None:
|
| 208 |
raise Exception("Inference Model not loaded.")
|
| 209 |
|
|
|
|
| 212 |
val = row_data.get(feature)
|
| 213 |
model_input[feature] = float(val) if val is not None else 0.0
|
| 214 |
|
| 215 |
+
# C. Decide Persona (Hybrid: Rules + AI)
|
| 216 |
+
final_persona = None
|
| 217 |
+
confidence = {}
|
| 218 |
+
|
| 219 |
+
if is_contract:
|
| 220 |
+
final_persona = "Smart Contract Protocol"
|
| 221 |
+
# Flat scores for contract (or custom distribution)
|
| 222 |
+
confidence = {
|
| 223 |
+
"High-Value NFT & Crypto Traders (Degen Whales)": 0.0,
|
| 224 |
+
"High-Frequency Bots / Automated Traders": 0.0,
|
| 225 |
+
"Active Retail Users / Everyday Traders": 0.0,
|
| 226 |
+
"Ultra-Whales / Institutional & Exchange Wallets": 0.0
|
| 227 |
+
}
|
| 228 |
+
elif model_input.get('tx_count', 0) < 30:
|
| 229 |
+
final_persona = "Dormant / New User"
|
| 230 |
+
# Flat scores for new user
|
| 231 |
+
confidence = {
|
| 232 |
+
"High-Value NFT & Crypto Traders (Degen Whales)": 0.0,
|
| 233 |
+
"High-Frequency Bots / Automated Traders": 0.0,
|
| 234 |
+
"Active Retail Users / Everyday Traders": 1.0, # Lean towards Retail
|
| 235 |
+
"Ultra-Whales / Institutional & Exchange Wallets": 0.0
|
| 236 |
+
}
|
| 237 |
+
else:
|
| 238 |
+
# Run AI Inference
|
| 239 |
+
prediction_result = predictor.predict(model_input)
|
| 240 |
+
final_persona = prediction_result['persona']
|
| 241 |
+
confidence = prediction_result['probabilities']
|
| 242 |
|
| 243 |
# 3. Generate AI Explanation
|
| 244 |
explanation = "AI Analysis unavailable."
|
| 245 |
if explainer:
|
| 246 |
explanation = explainer.generate_explanation(
|
| 247 |
+
final_persona,
|
| 248 |
model_input
|
| 249 |
)
|
| 250 |
|
|
|
|
| 252 |
final_result = {
|
| 253 |
"status": "completed",
|
| 254 |
"wallet_address": wallet_address,
|
| 255 |
+
"persona": final_persona,
|
| 256 |
+
"confidence_scores": confidence,
|
| 257 |
"explanation": explanation,
|
| 258 |
"stats": model_input
|
| 259 |
}
|