import { logger } from "../utils/logger"; import { getAllPairs, updatePairLastBackfillTime } from '../service/pair'; import { upsertOrUpdateKlines } from "../service/kline"; import { Client } from '../exchanges/rest'; function getNowMinuteMS() { const minuteMS = 1000 * 60; return Math.floor(Date.now() / minuteMS) * minuteMS } const allPairs = await getAllPairs(); for (const pair of allPairs) { const client = new Client("binance"); let lastBackfillTime = pair.last_backfill_time.getTime(); try { while (lastBackfillTime < getNowMinuteMS()) { console.log('lastBackfillTime', lastBackfillTime); const klines = await client.fetchKlines( pair.symbol, pair.kline_interval, lastBackfillTime, 500 ); console.log(`拉取到 ${klines.length} 条 K 线`); if (klines.length > 0) { await upsertOrUpdateKlines(klines); const lastK = klines[klines.length - 1]; if (lastK) { await updatePairLastBackfillTime(lastK?.symbol, new Date(lastK.openTime)); if (lastBackfillTime === lastK.openTime) { break; } lastBackfillTime = lastK.openTime; } } await new Promise((resolve) => { setTimeout(resolve, Math.random() * 1000); }); } } catch (err) { console.error("拉取失败:", err); } }