-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.py
More file actions
373 lines (319 loc) · 12.8 KB
/
executor.py
File metadata and controls
373 lines (319 loc) · 12.8 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# ============================================================
# executor.py — JUPITER SWAP EXECUTION ENGINE
# Handles all buys in correct Jupiter V6 sequence
# Paper trading mode: logs everything, spends nothing
# ============================================================
import aiohttp
import asyncio
import base64
import json
import requests
from config import (
WALLET_PRIVATE_KEY, WALLET_PUBLIC_KEY,
HELIUS_RPC_URL, PAPER_TRADING
)
from scorer import calculate_position_size
from database import log_trade
from telegram_bot import alert_buy_executed, alert_bot_error
JUPITER_QUOTE_URL = "https://quote-api.jup.ag/v6/quote"
JUPITER_SWAP_URL = "https://quote-api.jup.ag/v6/swap"
SOL_MINT = "So11111111111111111111111111111111111111112"
LAMPORTS_PER_SOL = 1_000_000_000
SLIPPAGE_BPS = 300 # 3% slippage tolerance
# ============================================================
# MAIN EXECUTE BUY
# ============================================================
async def execute_buy(token_address, token_name, ticker,
score, source):
print(f"[EXECUTOR] Buy signal: {ticker} | Score: {score}")
# ── Step 1: Get wallet SOL balance ───────────────────
balance_sol = await get_wallet_balance()
if balance_sol is None:
await alert_bot_error("Executor", f"Could not fetch wallet balance for {ticker}")
return
if balance_sol < 0.01:
print(f"[EXECUTOR] Wallet too low ({balance_sol} SOL) — skipping {ticker}")
return
# ── Step 2: Calculate position size ──────────────────
amount_sol = calculate_position_size(score, balance_sol)
if amount_sol <= 0:
print(f"[EXECUTOR] Position size 0 — skipping")
return
amount_lamports = int(amount_sol * LAMPORTS_PER_SOL)
print(f"[EXECUTOR] Buying {amount_sol} SOL worth of {ticker}")
# ── PAPER TRADING MODE ────────────────────────────────
if PAPER_TRADING:
await handle_paper_buy(
token_address, token_name, ticker,
amount_sol, score, source
)
return
# ── LIVE TRADING MODE ─────────────────────────────────
await handle_live_buy(
token_address, token_name, ticker,
amount_sol, amount_lamports, score, source
)
# ============================================================
# PAPER TRADING BUY — no real money, full simulation
# ============================================================
async def handle_paper_buy(token_address, token_name, ticker,
amount_sol, score, source):
# Get current price from DexScreener
price_usd, mcap_usd = get_current_price(token_address)
# Log as paper trade
log_trade(
token_address=token_address,
token_name=token_name,
ticker=ticker,
action="BUY",
price_usd=price_usd,
mcap_usd=mcap_usd,
amount_sol=amount_sol,
score=score,
source=source,
paper_trade=True
)
await alert_buy_executed(
ticker=ticker,
token_name=token_name,
token_address=token_address,
amount_sol=amount_sol,
price_usd=price_usd,
mcap_usd=mcap_usd,
score=score,
paper=True
)
print(f"[EXECUTOR] 📝 Paper buy logged: {ticker} | {amount_sol} SOL")
# ============================================================
# LIVE TRADING BUY — real Jupiter swap
# ============================================================
async def handle_live_buy(token_address, token_name, ticker,
amount_sol, amount_lamports,
score, source):
try:
# Step 1 — Get Jupiter quote
quote = await get_jupiter_quote(token_address, amount_lamports)
if not quote:
await alert_bot_error("Executor", f"No Jupiter quote for {ticker}")
return
# Step 2 — Build swap transaction
swap_tx = await get_swap_transaction(quote)
if not swap_tx:
await alert_bot_error("Executor", f"Could not build swap tx for {ticker}")
return
# Step 3 — Sign transaction
signed_tx = sign_transaction(swap_tx)
if not signed_tx:
await alert_bot_error("Executor", f"Could not sign tx for {ticker}")
return
# Step 4 — Send to Solana via Helius
tx_signature = await send_transaction(signed_tx)
if not tx_signature:
await alert_bot_error("Executor", f"Send tx failed for {ticker}")
return
# Step 5 — Wait for confirmation
confirmed = await confirm_transaction(tx_signature)
if not confirmed:
await alert_bot_error("Executor",
f"TX not confirmed for {ticker} — sig: {tx_signature[:20]}...")
return
# Step 6 — Log confirmed trade
price_usd, mcap_usd = get_current_price(token_address)
log_trade(
token_address=token_address,
token_name=token_name,
ticker=ticker,
action="BUY",
price_usd=price_usd,
mcap_usd=mcap_usd,
amount_sol=amount_sol,
score=score,
source=source,
paper_trade=False
)
await alert_buy_executed(
ticker=ticker,
token_name=token_name,
token_address=token_address,
amount_sol=amount_sol,
price_usd=price_usd,
mcap_usd=mcap_usd,
score=score,
paper=False
)
print(f"[EXECUTOR] ✅ Live buy confirmed: {ticker} | TX: {tx_signature[:20]}...")
except Exception as e:
print(f"[EXECUTOR] Live buy error: {e}")
await alert_bot_error("Executor", f"Buy failed for {ticker}: {e}")
# ============================================================
# JUPITER API CALLS
# ============================================================
async def get_jupiter_quote(token_address, amount_lamports):
"""Gets best swap route from Jupiter"""
try:
params = {
"inputMint": SOL_MINT,
"outputMint": token_address,
"amount": amount_lamports,
"slippageBps": SLIPPAGE_BPS,
}
async with aiohttp.ClientSession() as session:
async with session.get(
JUPITER_QUOTE_URL, params=params,
timeout=aiohttp.ClientTimeout(total=8)
) as resp:
if resp.status != 200:
print(f"[EXECUTOR] Jupiter quote status: {resp.status}")
return None
data = await resp.json()
# Check quote has valid route
if not data.get("routePlan"):
print(f"[EXECUTOR] No route found")
return None
return data
except Exception as e:
print(f"[EXECUTOR] Quote error: {e}")
return None
async def get_swap_transaction(quote):
"""Builds the swap transaction from a Jupiter quote"""
try:
payload = {
"quoteResponse": quote,
"userPublicKey": WALLET_PUBLIC_KEY,
"wrapAndUnwrapSol": True,
"prioritizationFeeLamports": 100000, # Priority fee prevents tx drop
"dynamicComputeUnitLimit": True
}
async with aiohttp.ClientSession() as session:
async with session.post(
JUPITER_SWAP_URL, json=payload,
timeout=aiohttp.ClientTimeout(total=10)
) as resp:
if resp.status != 200:
print(f"[EXECUTOR] Swap build status: {resp.status}")
return None
data = await resp.json()
return data.get("swapTransaction")
except Exception as e:
print(f"[EXECUTOR] Swap build error: {e}")
return None
def sign_transaction(swap_tx_base64):
"""Signs the base64 transaction with your private key"""
try:
from solders.keypair import Keypair
from solders.transaction import VersionedTransaction
# Decode the transaction
raw_tx = base64.b64decode(swap_tx_base64)
tx = VersionedTransaction.from_bytes(raw_tx)
# Load keypair from private key
keypair = Keypair.from_base58_string(WALLET_PRIVATE_KEY)
# Sign
tx.sign([keypair])
# Return as base64
return base64.b64encode(bytes(tx)).decode("utf-8")
except Exception as e:
print(f"[EXECUTOR] Sign error: {e}")
return None
async def send_transaction(signed_tx_base64):
"""Sends signed transaction to Solana via Helius RPC"""
try:
payload = {
"jsonrpc": "2.0",
"id": 1,
"method": "sendTransaction",
"params": [
signed_tx_base64,
{
"encoding": "base64",
"skipPreflight": False,
"preflightCommitment": "confirmed",
"maxRetries": 3
}
]
}
async with aiohttp.ClientSession() as session:
async with session.post(
HELIUS_RPC_URL, json=payload,
timeout=aiohttp.ClientTimeout(total=15)
) as resp:
data = await resp.json()
if "error" in data:
print(f"[EXECUTOR] RPC error: {data['error']}")
return None
return data.get("result") # Transaction signature
except Exception as e:
print(f"[EXECUTOR] Send tx error: {e}")
return None
async def confirm_transaction(signature, max_retries=20):
"""Polls Solana until transaction is confirmed or times out"""
payload = {
"jsonrpc": "2.0",
"id": 1,
"method": "getSignatureStatuses",
"params": [[signature]]
}
for attempt in range(max_retries):
try:
async with aiohttp.ClientSession() as session:
async with session.post(
HELIUS_RPC_URL, json=payload,
timeout=aiohttp.ClientTimeout(total=8)
) as resp:
data = await resp.json()
results = data.get("result", {}).get("value", [None])
status = results[0] if results else None
if status:
confirmation = status.get("confirmationStatus", "")
if confirmation in ("confirmed", "finalized"):
print(f"[EXECUTOR] ✅ TX confirmed: {signature[:20]}...")
return True
err = status.get("err")
if err:
print(f"[EXECUTOR] TX failed on chain: {err}")
return False
except Exception as e:
print(f"[EXECUTOR] Confirm poll error: {e}")
await asyncio.sleep(3) # Wait 3s between polls
print(f"[EXECUTOR] TX confirmation timeout: {signature[:20]}...")
return False
# ============================================================
# HELPERS
# ============================================================
async def get_wallet_balance():
"""Gets current SOL balance of trading wallet"""
try:
payload = {
"jsonrpc": "2.0",
"id": 1,
"method": "getBalance",
"params": [WALLET_PUBLIC_KEY]
}
async with aiohttp.ClientSession() as session:
async with session.post(
HELIUS_RPC_URL, json=payload,
timeout=aiohttp.ClientTimeout(total=6)
) as resp:
data = await resp.json()
lamports = data.get("result", {}).get("value", 0)
balance = lamports / LAMPORTS_PER_SOL
print(f"[EXECUTOR] Wallet balance: {balance:.4f} SOL")
return balance
except Exception as e:
print(f"[EXECUTOR] Balance check error: {e}")
return None
def get_current_price(token_address):
"""Returns (price_usd, mcap_usd) from DexScreener"""
try:
url = f"https://api.dexscreener.com/latest/dex/tokens/{token_address}"
response = requests.get(url, timeout=6)
data = response.json()
pairs = data.get("pairs", [])
if not pairs:
return 0.0, 0.0
best = max(pairs, key=lambda x: x.get("liquidity", {}).get("usd", 0))
price = float(best.get("priceUsd", 0) or 0)
mcap = float(best.get("marketCap", 0) or 0)
return price, mcap
except Exception as e:
print(f"[EXECUTOR] Price fetch error: {e}")
return 0.0, 0.0