-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoken-persistence.js
More file actions
94 lines (77 loc) Β· 4.1 KB
/
token-persistence.js
File metadata and controls
94 lines (77 loc) Β· 4.1 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
// Token Persistence Module - Handles managing selected tokens in memory
// Import the console logger to ensure logs reach the command prompt
try {
const consoleLogger = require('./console-logger');
consoleLogger.setup();
console.log('π TOKEN-PERSISTENCE: Console logger initialized');
} catch (err) {
console.error('Error setting up console logger:', err);
}
// Global cache of selected tokens
let cachedSelectedTokens = [];
// Initialize the module
function initTokenPersistence() {
console.log(`π TOKEN-PERSISTENCE: Initializing in-memory token storage`);
// No loading needed since we're only using in-memory storage now
console.log(`πΎ TOKEN-PERSISTENCE: Using in-memory storage only, no file persistence`);
// Check if we have any tokens already in global state
if (window.selectedTickers && Array.isArray(window.selectedTickers) && window.selectedTickers.length > 0) {
console.log(`π TOKEN-PERSISTENCE: Found ${window.selectedTickers.length} tokens in global state, using those`);
cachedSelectedTokens = [...window.selectedTickers];
} else {
console.log(`π TOKEN-PERSISTENCE: No tokens found in global state, starting with empty list`);
cachedSelectedTokens = [];
window.selectedTickers = [];
}
}
// Save selected tokens to in-memory storage
function saveSelectedTokens(tokens) {
console.log(`β‘ TOKEN-PERSISTENCE: saveSelectedTokens called with ${tokens.length} tokens`);
console.log(`π TOKEN-PERSISTENCE: First token (if any):`, tokens.length > 0 ? `${tokens[0].symbol} (${tokens[0].tokenAddress})` : 'none');
// Cache locally
console.log(`π¦ TOKEN-PERSISTENCE: Caching tokens in memory`);
cachedSelectedTokens = [...tokens];
// Update global state
console.log(`π TOKEN-PERSISTENCE: Updating global selectedTickers array`);
window.selectedTickers = [...tokens];
console.log(`πΎ TOKEN-PERSISTENCE: Saved ${tokens.length} tokens to memory`);
return true;
}
// Load selected tokens from memory
function loadSelectedTokens() {
console.log(`β‘ TOKEN-PERSISTENCE: loadSelectedTokens called`);
console.log(`πΎ TOKEN-PERSISTENCE: Using in-memory tokens only`);
// Return the cached tokens
console.log(`π TOKEN-PERSISTENCE: Returning ${cachedSelectedTokens.length} tokens from memory cache`);
// Update token selection UI if it exists
console.log(`πΌοΈ TOKEN-PERSISTENCE: Checking for TokenSelectionUI to update with cached tokens...`);
if (window.TokenSelectionUI && typeof window.TokenSelectionUI.setSelectedTokens === 'function') {
console.log(`β
TOKEN-PERSISTENCE: TokenSelectionUI found, updating selection`);
window.TokenSelectionUI.setSelectedTokens(cachedSelectedTokens);
}
// Also ensure global state is in sync - this is critical for bot execution
console.log(`π TOKEN-PERSISTENCE: Ensuring global selectedTickers is in sync with ${cachedSelectedTokens.length} tokens`);
if (window.selectedTickers) {
window.selectedTickers = [...cachedSelectedTokens];
}
// Log if there's a mismatch between what we think we have and global state
if (window.selectedTickers && window.selectedTickers.length !== cachedSelectedTokens.length) {
console.warn(`β οΈ TOKEN-PERSISTENCE: Mismatch between cache (${cachedSelectedTokens.length}) and global state (${window.selectedTickers.length})`);
}
return cachedSelectedTokens;
}
// Public API
window.TokenPersistence = {
init: initTokenPersistence,
saveTokens: saveSelectedTokens,
loadTokens: loadSelectedTokens,
getTokens: () => cachedSelectedTokens
};
// Initialize token persistence on page load
document.addEventListener('DOMContentLoaded', () => {
console.log(`π TOKEN-PERSISTENCE: DOMContentLoaded fired, initializing token persistence`);
// Initialize through the global interface to ensure window.TokenPersistence is available
window.TokenPersistence.init();
console.log(`β
TOKEN-PERSISTENCE: window.TokenPersistence initialized and available:`,
!!window.TokenPersistence);
});