-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
249 lines (213 loc) · 7.91 KB
/
database.py
File metadata and controls
249 lines (213 loc) · 7.91 KB
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
# ============================================================
# database.py — ALL DATA STORAGE
# ============================================================
from config import SUPABASE_URL, SUPABASE_KEY
from datetime import datetime, timedelta
# Safe Supabase import — works whether or not library is installed
try:
from supabase import create_client, Client
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
print("[DB] ✅ Supabase connected")
SUPABASE_OK = True
except Exception as e:
print(f"[DB] ⚠️ Supabase unavailable: {e}")
supabase = None
SUPABASE_OK = False
# ============================================================
# SAFE QUERY HELPER
# All DB functions use this — never crashes bot if DB is down
# ============================================================
def db_available():
if not SUPABASE_OK or supabase is None:
print("[DB] Skipping — Supabase not connected")
return False
return True
# ============================================================
# TRADES
# ============================================================
def log_trade(token_address, token_name, ticker, action,
price_usd, mcap_usd, amount_sol, score,
source, paper_trade=True):
if not db_available():
return
try:
data = {
"token_address": token_address,
"token_name": token_name,
"ticker": ticker,
"action": action,
"price_usd": price_usd,
"mcap_usd": mcap_usd,
"amount_sol": amount_sol,
"score": score,
"source": source,
"paper_trade": paper_trade,
"exited": False,
"timestamp": datetime.utcnow().isoformat()
}
supabase.table("trades").insert(data).execute()
print(f"[DB] Trade logged: {action} {ticker} | Score: {score}")
except Exception as e:
print(f"[DB] log_trade error: {e}")
def get_open_positions():
if not db_available():
return []
try:
result = supabase.table("trades") \
.select("*") \
.eq("action", "BUY") \
.eq("exited", False) \
.execute()
return result.data if result.data else []
except Exception as e:
print(f"[DB] get_open_positions error: {e}")
return []
def mark_position_exited(token_address):
if not db_available():
return
try:
supabase.table("trades") \
.update({
"exited": True,
"exit_time": datetime.utcnow().isoformat()
}) \
.eq("token_address", token_address) \
.eq("exited", False) \
.execute()
except Exception as e:
print(f"[DB] mark_position_exited error: {e}")
# ============================================================
# SMART WALLETS
# ============================================================
def save_smart_wallet(wallet_address, win_rate,
total_trades, avg_multiplier, notes=""):
if not db_available():
return
try:
data = {
"wallet_address": wallet_address,
"win_rate": win_rate,
"total_trades": total_trades,
"avg_multiplier": avg_multiplier,
"notes": notes,
"added_at": datetime.utcnow().isoformat()
}
supabase.table("smart_wallets").upsert(data).execute()
print(f"[DB] Smart wallet saved: {wallet_address[:20]}...")
except Exception as e:
print(f"[DB] save_smart_wallet error: {e}")
def get_all_smart_wallets():
if not db_available():
return []
try:
result = supabase.table("smart_wallets") \
.select("wallet_address") \
.execute()
if not result.data:
return []
return [row["wallet_address"] for row in result.data]
except Exception as e:
print(f"[DB] get_all_smart_wallets error: {e}")
return []
def update_wallet_stats(wallet_address, win_rate, total_trades):
if not db_available():
return
try:
supabase.table("smart_wallets") \
.update({
"win_rate": win_rate,
"total_trades": total_trades
}) \
.eq("wallet_address", wallet_address) \
.execute()
except Exception as e:
print(f"[DB] update_wallet_stats error: {e}")
# ============================================================
# KOL POSTS
# ============================================================
def log_kol_post(influencer, post_text, post_url, keywords):
if not db_available():
return
try:
data = {
"influencer": influencer,
"post_text": post_text[:500],
"post_url": post_url,
"keywords": ", ".join(keywords),
"detected_at": datetime.utcnow().isoformat()
}
supabase.table("kol_posts").insert(data).execute()
print(f"[DB] KOL post logged: @{influencer}")
except Exception as e:
print(f"[DB] log_kol_post error: {e}")
def get_recent_kol_posts(minutes=20):
if not db_available():
return []
try:
cutoff = (datetime.utcnow() - timedelta(minutes=minutes)).isoformat()
result = supabase.table("kol_posts") \
.select("*") \
.gte("detected_at", cutoff) \
.execute()
return result.data if result.data else []
except Exception as e:
print(f"[DB] get_recent_kol_posts error: {e}")
return []
# ============================================================
# TOKENS SEEN — duplicate protection
# ============================================================
def token_already_seen(token_address):
if not db_available():
return False
try:
result = supabase.table("tokens_seen") \
.select("token_address") \
.eq("token_address", token_address) \
.execute()
return len(result.data) > 0 if result.data else False
except Exception as e:
print(f"[DB] token_already_seen error: {e}")
return False
def mark_token_seen(token_address, token_name, score, decision):
if not db_available():
return
try:
data = {
"token_address": token_address,
"token_name": token_name,
"score": score,
"decision": decision,
"seen_at": datetime.utcnow().isoformat()
}
supabase.table("tokens_seen").insert(data).execute()
except Exception as e:
print(f"[DB] mark_token_seen error: {e}")
# ============================================================
# PERFORMANCE SUMMARY
# ============================================================
def get_performance_summary():
if not db_available():
return {"message": "Database not connected"}
try:
result = supabase.table("trades") \
.select("*") \
.execute()
trades = result.data if result.data else []
if not trades:
return {"message": "No trades yet"}
buys = [t for t in trades if t.get("action") == "BUY"]
sells = [t for t in trades if t.get("action") == "SELL"]
total_invested = sum(float(t.get("amount_sol") or 0) for t in buys)
total_returned = sum(float(t.get("amount_sol") or 0) for t in sells)
pnl = total_returned - total_invested
win_rate = (len(sells) / len(buys) * 100) if buys else 0
return {
"total_trades": len(buys),
"total_invested": round(total_invested, 4),
"total_returned": round(total_returned, 4),
"pnl_sol": round(pnl, 4),
"win_rate": round(win_rate, 2)
}
except Exception as e:
print(f"[DB] get_performance_summary error: {e}")
return {"message": f"Error: {e}"}