import sqlite3 from 'sqlite3' import fetch from 'node-fetch' sqlite3.verbose() const db = new sqlite3.Database('../db') function formatDate(date) { let day = date.getDate() let month = date.getMonth() + 1 let year = date.getFullYear() day = day < 10 ? '0' + day : day month = month < 10 ? '0' + month : month return `${day}-${month}-${year}` } async function fetchAndInsert(stmt, asset) { const assets = { 'TRX': 'tron', 'ETH': 'ethereum' } const date = new Date(Number(new Date()) - 24 * 3600 * 1000) const formattedDate= formatDate(date) const response = await fetch(`https://api.coingecko.com/api/v3/coins/${assets[asset]}/history?date=${formattedDate}`) const data = await response.json() const price = Number(data.market_data.current_price.rub) stmt.run(asset, price, formattedDate) console.log(asset, price, date) } async function main() { const stmt = db.prepare("INSERT INTO assets VALUES (?, ?, ?)") await fetchAndInsert(stmt, 'ETH') await fetchAndInsert(stmt, 'TRX') stmt.finalize() } main()