From ecdc61460a59d20ea2d246d22915af752fed8cbe Mon Sep 17 00:00:00 2001 From: Wizzard Date: Mon, 25 Nov 2024 07:40:52 -0500 Subject: [PATCH] Fix not saving ath correctly --- main.js | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/main.js b/main.js index 98ab62c..c129b71 100644 --- a/main.js +++ b/main.js @@ -29,17 +29,33 @@ function initializeDatabase() { value TEXT NOT NULL )`); - db.run('CREATE INDEX IF NOT EXISTS idx_timestamp ON price_history(timestamp)'); + db.run('CREATE INDEX IF NOT EXISTS idx_timestamp ON price_history(timestamp)', [], (err) => { + if (err) { + console.error('Error creating index:', err); + } else { + initializeBotState(); + } + }); }); } -db.run('INSERT OR REPLACE INTO bot_state (key, value) VALUES (?, ?)', - ['lastAnnouncedPrice', '195'], - (err) => { - if (err) console.error('Error setting initial price:', err); - else console.log('Initial price set successfully'); - } -); +function initializeBotState() { + db.get('SELECT value FROM bot_state WHERE key = ?', ['lastAnnouncedPrice'], (err, row) => { + if (err) { + console.error('Error checking lastAnnouncedPrice:', err); + } else if (!row) { + db.run('INSERT INTO bot_state (key, value) VALUES (?, ?)', + ['lastAnnouncedPrice', '195'], + (err) => { + if (err) console.error('Error setting initial lastAnnouncedPrice:', err); + else console.log('Initial lastAnnouncedPrice set to 195'); + } + ); + } else { + console.log('lastAnnouncedPrice already initialized to', row.value); + } + }); +} async function fetchSolanaPrice() { try {