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 {