This repository was archived by the owner on Feb 6, 2026. It is now read-only.
forked from sollama58/ASDForecast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
998 lines (887 loc) · 47.6 KB
/
Copy pathserver.js
File metadata and controls
998 lines (887 loc) · 47.6 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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
const express = require('express');
const cors = require('cors');
const { Connection, PublicKey, Keypair, Transaction, SystemProgram, sendAndConfirmTransaction, ComputeBudgetProgram } = require('@solana/web3.js');
const axios = require('axios');
const fs = require('fs').promises;
const fsSync = require('fs');
const path = require('path');
const { Mutex } = require('async-mutex');
const rateLimit = require('express-rate-limit');
const app = express();
const stateMutex = new Mutex();
const payoutMutex = new Mutex();
app.set('trust proxy', 1);
app.use(cors({ origin: '*', methods: ['GET', 'POST'] }));
app.use(express.json());
const PORT = process.env.PORT || 3000;
// --- CONFIGURATION ---
const SOLANA_NETWORK = `https://mainnet.helius-rpc.com/?api-key=${process.env.HELIUS_API_KEY}`;
const FEE_WALLET = new PublicKey("5xfyqaDzaj1XNvyz3gnuRJMSNUzGkkMbYbh2bKzWxuan");
const UPKEEP_WALLET = new PublicKey("BH8aAiEDgZGJo6pjh32d5b6KyrNt6zA9U8WTLZShmVXq");
const PYTH_HERMES_URL = "https://hermes.pyth.network/v2/updates/price/latest";
const SOL_FEED_ID = "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d";
const COINGECKO_API_KEY = process.env.COINGECKO_API_KEY || "";
const ASDF_MINT = "9zB5wRarXMj86MymwLumSKA1Dx35zPqqKfcZtK1Spump";
const PRICE_SCALE = 0.1;
const PAYOUT_MULTIPLIER = 0.94;
const FEE_PERCENT = 0.0552;
const UPKEEP_PERCENT = 0.0048;
const FRAME_DURATION = 15 * 60 * 1000;
const BACKEND_VERSION = "142.0"; // UPDATE: Daily Sentiment Persistence & ASDF Fix
const PRIORITY_FEE_UNITS = 50000;
// --- LOGGING ---
const serverLogs = [];
const MAX_LOGS = 100;
function log(message, type = "INFO") {
const timestamp = new Date().toISOString();
const logEntry = `[${timestamp}] [${type}] ${message}`;
console.log(logEntry);
serverLogs.push(logEntry);
if (serverLogs.length > MAX_LOGS) serverLogs.shift();
}
// --- VALIDATION ---
const SOLANA_ADDRESS_REGEX = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
const SIGNATURE_REGEX = /^[1-9A-HJ-NP-Za-km-z]{80,100}$/;
function isValidSolanaAddress(address) { return typeof address === 'string' && SOLANA_ADDRESS_REGEX.test(address); }
function isValidSignature(signature) { return typeof signature === 'string' && SIGNATURE_REGEX.test(signature); }
// --- RATE LIMIT ---
const betLimiter = rateLimit({ windowMs: 1 * 60 * 1000, max: 10, message: { error: "RATE_LIMIT_EXCEEDED" }, standardHeaders: true, legacyHeaders: false });
const stateLimiter = rateLimit({ windowMs: 1 * 60 * 1000, max: 120, message: { error: "POLLING_LIMIT_EXCEEDED" }, standardHeaders: true, legacyHeaders: false });
const voteLimiter = rateLimit({
windowMs: 3600 * 1000, // 1 hour per IP limit (Backup/Safety)
max: 1,
message: { error: "IP_COOLDOWN_ACTIVE" },
standardHeaders: true,
legacyHeaders: false
});
// --- PERSISTENCE ---
const RENDER_DISK_PATH = '/var/data';
const DATA_DIR = fsSync.existsSync(RENDER_DISK_PATH) ? RENDER_DISK_PATH : path.join(__dirname, 'data');
const FRAMES_DIR = path.join(DATA_DIR, 'frames');
const USERS_DIR = path.join(DATA_DIR, 'users');
if (!fsSync.existsSync(DATA_DIR)) fsSync.mkdirSync(DATA_DIR);
if (!fsSync.existsSync(FRAMES_DIR)) fsSync.mkdirSync(FRAMES_DIR);
if (!fsSync.existsSync(USERS_DIR)) fsSync.mkdirSync(USERS_DIR);
const STATE_FILE = path.join(DATA_DIR, 'state.json');
const HISTORY_FILE = path.join(DATA_DIR, 'history.json');
const STATS_FILE = path.join(DATA_DIR, 'global_stats.json');
const SIGS_FILE = path.join(DATA_DIR, 'signatures.log');
const QUEUE_FILE = path.join(DATA_DIR, 'payout_queue.json');
const PAYOUT_HISTORY_FILE = path.join(DATA_DIR, 'payout_history.json');
const PAYOUT_MASTER_LOG = path.join(DATA_DIR, 'payout_master.log');
const FAILED_REFUNDS_FILE = path.join(DATA_DIR, 'failed_refunds.json');
const SENTIMENT_VOTES_FILE = path.join(DATA_DIR, 'sentiment_votes.json'); // NEW: Persistent vote map file
log(`> [SYS] Persistence Root: ${DATA_DIR}`);
async function atomicWrite(filePath, data) {
const tempPath = `${filePath}.tmp`;
try { await fs.writeFile(tempPath, JSON.stringify(data)); await fs.rename(tempPath, filePath); }
catch (e) { log(`> [IO] Write Error on ${filePath}: ${e.message}`, "ERR"); }
}
// --- IMAGES ---
let cachedShareImage = null, cachedItsFineImage = null, cachedItsOverImage = null;
let cachedSentUpImage = null, cachedSentDownImage = null;
let isInitialized = false;
async function cacheImage(url) {
if (!url) return null;
if (url.startsWith('http')) {
try {
const response = await axios.get(url, { responseType: 'arraybuffer' });
const buffer = Buffer.from(response.data, 'binary');
const type = response.headers['content-type'];
return `data:${type};base64,${buffer.toString('base64')}`;
} catch (e) {
log(`> [ERR] Failed to cache image: ${url} - ${e.message}`, "ERR");
return null;
}
}
return url;
}
async function initImages() {
log("> [SYS] Caching Images...");
cachedShareImage = await cacheImage(process.env.BASE_IMAGE_SRC);
cachedItsFineImage = await cacheImage(process.env.ITSFINE_IMG_SRC);
cachedItsOverImage = await cacheImage(process.env.ITSOVER_IMG_SRC);
cachedSentUpImage = await cacheImage(process.env.SENTIMENT_UP_IMG_SRC);
cachedSentDownImage = await cacheImage(process.env.SENTIMENT_DOWN_IMG_SRC);
log("> [SYS] Images Cached.");
}
let houseKeypair;
try {
if (process.env.SOLANA_WALLET_JSON) { houseKeypair = Keypair.fromSecretKey(Uint8Array.from(JSON.parse(process.env.SOLANA_WALLET_JSON))); }
else if (fsSync.existsSync('house-wallet.json')) { houseKeypair = Keypair.fromSecretKey(Uint8Array.from(JSON.parse(fsSync.readFileSync('house-wallet.json')))); }
} catch (e) { log(`> [ERR] Wallet Load Failed: ${e.message}`, "ERR"); }
// --- STATE ---
let gameState = {
price: 0, lastPriceTimestamp: 0, candleOpen: 0, candleStartTime: 0, poolShares: { up: 50, down: 50 }, bets: [],
recentTrades: [], sharePriceHistory: [], isPaused: false, isResetting: false, isCancelled: false,
broadcast: { message: "", isActive: false }, vaultStartBalance: 0, hourlySentiment: { up: 0, down: 0, nextReset: 0 }
};
let historySummary = [];
let globalStats = { totalVolume: 0, totalFees: 0, totalASDF: 0, totalWinnings: 0, totalLifetimeUsers: 0, lastASDFSignature: null };
let processedSignatures = new Set();
let globalLeaderboard = [];
let knownUsers = new Set();
let currentQueueLength = 0;
let payoutHistory = [];
let cachedPublicState = null;
let sentimentVotes = new Map();
let isProcessingQueue = false;
function getNextDayTimestamp() { // MODIFIED: Next Day Reset
const now = new Date();
now.setDate(now.getDate() + 1);
now.setHours(0, 0, 0, 0); // Reset at midnight EST
return now.getTime();
}
function getCurrentWindowStart() {
const now = new Date();
const minutes = Math.floor(now.getMinutes() / 15) * 15;
const start = new Date(now);
start.setMinutes(minutes, 0, 0, 0);
return start.getTime();
}
async function loadAndInit() {
await initImages();
await loadGlobalState();
await updatePrice();
isInitialized = true;
updatePublicStateCache();
log(`> [SYS] Backend Initialization Complete.`, "SYS");
}
function loadGlobalState() {
try {
if (fsSync.existsSync(HISTORY_FILE)) historySummary = JSON.parse(fsSync.readFileSync(HISTORY_FILE));
if (fsSync.existsSync(STATS_FILE)) {
const loaded = JSON.parse(fsSync.readFileSync(STATS_FILE));
globalStats = { ...globalStats, ...loaded };
if(!globalStats.totalWinnings) globalStats.totalWinnings = 0;
if(!globalStats.totalLifetimeUsers) globalStats.totalLifetimeUsers = 0;
}
if (fsSync.existsSync(SIGS_FILE)) {
const fileContent = fsSync.readFileSync(SIGS_FILE, 'utf-8');
const lines = fileContent.split('\n');
lines.slice(-5000).forEach(line => { if(line.trim()) processedSignatures.add(line.trim()); });
}
if (fsSync.existsSync(PAYOUT_HISTORY_FILE)) payoutHistory = JSON.parse(fsSync.readFileSync(PAYOUT_HISTORY_FILE));
// NEW: Load Sentiment Votes Map
if (fsSync.existsSync(SENTIMENT_VOTES_FILE)) {
const voteArray = JSON.parse(fsSync.readFileSync(SENTIMENT_VOTES_FILE));
sentimentVotes = new Map(voteArray);
}
if (fsSync.existsSync(STATE_FILE)) {
const savedState = JSON.parse(fsSync.readFileSync(STATE_FILE));
gameState = { ...gameState, ...savedState };
gameState.isResetting = false;
if (!gameState.broadcast) gameState.broadcast = { message: "", isActive: false };
if (!gameState.vaultStartBalance) gameState.vaultStartBalance = 0;
// Re-init sentiment if missing or incorrectly hourly (now daily)
if (!gameState.hourlySentiment || !gameState.hourlySentiment.nextReset) {
gameState.hourlySentiment = { up: 0, down: 0, nextReset: getNextDayTimestamp() };
}
if (gameState.candleStartTime > 0) {
const currentFrameFile = path.join(FRAMES_DIR, `frame_${gameState.candleStartTime}.json`);
if (fsSync.existsSync(currentFrameFile)) {
const frameData = JSON.parse(fsSync.readFileSync(currentFrameFile));
gameState.bets = frameData.bets || [];
}
}
} else {
gameState.hourlySentiment = { up: 0, down: 0, nextReset: getNextDayTimestamp() };
}
const userFiles = fsSync.readdirSync(USERS_DIR);
userFiles.forEach(f => {
if(f.startsWith('user_') && f.endsWith('.json')) {
knownUsers.add(f.replace('user_','').replace('.json',''));
}
});
globalStats.totalLifetimeUsers = knownUsers.size;
let recalculatedWinnings = 0;
if (historySummary.length > 0) recalculatedWinnings = historySummary.reduce((sum, frame) => sum + (frame.payout || 0), 0);
globalStats.totalWinnings = recalculatedWinnings;
log(`> [SYS] State Loaded.`);
} catch (e) { log(`> [ERR] Load Error: ${e}`, "ERR"); }
}
async function saveSystemState() {
await atomicWrite(STATE_FILE, {
candleOpen: gameState.candleOpen, candleStartTime: gameState.candleStartTime, poolShares: gameState.poolShares, recentTrades: gameState.recentTrades,
sharePriceHistory: gameState.sharePriceHistory, isPaused: gameState.isPaused, isResetting: gameState.isResetting, isCancelled: gameState.isCancelled,
lastPriceTimestamp: gameState.lastPriceTimestamp, broadcast: gameState.broadcast, vaultStartBalance: gameState.vaultStartBalance, hourlySentiment: gameState.hourlySentiment
});
await atomicWrite(STATS_FILE, globalStats);
// NEW: Save Sentiment Votes Map
await atomicWrite(SENTIMENT_VOTES_FILE, Array.from(sentimentVotes.entries()));
}
async function getUser(pubKey) {
if (!isValidSolanaAddress(pubKey)) { return { wins: 0, losses: 0, totalSol: 0, framesPlayed: 0, frameLog: {} }; }
const file = path.join(USERS_DIR, `user_${pubKey}.json`);
try {
const data = await fs.readFile(file, 'utf8');
return JSON.parse(data);
} catch (e) { return { wins: 0, losses: 0, totalSol: 0, framesPlayed: 0, frameLog: {} }; }
}
async function saveUser(pubKey, data) {
if (!isValidSolanaAddress(pubKey)) return;
try {
const file = path.join(USERS_DIR, `user_${pubKey}.json`);
await atomicWrite(file, data);
} catch (e) {}
}
async function updateASDFPurchases() {
const connection = new Connection(SOLANA_NETWORK);
try {
const options = { limit: 20 };
if (globalStats.lastASDFSignature) options.until = globalStats.lastASDFSignature;
const signaturesDetails = await connection.getSignaturesForAddress(FEE_WALLET, options);
if (signaturesDetails.length === 0) return;
globalStats.lastASDFSignature = signaturesDetails[0].signature;
const txs = await connection.getParsedTransactions(signaturesDetails.map(s => s.signature), { maxSupportedTransactionVersion: 0 });
let newPurchasedAmount = 0;
for (const tx of txs) {
if (!tx || !tx.meta) continue;
const preBal = tx.meta.preTokenBalances.find(b => b.mint === ASDF_MINT && b.owner === FEE_WALLET.toString());
const postBal = tx.meta.postTokenBalances.find(b => b.mint === ASDF_MINT && b.owner === FEE_WALLET.toString());
const preAmount = preBal?.uiTokenAmount.uiAmount || 0;
const postAmount = postBal?.uiTokenAmount.uiAmount || 0;
if (postAmount > preAmount) newPurchasedAmount += (postAmount - preAmount);
}
if (newPurchasedAmount > 0) {
globalStats.totalASDF += newPurchasedAmount;
}
} catch (e) { log(`> [ASDF] History Check Failed: ${e.message}`, "ERR"); }
}
async function updateLeaderboard() {
try {
const files = await fs.readdir(USERS_DIR);
const leaders = [];
for (const file of files) {
if (!file.endsWith('.json')) continue;
try {
const raw = await fs.readFile(path.join(USERS_DIR, file), 'utf8');
const data = JSON.parse(raw);
let totalWon = 0;
if (data.frameLog) Object.values(data.frameLog).forEach(log => { if (log.payoutAmount) totalWon += log.payoutAmount; });
const totalGames = data.wins + data.losses;
if (totalGames > 0) leaders.push({ pubKey: file.replace('user_', '').replace('.json', ''), wins: data.wins, bets: totalGames, winRate: (data.wins / totalGames) * 100, totalWon: totalWon });
} catch(e) {}
}
leaders.sort((a, b) => b.totalWon - a.totalWon);
globalLeaderboard = leaders.slice(0, 5);
} catch (e) {}
}
setInterval(updateLeaderboard, 60000);
updateLeaderboard();
async function processPayoutQueue() {
if (isProcessingQueue) return;
isProcessingQueue = true;
const release = await payoutMutex.acquire();
try {
if (!houseKeypair || !fsSync.existsSync(QUEUE_FILE)) { currentQueueLength = 0; return; }
let queueData = [];
try { queueData = JSON.parse(await fs.readFile(QUEUE_FILE, 'utf8')); } catch(e) { queueData = []; }
currentQueueLength = queueData.length;
if (!queueData || queueData.length === 0) return;
log(`> [QUEUE] Processing ${queueData.length} batches...`, "QUEUE");
const connection = new Connection(SOLANA_NETWORK, 'confirmed');
while (queueData.length > 0) {
const batch = queueData[0];
if (typeof batch.retries === 'undefined') batch.retries = 0;
const { type, recipients, frameId } = batch;
try {
const tx = new Transaction();
const priorityFeeIx = ComputeBudgetProgram.setComputeUnitPrice({ microLamports: PRIORITY_FEE_UNITS });
tx.add(priorityFeeIx);
let hasInstructions = false;
const validRecipients = [];
let totalBatchAmount = 0;
if (type === 'USER_PAYOUT' || type === 'USER_REFUND') {
for (const item of recipients) {
const uData = await getUser(item.pubKey);
if (type === 'USER_PAYOUT' && uData.frameLog && uData.frameLog[frameId] && uData.frameLog[frameId].payoutTx) continue;
validRecipients.push(item); totalBatchAmount += (item.amount || 0);
}
} else { recipients.forEach(r => { validRecipients.push(r); totalBatchAmount += (r.amount || 0); }); }
if (validRecipients.length > 0) {
for (const item of validRecipients) { tx.add(SystemProgram.transfer({ fromPubkey: houseKeypair.publicKey, toPubkey: new PublicKey(item.pubKey), lamports: item.amount })); hasInstructions = true; }
if (hasInstructions) {
const sig = await sendAndConfirmTransaction(connection, tx, [houseKeypair], { commitment: 'confirmed', maxRetries: 5 });
log(`> [TX] Batch Sent (${type}): ${sig}`, "TX");
const historyRecord = { timestamp: Date.now(), frameId: frameId || 'N/A', type: type, signature: sig, recipientCount: validRecipients.length, totalAmount: (totalBatchAmount / 1e9).toFixed(4) };
payoutHistory.unshift(historyRecord);
if (payoutHistory.length > 100) payoutHistory.pop();
await atomicWrite(PAYOUT_HISTORY_FILE, payoutHistory);
await fs.appendFile(PAYOUT_MASTER_LOG, JSON.stringify(historyRecord) + '\n');
if (type === 'USER_PAYOUT') {
for (const item of validRecipients) {
const uData = await getUser(item.pubKey);
if (uData.frameLog && uData.frameLog[frameId]) { uData.frameLog[frameId].payoutTx = sig; uData.frameLog[frameId].payoutAmount = item.amount / 1e9; await saveUser(item.pubKey, uData); }
}
} else if (type === 'USER_REFUND') {
for (const item of validRecipients) {
const uData = await getUser(item.pubKey);
if (!uData.frameLog[frameId]) uData.frameLog[frameId] = { result: 'CANCELLED', time: Date.now() };
uData.frameLog[frameId].refundTx = sig;
await saveUser(item.pubKey, uData);
}
}
}
}
queueData.shift(); await atomicWrite(QUEUE_FILE, queueData); currentQueueLength = queueData.length;
} catch (e) {
log(`> [TX] Batch Failed: ${e.message}`, "ERR");
batch.retries = (batch.retries || 0) + 1;
if (batch.retries >= 5) {
log(`> [QUEUE] Batch failed 5 times. Decomposing or Logging Failure.`, "ERR");
queueData.shift();
if (batch.recipients.length > 1) {
for (const item of batch.recipients) { queueData.push({ type: 'USER_REFUND', frameId: batch.frameId, recipients: [item], retries: 0 }); }
} else {
const failedRecord = { timestamp: Date.now(), batch: batch, error: e.message };
await fs.appendFile(FAILED_REFUNDS_FILE, JSON.stringify(failedRecord) + '\n');
}
await atomicWrite(QUEUE_FILE, queueData);
} else {
await atomicWrite(QUEUE_FILE, queueData);
break;
}
currentQueueLength = queueData.length;
}
}
} catch (e) { log(`> [QUEUE] Error: ${e}`, "ERR"); }
finally { release(); isProcessingQueue = false; }
}
setInterval(processPayoutQueue, 20000);
async function queuePayouts(frameId, result, bets, totalVolume) {
log(`> [PAYOUT] Queuing payouts for Frame ${frameId}. Result: ${result}, Vol: ${totalVolume}`, "PAYOUT");
if (totalVolume === 0) return;
const queue = [];
if (result === "FLAT") {
const burnLamports = Math.floor((totalVolume * 0.99) * 1e9);
if (burnLamports > 0) queue.push({ type: 'FEE', recipients: [{ pubKey: FEE_WALLET.toString(), amount: burnLamports }], retries: 0 });
} else {
const feeLamports = Math.floor((totalVolume * FEE_PERCENT) * 1e9);
const upkeepLamports = Math.floor((totalVolume * UPKEEP_PERCENT) * 1e9);
if (feeLamports > 0) queue.push({ type: 'FEE', recipients: [{ pubKey: FEE_WALLET.toString(), amount: feeLamports }], retries: 0 });
if (upkeepLamports > 0) queue.push({ type: 'FEE', recipients: [{ pubKey: UPKEEP_WALLET.toString(), amount: upkeepLamports }], retries: 0 });
}
if (result !== "FLAT") {
const potLamports = Math.floor((totalVolume * 0.94) * 1e9);
const userPositions = {};
bets.forEach(bet => {
if (!userPositions[bet.user]) userPositions[bet.user] = { up: 0, down: 0 };
if (bet.direction === 'UP') userPositions[bet.user].up += bet.shares;
else userPositions[bet.user].down += bet.shares;
});
let totalWinningShares = 0;
const eligibleWinners = [];
for (const [pubKey, pos] of Object.entries(userPositions)) {
const rUp = Math.round(pos.up * 10000) / 10000;
const rDown = Math.round(pos.down * 10000) / 10000;
let userDir = "FLAT";
if (rUp > rDown) userDir = "UP";
else if (rDown > rUp) userDir = "DOWN";
if (pos.up === pos.down) userDir = "FLAT";
if (userDir === result) {
const sharesHeld = result === "UP" ? pos.up : pos.down;
totalWinningShares += sharesHeld;
eligibleWinners.push({ pubKey, sharesHeld });
}
}
if (totalWinningShares > 0) {
const BATCH_SIZE = 12;
for (let i = 0; i < eligibleWinners.length; i += BATCH_SIZE) {
const batchWinners = eligibleWinners.slice(i, i + BATCH_SIZE);
const batchRecipients = [];
for (const winner of batchWinners) {
const shareRatio = winner.sharesHeld / totalWinningShares;
const payoutLamports = Math.floor(potLamports * shareRatio);
if (payoutLamports > 5000) batchRecipients.push({ pubKey: winner.pubKey, amount: payoutLamports });
}
if (batchRecipients.length > 0) queue.push({ type: 'USER_PAYOUT', frameId: frameId, recipients: batchRecipients, retries: 0 });
}
}
}
const release = await payoutMutex.acquire();
try {
let existingQueue = [];
if (fsSync.existsSync(QUEUE_FILE)) { try { existingQueue = JSON.parse(await fs.readFile(QUEUE_FILE, 'utf8')); } catch(e) {} }
const newQueue = existingQueue.concat(queue);
await atomicWrite(QUEUE_FILE, newQueue);
currentQueueLength = newQueue.length;
} finally { release(); }
processPayoutQueue();
}
async function processRefunds(frameId, bets) {
log(`> [REFUND] Processing refunds for Frame ${frameId}. Count: ${bets.length}`, "REFUND");
if (!houseKeypair || bets.length === 0) return;
const refunds = {};
let totalFee = 0;
bets.forEach(bet => {
if (!refunds[bet.user]) refunds[bet.user] = 0;
const refundAmt = Math.floor(bet.costSol * 0.99 * 1e9);
refunds[bet.user] += refundAmt;
totalFee += (bet.costSol * 0.01 * 1e9);
});
const queue = [];
if (totalFee > 0) queue.push({ type: 'FEE', recipients: [{ pubKey: FEE_WALLET.toString(), amount: Math.floor(totalFee) }], retries: 0 });
const recipients = Object.entries(refunds).map(([pub, amt]) => ({ pubKey: pub, amount: amt }));
const BATCH_SIZE = 12;
for (let i = 0; i < recipients.length; i += BATCH_SIZE) {
const batch = recipients.slice(i, i + BATCH_SIZE);
queue.push({ type: 'USER_REFUND', frameId: frameId, recipients: batch, retries: 0 });
}
const release = await payoutMutex.acquire();
try {
let existingQueue = [];
if (fsSync.existsSync(QUEUE_FILE)) { try { existingQueue = JSON.parse(await fs.readFile(QUEUE_FILE, 'utf8')); } catch(e) {} }
const newQueue = existingQueue.concat(queue);
await atomicWrite(QUEUE_FILE, newQueue);
currentQueueLength = newQueue.length;
} finally { release(); }
processPayoutQueue();
}
async function cancelCurrentFrameAndRefund() {
gameState.isPaused = true;
gameState.isCancelled = true;
if (gameState.bets.length > 0) {
log(`> [CANCEL] Performing Cancellation & Refund for frame ${gameState.candleStartTime}...`, "CANCEL");
const betsToRefund = [...gameState.bets];
const frameRecord = { id: gameState.candleStartTime, time: new Date(gameState.candleStartTime).toISOString(), result: "CANCELLED", totalSol: 0, winners: 0, payout: 0 };
historySummary.unshift(frameRecord);
if (historySummary.length > 100) historySummary = historySummary.slice(0, 100);
await atomicWrite(HISTORY_FILE, historySummary);
gameState.bets = [];
gameState.poolShares = { up: 50, down: 50 };
processRefunds(gameState.candleStartTime, betsToRefund);
}
await saveSystemState();
}
async function closeFrame(closePrice, closeTime) {
try {
const frameId = gameState.candleStartTime;
log(`> [SYS] Closing Frame: ${frameId}`);
// 1. GET VAULT CLOSING BALANCE
let vaultEndBalance = 0;
if (houseKeypair) {
try {
const connection = new Connection(SOLANA_NETWORK);
vaultEndBalance = (await connection.getBalance(houseKeypair.publicKey)) / 1e9;
} catch(e) { log(`> [ERR] Failed to fetch vault close balance: ${e.message}`, "WARN"); }
}
await updateASDFPurchases();
const openPrice = gameState.candleOpen;
let result = "FLAT";
if (closePrice > openPrice) result = "UP";
else if (closePrice < openPrice) result = "DOWN";
const realSharesUp = Math.max(0, gameState.poolShares.up - 50);
const realSharesDown = Math.max(0, gameState.poolShares.down - 50);
const frameSol = gameState.bets.reduce((acc, bet) => acc + bet.costSol, 0);
let feeAmt = 0;
let upkeepAmt = 0;
if (result === "FLAT") {
feeAmt = frameSol * 0.99;
} else {
feeAmt = frameSol * FEE_PERCENT;
upkeepAmt = frameSol * UPKEEP_PERCENT;
}
globalStats.totalFees += feeAmt;
let winnerCount = 0;
let payoutTotal = 0;
// FIX: userPositions hoisted to avoid ReferenceError below
const userPositions = {};
if (result !== "FLAT") {
const potSol = frameSol * 0.94;
gameState.bets.forEach(bet => {
if (!userPositions[bet.user]) userPositions[bet.user] = { up: 0, down: 0 };
if (bet.direction === 'UP') userPositions[bet.user].up += bet.shares;
else userPositions[bet.user].down += bet.shares;
});
for (const [pk, pos] of Object.entries(userPositions)) {
const rUp = Math.round(pos.up * 10000) / 10000;
const rDown = Math.round(pos.down * 10000) / 10000;
let dir = "FLAT";
if (rUp > rDown) dir = "UP";
else if (rDown > rUp) dir = "DOWN";
if (rUp === rDown) dir = "FLAT";
if (dir === result) winnerCount++;
}
if (winnerCount > 0) {
payoutTotal = potSol;
globalStats.totalWinnings += payoutTotal;
}
}
const uniqueUsers = new Set(gameState.bets.map(b => b.user)).size;
const frameRecord = {
id: frameId, startTime: frameId, endTime: frameId + FRAME_DURATION,
time: new Date(frameId).toISOString(), open: openPrice, close: closePrice,
result: result, sharesUp: realSharesUp, sharesDown: realSharesDown,
totalSol: frameSol, winners: winnerCount, payout: payoutTotal,
fee: feeAmt, upkeep: upkeepAmt, users: uniqueUsers, vStart: gameState.vaultStartBalance, vEnd: vaultEndBalance
};
historySummary.unshift(frameRecord);
if (historySummary.length > 100) historySummary = historySummary.slice(0, 100);
await atomicWrite(HISTORY_FILE, historySummary);
const betsSnapshot = [...gameState.bets];
const usersToUpdate = Object.entries(userPositions);
const USER_IO_BATCH_SIZE = 20;
for (let i = 0; i < usersToUpdate.length; i += USER_IO_BATCH_SIZE) {
const batch = usersToUpdate.slice(i, i + USER_IO_BATCH_SIZE);
await Promise.all(batch.map(async ([pubKey, pos]) => {
const userData = await getUser(pubKey);
userData.framesPlayed += 1;
const rUp = Math.round(pos.up * 10000) / 10000;
const rDown = Math.round(pos.down * 10000) / 10000;
let userDir = "FLAT";
if (rUp > rDown) userDir = "UP";
else if (rDown > rUp) userDir = "DOWN";
if (rUp === rDown) userDir = "FLAT";
const outcome = (userDir !== "FLAT" && result !== "FLAT" && userDir === result) ? "WIN" : "LOSS";
if (outcome === "WIN") userData.wins += 1; else if (outcome === "LOSS") userData.losses += 1;
if (!userData.frameLog) userData.frameLog = {};
userData.frameLog[frameId] = {
dir: userDir, result: outcome, time: Date.now(),
upShares: pos.up, downShares: pos.down, wagered: pos.sol
};
await saveUser(pubKey, userData);
}));
}
processedSignatures.clear();
await fs.writeFile(SIGS_FILE, '');
updatePublicStateCache();
gameState.candleStartTime = closeTime;
gameState.candleOpen = closePrice;
gameState.poolShares = { up: 50, down: 50 };
gameState.bets = [];
gameState.sharePriceHistory = [];
gameState.isResetting = false;
gameState.vaultStartBalance = vaultEndBalance;
await saveSystemState();
await queuePayouts(frameId, result, betsSnapshot, frameSol);
} catch(e) {
log(`> [ERR] CloseFrame Failed: ${e.message}`, "ERR");
gameState.isResetting = false;
}
}
async function updatePrice() {
let fetchedPrice = 0;
let priceFound = false;
try {
const response = await axios.get(`${PYTH_HERMES_URL}?ids[]=${SOL_FEED_ID}`, { timeout: 2000 });
if (response.data && response.data.parsed && response.data.parsed[0]) {
const p = response.data.parsed[0].price;
fetchedPrice = Number(p.price) * Math.pow(10, p.expo);
priceFound = true;
}
} catch (e) { console.log(`[ORACLE] Pyth Failed: ${e.message}`); }
if (!priceFound && COINGECKO_API_KEY) {
try {
const response = await axios.get('https://api.coingecko.com/api/v3/simple/price', {
params: { ids: 'solana', vs_currencies: 'usd', x_cg_demo_api_key: COINGECKO_API_KEY },
timeout: 4000
});
if (response.data.solana) {
fetchedPrice = response.data.solana.usd;
priceFound = true;
}
} catch (e) { console.log(`[ORACLE] CoinGecko Failed: ${e.message}`); }
}
if (priceFound) {
await stateMutex.runExclusive(async () => {
gameState.price = fetchedPrice;
gameState.lastPriceTimestamp = Date.now();
const currentWindowStart = getCurrentWindowStart();
if (Date.now() >= gameState.hourlySentiment.nextReset) {
log("> [SENTIMENT] Daily reset triggered. Clearing votes.", "SENTIMENT");
gameState.hourlySentiment.up = 0;
gameState.hourlySentiment.down = 0;
gameState.hourlySentiment.nextReset = getNextDayTimestamp();
sentimentVotes.clear();
await saveSystemState();
}
const frameEndTime = gameState.candleStartTime + FRAME_DURATION;
if (gameState.isResetting && Date.now() > (frameEndTime + 60000)) {
log("⚠️ [FAILSAFE] RESETTING stuck > 1m. Refunds issued. Starting new frame.", "FAILSAFE");
await cancelCurrentFrameAndRefund();
gameState.isResetting = false;
gameState.isPaused = false;
gameState.isCancelled = false;
gameState.candleStartTime = currentWindowStart;
gameState.candleOpen = fetchedPrice;
gameState.poolShares = { up: 50, down: 50 };
gameState.bets = [];
gameState.sharePriceHistory = [];
await saveSystemState();
updatePublicStateCache();
return;
}
if (gameState.isResetting) return;
if (gameState.isPaused) {
const timeRemaining = (gameState.candleStartTime + FRAME_DURATION) - Date.now();
if (!gameState.isCancelled && timeRemaining < 300000 && timeRemaining > 0) {
log("⚠️ [AUTO] Auto-cancelling due to prolonged pause...", "AUTO");
await cancelCurrentFrameAndRefund();
return;
}
if (currentWindowStart > gameState.candleStartTime) {
log("> [SYS] Unpausing for new Frame.");
if (!gameState.isCancelled) {
const skippedFrameRecord = {
id: gameState.candleStartTime,
startTime: gameState.candleStartTime,
endTime: gameState.candleStartTime + FRAME_DURATION,
time: new Date(gameState.candleStartTime).toISOString(),
open: gameState.candleOpen,
close: fetchedPrice,
result: "PAUSED",
sharesUp: 0, sharesDown: 0, totalSol: 0, winners: 0, payout: 0
};
historySummary.unshift(skippedFrameRecord);
if(historySummary.length > 100) historySummary = historySummary.slice(0,100);
await atomicWrite(HISTORY_FILE, historySummary);
gameState.isPaused = false;
} else {
gameState.isCancelled = false;
gameState.isPaused = false;
}
gameState.candleStartTime = currentWindowStart;
gameState.candleOpen = fetchedPrice;
await saveSystemState();
}
return;
}
if (currentWindowStart > gameState.candleStartTime) {
if (gameState.candleStartTime === 0) {
gameState.candleStartTime = currentWindowStart;
gameState.candleOpen = fetchedPrice;
await saveSystemState();
} else {
log(`> [SYS] Closing Frame: ${gameState.candleStartTime} @ $${fetchedPrice}`);
gameState.isResetting = true;
await saveSystemState();
await closeFrame(fetchedPrice, currentWindowStart);
}
}
const totalS = gameState.poolShares.up + gameState.poolShares.down;
const pUp = (gameState.poolShares.up / totalS) * PRICE_SCALE;
const pDown = (gameState.poolShares.down / totalS) * PRICE_SCALE;
if (!gameState.sharePriceHistory) gameState.sharePriceHistory = [];
gameState.sharePriceHistory.push({ t: Date.now(), up: pUp, down: pDown });
const FIVE_MINS = 5 * 60 * 1000;
gameState.sharePriceHistory = gameState.sharePriceHistory.filter(x => x.t > Date.now() - FIVE_MINS);
await saveSystemState();
});
}
}
setInterval(updatePrice, 10000);
updatePrice();
// --- CACHE GENERATION ---
function updatePublicStateCache() {
if (!isInitialized) return;
const now = Date.now();
const priceChange = gameState.price - gameState.candleOpen;
const percentChange = gameState.candleOpen ? (priceChange / gameState.candleOpen) * 100 : 0;
const currentVolume = gameState.bets.reduce((acc, b) => acc + b.costSol, 0);
const totalShares = gameState.poolShares.up + gameState.poolShares.down;
const priceUp = (gameState.poolShares.up / totalShares) * PRICE_SCALE;
const priceDown = (gameState.poolShares.down / totalShares) * PRICE_SCALE;
const history = gameState.sharePriceHistory || [];
const baseline = { up: 0.05, down: 0.05 };
const oneMinAgo = history.find(x => x.t >= now - 60000) || baseline;
const fiveMinAgo = history.find(x => x.t >= now - 300000) || baseline;
function getPercentChange(current, old) { if (!old || old === 0) return 0; return ((current - old) / old) * 100; }
const changes = {
up1m: getPercentChange(priceUp, oneMinAgo.up),
up5m: getPercentChange(priceUp, fiveMinAgo.up),
down1m: getPercentChange(priceDown, oneMinAgo.down),
down5m: getPercentChange(priceDown, fiveMinAgo.down),
};
const uniqueUsers = new Set(gameState.bets.map(b => b.user)).size;
const lastFramePot = historySummary.length > 0 ? historySummary[0].totalSol : 0;
cachedPublicState = {
price: gameState.price,
openPrice: gameState.candleOpen,
candleStartTime: gameState.candleStartTime,
candleEndTime: gameState.candleStartTime + FRAME_DURATION,
change: percentChange,
currentVolume: currentVolume,
uniqueUsers: uniqueUsers,
platformStats: globalStats,
isPaused: gameState.isPaused,
isResetting: gameState.isResetting,
isCancelled: gameState.isCancelled,
broadcast: gameState.broadcast,
market: { priceUp, priceDown, sharesUp: gameState.poolShares.up, sharesDown: gameState.poolShares.down, changes },
history: historySummary,
recentTrades: gameState.recentTrades,
leaderboard: globalLeaderboard,
lastPriceTimestamp: gameState.lastPriceTimestamp,
backendVersion: BACKEND_VERSION,
payoutQueueLength: currentQueueLength,
lastFramePot: lastFramePot,
totalLifetimeUsers: globalStats.totalLifetimeUsers,
totalWinnings: globalStats.totalWinnings,
hourlySentiment: gameState.hourlySentiment
};
}
setInterval(updatePublicStateCache, 500);
// --- ENDPOINTS ---
app.get('/api/state', stateLimiter, async (req, res) => {
if (!isInitialized || !cachedPublicState) return res.status(503).json({ error: "SERVICE_UNAVAILABLE", reason: "Backend cache initializing." });
const response = { ...cachedPublicState };
const now = new Date();
const nextWindowStart = getCurrentWindowStart() + FRAME_DURATION;
response.msUntilClose = nextWindowStart - now.getTime();
const userKey = req.query.user;
if (userKey && isValidSolanaAddress(userKey)) {
const myStats = await getUser(userKey);
response.userStats = myStats;
const userBets = gameState.bets.filter(b => b.user === userKey);
response.activePosition = {
upShares: userBets.filter(b => b.direction === 'UP').reduce((a, b) => a + b.shares, 0),
downShares: userBets.filter(b => b.direction === 'DOWN').reduce((a, b) => a + b.shares, 0),
upSol: userBets.filter(b => b.direction === 'UP').reduce((a, b) => a + b.costSol, 0),
downSol: userBets.filter(b => b.direction === 'DOWN').reduce((a, b) => a + b.costSol, 0),
wageredSol: userBets.reduce((a, b) => a + b.costSol, 0)
};
}
res.json(response);
});
app.get('/api/image/fine', (req, res) => { res.json({ image: cachedItsFineImage }); });
app.get('/api/image/over', (req, res) => { res.json({ image: cachedItsOverImage }); });
app.get('/api/share-image', (req, res) => { res.json({ image: cachedShareImage }); });
app.get('/api/image/sentiment-up', (req, res) => { res.json({ image: cachedSentUpImage }); });
app.get('/api/image/sentiment-down', (req, res) => { res.json({ image: cachedSentDownImage }); });
app.post('/api/sentiment/vote', voteLimiter, async (req, res) => {
const { direction, userPubKey } = req.body;
if (!['UP', 'DOWN'].includes(direction)) return res.status(400).json({ error: "Invalid Direction" });
if (!userPubKey || !isValidSolanaAddress(userPubKey)) return res.status(400).json({ error: "Invalid Wallet" });
const release = await stateMutex.acquire();
try {
const now = Date.now();
const lastVoteTime = sentimentVotes.get(userPubKey) || 0;
const COOLDOWN_MS = 60 * 60 * 1000; // 1 Hour
if (now - lastVoteTime < COOLDOWN_MS) {
return res.status(429).json({ error: "VOTE_COOLDOWN", nextVote: lastVoteTime + COOLDOWN_MS });
}
if (direction === 'UP') gameState.hourlySentiment.up++;
else gameState.hourlySentiment.down++;
sentimentVotes.set(userPubKey, now);
await saveSystemState();
updatePublicStateCache();
log(`> [SENTIMENT] Vote: ${direction} from ${userPubKey.slice(0,6)}`, "SENT");
res.json({ success: true, sentiment: gameState.hourlySentiment });
} finally { release(); }
});
app.get('/api/admin/logs', (req, res) => {
const auth = req.headers['x-admin-secret'];
if (!auth || auth !== process.env.ADMIN_ACTION_PASSWORD) return res.status(403).json({ error: "UNAUTHORIZED" });
res.json({ logs: serverLogs });
});
app.get('/api/admin/payouts', async (req, res) => {
const auth = req.headers['x-admin-secret'];
if (!auth || auth !== process.env.ADMIN_ACTION_PASSWORD) return res.status(403).json({ error: "UNAUTHORIZED" });
let currentQueue = [];
if (fsSync.existsSync(QUEUE_FILE)) { try { currentQueue = JSON.parse(fsSync.readFileSync(QUEUE_FILE, 'utf8')); } catch(e) {} }
res.json({ queue: currentQueue, history: payoutHistory });
});
app.get('/api/admin/full-history', async (req, res) => {
const auth = req.headers['x-admin-secret'];
if (!auth || auth !== process.env.ADMIN_ACTION_PASSWORD) return res.status(403).json({ error: "UNAUTHORIZED" });
res.json({ history: historySummary });
});
app.post('/api/verify-bet', betLimiter, async (req, res) => {
if (!isInitialized) return res.status(503).json({ error: "SERVICE_UNAVAILABLE" });
if (gameState.isPaused) return res.status(400).json({ error: "MARKET_PAUSED" });
if (gameState.isResetting) return res.status(503).json({ error: "CALCULATING_RESULTS" });
if (gameState.isCancelled) return res.status(400).json({ error: "MARKET_CANCELLED" });
const { signature, direction, userPubKey } = req.body;
if (!signature || !userPubKey || !isValidSolanaAddress(userPubKey) || !isValidSignature(signature)) return res.status(400).json({ error: "INVALID_DATA_FORMAT" });
if (processedSignatures.has(signature)) return res.status(400).json({ error: "DUPLICATE_TX_DETECTED" });
let solAmount = 0;
let txBlockTime = 0;
try {
const connection = new Connection(SOLANA_NETWORK, 'confirmed');
const tx = await connection.getParsedTransaction(signature, { commitment: 'confirmed', maxSupportedTransactionVersion: 0 });
if (!tx || tx.meta.err) return res.status(400).json({ error: "TX_INVALID" });
txBlockTime = tx.blockTime * 1000;
let housePubKeyStr = houseKeypair ? houseKeypair.publicKey.toString() : "BXSp5y6Ua6tB5fZDe1EscVaaEaZLg1yqzrsPqAXhKJYy";
const instructions = tx.transaction.message.instructions;
for (let ix of instructions) {
if (ix.program === 'system' && ix.parsed.type === 'transfer') {
if (ix.parsed.info.destination === housePubKeyStr) {
solAmount = ix.parsed.info.lamports / 1000000000;
break;
}
}
}
if (solAmount <= 0) return res.status(400).json({ error: "NO_FUNDS" });
} catch (e) { return res.status(500).json({ error: "CHAIN_ERROR" }); }
const release = await stateMutex.acquire();
try {
if (processedSignatures.has(signature)) return res.status(400).json({ error: "DUPLICATE_TX_DETECTED" });
if (txBlockTime < gameState.candleStartTime) return res.status(400).json({ error: "TX_TIMESTAMP_EXPIRED" });
if (gameState.isPaused) return res.status(400).json({ error: "MARKET_PAUSED" });
if (gameState.isResetting) return res.status(503).json({ error: "CALCULATING_RESULTS" });
const totalShares = gameState.poolShares.up + gameState.poolShares.down;
let price = 0.05;
if (direction === 'UP') price = (gameState.poolShares.up / totalShares) * PRICE_SCALE;
else price = (gameState.poolShares.down / totalShares) * PRICE_SCALE;
if(price < 0.001) price = 0.001;
const sharesReceived = solAmount / price;
if (!Number.isFinite(sharesReceived) || sharesReceived <= 0) return res.status(500).json({ error: "CALCULATION_ERROR" });
if (direction === 'UP') gameState.poolShares.up += sharesReceived;
else gameState.poolShares.down += sharesReceived;
if (!knownUsers.has(userPubKey)) { knownUsers.add(userPubKey); globalStats.totalLifetimeUsers++; }
getUser(userPubKey).then(userData => { userData.totalSol += solAmount; saveUser(userPubKey, userData); });
gameState.bets.push({ signature, user: userPubKey, direction, costSol: solAmount, entryPrice: price, shares: sharesReceived, timestamp: Date.now() });
gameState.recentTrades.unshift({ user: userPubKey, direction, shares: sharesReceived, time: Date.now() });
if (gameState.recentTrades.length > 20) gameState.recentTrades.pop();
globalStats.totalVolume += solAmount;
processedSignatures.add(signature);
await fs.appendFile(path.join(DATA_DIR, 'signatures.log'), signature + '\n');
updatePublicStateCache();
await saveSystemState();
log(`> [TRADE] ${userPubKey.slice(0,6)} bought ${sharesReceived.toFixed(2)} ${direction} for ${solAmount} SOL`, "TRADE");
res.json({ success: true, shares: sharesReceived, price: price });
} catch (e) {
log(`> [ERR] Trade Error: ${e}`, "ERR");
res.status(500).json({ error: "STATE_ERROR" });
} finally { release(); }
});
app.post('/api/admin/toggle-pause', async (req, res) => {
const auth = req.headers['x-admin-secret'];
if (!auth || auth !== process.env.ADMIN_ACTION_PASSWORD) return res.status(403).json({ error: "UNAUTHORIZED" });
const release = await stateMutex.acquire();
try {
gameState.isPaused = !gameState.isPaused;
if (!gameState.isPaused) gameState.isCancelled = false;
await saveSystemState();
log(`> [ADMIN] Market Paused State toggled to: ${gameState.isPaused}`, "ADMIN");
res.json({ success: true, isPaused: gameState.isPaused });
} catch (e) { log(`> [ERR] Admin Pause Error: ${e}`, "ERR"); res.status(500).json({ error: "ADMIN_ERROR" }); }
finally { release(); }
});
app.post('/api/admin/cancel-frame', async (req, res) => {
const auth = req.headers['x-admin-secret'];
if (!auth || auth !== process.env.ADMIN_ACTION_PASSWORD) return res.status(403).json({ error: "UNAUTHORIZED" });
const release = await stateMutex.acquire();
try {
await cancelCurrentFrameAndRefund();
log(`> [ADMIN] Frame Cancelled by Admin`, "ADMIN");
res.json({ success: true, message: "Frame Cancelled. Market Paused." });
} catch (e) { log(`> [ERR] Admin Cancel Error: ${e}`, "ERR"); res.status(500).json({ error: "ADMIN_ERROR" }); }
finally { release(); }
});
app.post('/api/admin/broadcast', async (req, res) => {
const auth = req.headers['x-admin-secret'];
if (!auth || auth !== process.env.ADMIN_ACTION_PASSWORD) return res.status(403).json({ error: "UNAUTHORIZED" });
const { message, isActive } = req.body;
const release = await stateMutex.acquire();
try {
gameState.broadcast = { message: message || "", isActive: !!isActive };
await saveSystemState();
updatePublicStateCache();
log(`> [ADMIN] Broadcast updated: "${message}" (Active: ${isActive})`, "ADMIN");
res.json({ success: true, broadcast: gameState.broadcast });
} catch (e) { log(`> [ERR] Admin Broadcast Error: ${e}`, "ERR"); res.status(500).json({ error: "ADMIN_ERROR" }); }
finally { release(); }
});
app.listen(PORT, () => { log(`> ASDForecast Engine v${BACKEND_VERSION} running on ${PORT}`, "SYS"); loadAndInit(); });