-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathwebsocket.js
More file actions
763 lines (638 loc) · 24.2 KB
/
websocket.js
File metadata and controls
763 lines (638 loc) · 24.2 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
require("dotenv").config();
const { SolanaTracker } = require("solana-swap");
const { Keypair, PublicKey, Connection } = require("@solana/web3.js");
const { Client, Datastream } = require("@solana-tracker/data-api");
const bs58 = require("bs58");
const winston = require("winston");
const chalk = require("chalk");
const fs = require("fs").promises;
// Utility functions
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const formatUSD = (value) => {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
maximumFractionDigits: 6
}).format(value);
};
const formatPercentage = (value) => {
return `${value >= 0 ? '+' : ''}${value.toFixed(2)}%`;
};
// Logger configuration
const logger = winston.createLogger({
level: "info",
format: winston.format.combine(
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
winston.format.errors({ stack: true })
),
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.printf(
(info) => {
// Only show timestamp for error level
if (info.level.includes('error')) {
return `${info.timestamp} ${info.level}: ${info.message}`;
}
// For other levels, just show the message
return info.message;
}
)
)
}),
new winston.transports.File({
filename: "trading-bot.log",
format: winston.format.combine(
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
winston.format.printf(
(info) => `${info.timestamp} ${info.level}: ${info.message}`
)
)
}),
new winston.transports.File({
filename: "trading-bot-error.log",
level: "error",
format: winston.format.combine(
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
winston.format.printf(
(info) => `${info.timestamp} ${info.level}: ${info.message}`
)
)
}),
],
});
class TradingBot {
constructor() {
// Load and validate configuration
this.config = this.loadConfig();
this.validateConfig();
// Core properties
this.privateKey = process.env.PRIVATE_KEY;
this.SOL_ADDRESS = "So11111111111111111111111111111111111111112";
// State management
this.positions = new Map();
this.soldPositions = [];
this.seenTokens = new Set();
this.buyingTokens = new Set();
this.sellingPositions = new Set();
// File paths
this.positionsFile = "positions.json";
this.soldPositionsFile = "sold_positions.json";
// API clients
this.connection = new Connection(this.config.rpcUrl, {
commitment: 'confirmed',
confirmTransactionInitialTimeout: 60000
});
this.dataClient = new Client({
apiKey: process.env.SOLANA_TRACKER_API_KEY
});
// Initialize Datastream for real-time data
this.dataStream = new Datastream({
wsUrl: process.env.WS_URL,
autoReconnect: true,
reconnectDelay: 2500,
reconnectDelayMax: 10000
});
// Subscriptions storage
this.subscriptions = {
latest: null,
positions: new Map() // tokenAddress -> subscription
};
// Performance tracking
this.stats = {
totalBuys: 0,
totalSells: 0,
successfulBuys: 0,
successfulSells: 0,
totalPnL: 0,
tokensAnalyzed: 0,
startTime: Date.now()
};
}
loadConfig() {
return {
// Trading parameters
amount: parseFloat(process.env.AMOUNT) || 0.01,
monitorInterval: parseInt(process.env.MONITOR_INTERVAL) || 30000,
slippage: parseInt(process.env.SLIPPAGE) || 15,
priorityFee: parseFloat(process.env.PRIORITY_FEE) || 0.00001,
useJito: process.env.JITO === "true",
rpcUrl: process.env.RPC_URL || "https://api.mainnet-beta.solana.com",
// Filter parameters
minLiquidity: parseFloat(process.env.MIN_LIQUIDITY) || 1000,
maxLiquidity: parseFloat(process.env.MAX_LIQUIDITY) || 100000,
minMarketCap: parseFloat(process.env.MIN_MARKET_CAP) || 10000,
maxMarketCap: parseFloat(process.env.MAX_MARKET_CAP) || 1000000,
minRiskScore: parseInt(process.env.MIN_RISK_SCORE) || 0,
maxRiskScore: parseInt(process.env.MAX_RISK_SCORE) || 7,
requireSocialData: process.env.REQUIRE_SOCIAL_DATA === "true",
// PnL parameters
maxNegativePnL: parseFloat(process.env.MAX_NEGATIVE_PNL) || -50,
maxPositivePnL: parseFloat(process.env.MAX_POSITIVE_PNL) || 100,
// Markets
markets: process.env.MARKETS?.split(",").map((m) => m.trim()) ||
['raydium', 'orca', 'pumpfun', 'moonshot', 'raydium-cpmm'],
// Advanced settings
maxPositions: parseInt(process.env.MAX_POSITIONS) || 10,
minHolders: parseInt(process.env.MIN_HOLDERS) || 0,
maxRetries: parseInt(process.env.MAX_RETRIES) || 3,
debug: process.env.DEBUG === "true" || false,
// Datastream specific
subscribeToPriceUpdates: process.env.SUBSCRIBE_PRICE_UPDATES !== "false",
subscribeToTransactions: process.env.SUBSCRIBE_TRANSACTIONS === "true",
};
}
validateConfig() {
const required = ['PRIVATE_KEY', 'SOLANA_TRACKER_API_KEY', 'WS_URL'];
const missing = required.filter(key => !process.env[key]);
if (missing.length > 0) {
throw new Error(`Missing required environment variables: ${missing.join(', ')}`);
}
if (this.config.amount <= 0) {
throw new Error('AMOUNT must be greater than 0');
}
if (this.config.maxNegativePnL > 0) {
throw new Error('MAX_NEGATIVE_PNL must be negative or zero');
}
if (this.config.maxPositivePnL < 0) {
throw new Error('MAX_POSITIVE_PNL must be positive or zero');
}
}
async initialize() {
try {
logger.info("🚀 Initializing Trading Bot with Datastream...");
// Initialize keypair
this.keypair = Keypair.fromSecretKey(
bs58.decode ? bs58.decode(this.privateKey) : bs58.default.decode(this.privateKey)
);
logger.info(`📱 Wallet: ${this.keypair.publicKey.toBase58()}`);
// Initialize Solana swap tracker
this.solanaTracker = new SolanaTracker(this.keypair, this.config.rpcUrl);
// Load saved positions
await this.loadPositions();
await this.loadSoldPositions();
// Display configuration
this.displayConfig();
// Check wallet balance
await this.checkWalletBalance();
// Connect to WebSocket
await this.connectDatastream();
logger.info("✅ Bot initialized successfully");
} catch (error) {
logger.error(`❌ Failed to initialize bot: ${error.message}`);
throw error;
}
}
async connectDatastream() {
logger.info("📡 Connecting to Datastream WebSocket...");
// Set up event handlers
this.dataStream.on('connected', () => {
logger.info("✅ Connected to Datastream");
this.setupSubscriptions();
});
this.dataStream.on('disconnected', () => {
logger.warn("⚠️ Disconnected from Datastream");
});
this.dataStream.on('reconnecting', (attempt) => {
logger.info(`🔄 Reconnecting to Datastream (attempt ${attempt})...`);
});
this.dataStream.on('error', (error) => {
logger.error(`❌ Datastream error: ${error.message}`);
});
// Connect to the WebSocket
await this.dataStream.connect();
}
setupSubscriptions() {
// Subscribe to latest tokens
this.subscriptions.latest = this.dataStream.subscribe.latest().on((tokenData) => {
this.handleNewToken(tokenData);
});
logger.info("📍 Subscribed to latest tokens feed");
// If we have existing positions, subscribe to their price updates
for (const [tokenMint, position] of this.positions) {
this.subscribeToTokenUpdates(tokenMint);
}
}
subscribeToTokenUpdates(tokenAddress) {
// Subscribe to price updates for the token
const priceSubscription = this.dataStream.subscribe.price.token(tokenAddress).on((priceData) => {
this.handlePriceUpdate(tokenAddress, priceData);
});
// Store subscription reference
this.subscriptions.positions.set(tokenAddress, {
price: priceSubscription
});
logger.debug(`📊 Subscribed to price updates for ${tokenAddress}`);
}
unsubscribeFromToken(tokenAddress) {
const subs = this.subscriptions.positions.get(tokenAddress);
if (subs) {
if (subs.price) subs.price.unsubscribe();
this.subscriptions.positions.delete(tokenAddress);
logger.debug(`📴 Unsubscribed from ${tokenAddress}`);
}
}
async handleNewToken(tokenData) {
this.stats.tokensAnalyzed++;
// Check if token passes filters
if (this.filterToken(tokenData)) {
logger.info(`\n✨ New token found: ${tokenData.token.symbol}`);
logger.info(` Name: ${tokenData.token.name}`);
logger.info(` Market Cap: ${formatUSD(tokenData.pools[0].marketCap.usd)}`);
logger.info(` Liquidity: ${formatUSD(tokenData.pools[0].liquidity.usd)}`);
logger.info(` Risk Score: ${tokenData.risk.score}`);
// Attempt to buy
if (!this.positions.has(tokenData.token?.mint) &&
!this.buyingTokens.has(tokenData.token?.mint) &&
this.positions.size < this.config.maxPositions) {
this.buyingTokens.add(tokenData.token.mint);
await this.performSwap(tokenData, true);
}
}
}
async handlePriceUpdate(tokenAddress, priceData) {
const position = this.positions.get(tokenAddress);
if (!position || this.sellingPositions.has(tokenAddress)) return;
const currentPrice = priceData.price;
const pnlPercentage = ((currentPrice - position.entryPrice) / position.entryPrice) * 100;
// Log real-time PnL
if (this.config.debug) {
const emoji = pnlPercentage >= 0 ? "📈" : "📉";
logger.debug(`${emoji} ${position.symbol}: ${formatPercentage(pnlPercentage)} @ ${formatUSD(currentPrice)}`);
}
// Check exit conditions
if (pnlPercentage <= this.config.maxNegativePnL || pnlPercentage >= this.config.maxPositivePnL) {
logger.info(`\n🎯 Exit trigger for ${position.symbol} at ${formatPercentage(pnlPercentage)}`);
// Fetch full token data for selling
try {
const tokenData = await this.dataClient.getTokenInfo(tokenAddress);
if (tokenData) {
this.sellingPositions.add(tokenAddress);
await this.performSwap(tokenData, false);
}
} catch (error) {
logger.error(`Failed to fetch token data for sell: ${error.message}`);
this.sellingPositions.delete(tokenAddress);
}
}
}
filterToken(tokenData) {
try {
// Basic validation
if (!tokenData.pools || tokenData.pools.length === 0) return false;
const pool = tokenData.pools[0];
if (!pool) return false;
// Extract metrics
const liquidity = pool.liquidity?.usd || 0;
const marketCap = pool.marketCap?.usd || 0;
const riskScore = tokenData.risk?.score || 10;
const holders = tokenData.holders || 0;
// Check social requirements
const hasSocialData = !!(
tokenData?.token?.twitter ||
tokenData?.token?.telegram ||
tokenData?.token?.website
);
// Market validation
const isInAllowedMarket = this.config.markets.includes(pool.market);
// Token validation
const isAlreadySeen = this.seenTokens.has(tokenData.token.mint);
const isBeingBought = this.buyingTokens.has(tokenData.token.mint);
const hasPosition = this.positions.has(tokenData.token.mint);
// Apply all filters
const passesFilters =
liquidity >= this.config.minLiquidity &&
liquidity <= this.config.maxLiquidity &&
marketCap >= this.config.minMarketCap &&
marketCap <= this.config.maxMarketCap &&
riskScore >= this.config.minRiskScore &&
riskScore <= this.config.maxRiskScore &&
holders >= this.config.minHolders &&
(!this.config.requireSocialData || hasSocialData) &&
isInAllowedMarket &&
!isAlreadySeen &&
!isBeingBought &&
!hasPosition;
if (!passesFilters && this.config.debug) {
logger.debug(`❌ Token ${tokenData.token.symbol} filtered out`);
}
return passesFilters;
} catch (error) {
logger.error(`Error filtering token: ${error.message}`);
return false;
}
}
displayConfig() {
logger.info("📋 Configuration:");
logger.info(` - Trade Amount: ${this.config.amount} SOL`);
logger.info(` - Markets: ${this.config.markets.join(', ')}`);
logger.info(` - Liquidity Range: ${formatUSD(this.config.minLiquidity)} - ${formatUSD(this.config.maxLiquidity)}`);
logger.info(` - Market Cap Range: ${formatUSD(this.config.minMarketCap)} - ${formatUSD(this.config.maxMarketCap)}`);
logger.info(` - Risk Score Range: ${this.config.minRiskScore} - ${this.config.maxRiskScore}`);
logger.info(` - Stop Loss: ${this.config.maxNegativePnL}%`);
logger.info(` - Take Profit: ${this.config.maxPositivePnL}%`);
logger.info(` - Max Positions: ${this.config.maxPositions}`);
logger.info(` - Real-time Mode: Enabled (WebSocket)`);
}
async checkWalletBalance() {
try {
const balance = await this.connection.getBalance(this.keypair.publicKey);
const solBalance = balance / 1e9;
logger.info(`💰 Wallet Balance: ${solBalance.toFixed(4)} SOL`);
if (solBalance < this.config.amount) {
logger.warn(`⚠️ Insufficient balance for trading. Need at least ${this.config.amount} SOL`);
}
} catch (error) {
logger.error(`Failed to check wallet balance: ${error.message}`);
}
}
async getWalletTokenBalance(wallet, mint, retries = 3) {
for (let attempt = 0; attempt <= retries; attempt++) {
try {
const tokenAccounts = await this.connection.getParsedTokenAccountsByOwner(
new PublicKey(wallet),
{ mint: new PublicKey(mint) }
);
if (tokenAccounts.value && tokenAccounts.value.length > 0) {
const balance = tokenAccounts.value[0].account.data.parsed.info.tokenAmount.uiAmount;
if (balance && balance > 0) {
return balance;
}
}
if (attempt < retries) {
await sleep(2000 * (attempt + 1));
}
} catch (error) {
logger.error(`Attempt ${attempt + 1} failed to get wallet balance: ${error.message}`);
if (attempt < retries) {
await sleep(2000 * (attempt + 1));
}
}
}
return null;
}
async performSwap(token, isBuy) {
const operation = isBuy ? "BUY" : "SELL";
const startTime = Date.now();
logger.info(`\n🔄 [${operation}] Starting swap for ${token.token.symbol}`);
logger.info(` Address: ${token.token.mint}`);
logger.info(` Price: ${formatUSD(token.pools[0].price.usd)}`);
const { amount, slippage, priorityFee } = this.config;
const [fromToken, toToken] = isBuy
? [this.SOL_ADDRESS, token.token.mint]
: [token.token.mint, this.SOL_ADDRESS];
try {
// Determine swap amount
let swapAmount;
if (isBuy) {
swapAmount = amount;
this.stats.totalBuys++;
} else {
const position = this.positions.get(token.token.mint);
if (!position) {
logger.error(`No position found for ${token.token.symbol}`);
return false;
}
swapAmount = position.amount;
this.stats.totalSells++;
}
// Get swap instructions
const swapResponse = await this.solanaTracker.getSwapInstructions(
fromToken,
toToken,
swapAmount,
slippage,
this.keypair.publicKey.toBase58(),
priorityFee
);
// Execute swap
const swapOptions = this.buildSwapOptions();
const txid = await this.solanaTracker.performSwap(swapResponse, swapOptions);
const executionTime = ((Date.now() - startTime) / 1000).toFixed(2);
logger.info(`✅ [${operation}] Transaction sent: ${txid}`);
logger.info(` Execution time: ${executionTime}s`);
// Handle post-swap logic
if (isBuy) {
await this.handleSuccessfulBuy(token, txid);
} else {
await this.handleSuccessfulSell(token, txid);
}
return txid;
} catch (error) {
logger.error(`❌ [${operation}] Swap failed: ${error.message}`);
if (isBuy) {
this.buyingTokens.delete(token.token.mint);
} else {
this.sellingPositions.delete(token.token.mint);
}
return false;
}
}
async handleSuccessfulBuy(token, txid) {
// Wait for confirmation
await sleep(5000);
const tokenAmount = await this.getWalletTokenBalance(
this.keypair.publicKey.toBase58(),
token.token.mint
);
if (!tokenAmount) {
logger.error(`Failed to confirm token balance for ${token.token.symbol}`);
this.buyingTokens.delete(token.token.mint);
return;
}
// Record position
const position = {
txid,
symbol: token.token.symbol,
name: token.token.name,
entryPrice: token.pools[0].price.usd,
amount: tokenAmount,
investment: this.config.amount,
openTime: Date.now(),
market: token.pools[0].market,
riskScore: token.risk.score
};
this.positions.set(token.token.mint, position);
this.seenTokens.add(token.token.mint);
this.buyingTokens.delete(token.token.mint);
this.stats.successfulBuys++;
// Subscribe to real-time updates for this token
this.subscribeToTokenUpdates(token.token.mint);
logger.info(`📈 Position opened for ${token.token.symbol}`);
logger.info(` Amount: ${tokenAmount.toFixed(2)} tokens`);
logger.info(` Entry: ${formatUSD(position.entryPrice)}`);
logger.info(` Value: ${formatUSD(tokenAmount * position.entryPrice)}`);
await this.savePositions();
}
async handleSuccessfulSell(token, txid) {
const position = this.positions.get(token.token.mint);
if (!position) return;
const exitPrice = token.pools[0].price.usd;
const holdTime = Date.now() - position.openTime;
const pnl = (exitPrice - position.entryPrice) * position.amount;
const pnlPercentage = ((exitPrice - position.entryPrice) / position.entryPrice) * 100;
const soldPosition = {
...position,
exitPrice,
pnl,
pnlPercentage,
closeTime: Date.now(),
closeTxid: txid,
holdTime: Math.floor(holdTime / 1000 / 60) // minutes
};
this.soldPositions.push(soldPosition);
this.stats.totalPnL += pnl;
this.stats.successfulSells++;
const profitEmoji = pnl >= 0 ? "🟢" : "🔴";
logger.info(`${profitEmoji} Position closed for ${token.token.symbol}`);
logger.info(` Exit: ${formatUSD(exitPrice)}`);
logger.info(` PnL: ${formatUSD(pnl)} (${formatPercentage(pnlPercentage)})`);
logger.info(` Hold time: ${soldPosition.holdTime} minutes`);
// Unsubscribe from real-time updates
this.unsubscribeFromToken(token.token.mint);
this.positions.delete(token.token.mint);
this.sellingPositions.delete(token.token.mint);
await this.savePositions();
await this.saveSoldPositions();
}
buildSwapOptions() {
return {
sendOptions: { skipPreflight: true },
confirmationRetries: 30,
confirmationRetryTimeout: 1000,
lastValidBlockHeightBuffer: 150,
resendInterval: 1000,
confirmationCheckInterval: 1000,
commitment: "processed",
jito: this.config.useJito ? { enabled: true, tip: 0.0001 } : undefined,
};
}
async statusMonitor() {
logger.info("📊 Starting status monitor...");
while (true) {
try {
this.displayStats();
// Check wallet balance periodically
if (this.positions.size < this.config.maxPositions) {
await this.checkWalletBalance();
}
} catch (error) {
logger.error(`Error in status monitor: ${error.message}`);
}
await sleep(this.config.monitorInterval);
}
}
displayStats() {
const runtime = Math.floor((Date.now() - this.stats.startTime) / 1000 / 60);
const winRate = this.stats.successfulSells > 0
? (this.soldPositions.filter(p => p.pnl > 0).length / this.stats.successfulSells * 100).toFixed(1)
: 0;
logger.info(`\n📊 Bot Statistics (${runtime} minutes runtime):`);
logger.info(` Total PnL: ${formatUSD(this.stats.totalPnL)}`);
logger.info(` Tokens Analyzed: ${this.stats.tokensAnalyzed}`);
logger.info(` Buys: ${this.stats.successfulBuys}/${this.stats.totalBuys}`);
logger.info(` Sells: ${this.stats.successfulSells}/${this.stats.totalSells}`);
logger.info(` Win Rate: ${winRate}%`);
logger.info(` Active Positions: ${this.positions.size}/${this.config.maxPositions}`);
// Show active positions
if (this.positions.size > 0) {
logger.info(` Watching:`);
for (const [mint, position] of this.positions) {
logger.info(` - ${position.symbol}`);
}
}
}
async loadPositions() {
try {
const data = await fs.readFile(this.positionsFile, "utf8");
const loadedPositions = JSON.parse(data);
this.positions = new Map(Object.entries(loadedPositions));
this.seenTokens = new Set(this.positions.keys());
logger.info(`📂 Loaded ${this.positions.size} active positions`);
} catch (error) {
if (error.code !== "ENOENT") {
logger.error(`Error loading positions: ${error.message}`);
}
}
}
async savePositions() {
try {
const positionsObject = Object.fromEntries(this.positions);
await fs.writeFile(
this.positionsFile,
JSON.stringify(positionsObject, null, 2)
);
} catch (error) {
logger.error(`Error saving positions: ${error.message}`);
}
}
async loadSoldPositions() {
try {
const data = await fs.readFile(this.soldPositionsFile, "utf8");
this.soldPositions = JSON.parse(data);
// Calculate total PnL from history
this.stats.totalPnL = this.soldPositions.reduce((sum, pos) => sum + (pos.pnl || 0), 0);
logger.info(`📂 Loaded ${this.soldPositions.length} trade history`);
} catch (error) {
if (error.code !== "ENOENT") {
logger.error(`Error loading sold positions: ${error.message}`);
}
}
}
async saveSoldPositions() {
try {
await fs.writeFile(
this.soldPositionsFile,
JSON.stringify(this.soldPositions, null, 2)
);
} catch (error) {
logger.error(`Error saving sold positions: ${error.message}`);
}
}
async start() {
try {
logger.info(chalk.cyan("\n═══════════════════════════════════════"));
logger.info(chalk.cyan("🤖 Solana Trading Bot v3.0 (Real-time)"));
logger.info(chalk.cyan("═══════════════════════════════════════\n"));
await this.initialize();
logger.info("\n🎯 Starting real-time monitoring...\n");
// Start status monitor (the WebSocket handles token discovery)
await this.statusMonitor();
} catch (error) {
logger.error(`💥 Critical error: ${error.message}`);
process.exit(1);
}
}
// Graceful shutdown
async shutdown() {
logger.info("\n🛑 Shutting down bot...");
// Disconnect from WebSocket
this.dataStream.disconnect();
// Save current state
await this.savePositions();
await this.saveSoldPositions();
// Display final stats
this.displayStats();
logger.info("👋 Bot stopped");
process.exit(0);
}
}
// Handle shutdown signals
process.on('SIGINT', async () => {
if (bot) await bot.shutdown();
});
process.on('SIGTERM', async () => {
if (bot) await bot.shutdown();
});
// Unhandled rejection handler
process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
// Start the bot
const bot = new TradingBot();
bot.start().catch((error) => {
logger.error("Fatal error:", error);
process.exit(1);
});