async function syncTransactionHistory()

in middleware/cron/syncTransactionHistory.js [203:238]


async function syncTransactionHistory() {
    try {
        logger.info('Starting transaction history sync job');
        
        // Get current state
        const lastProcessedId = await getLastProcessedBlockId();
        const totalBlocks = await getTotalBlocks();
        
        logger.info(`Last processed block ID: ${lastProcessedId}, Total blocks: ${totalBlocks}`);
        
        if (lastProcessedId < totalBlocks) {
            const batchSize = BATCH_SIZE;
            let currentStart = lastProcessedId + 1;
            
            // Fetch and store blocks in batches
            while (currentStart <= totalBlocks) {
                const currentEnd = Math.min(currentStart + batchSize - 1, totalBlocks);
                logger.info(`Fetching blocks from ${currentStart} to ${currentEnd}`);
                
                const blocks = await fetchBlockRange(currentStart, currentEnd);
                await storeTransactions(blocks);

                // Add 1 second delay to avoid overwhelming the database or API
                await sleep(1000);
                
                currentStart = currentEnd + 1;
            }
            
            logger.info('Transaction history sync completed successfully');
        } else {
            logger.info('Transaction history is already up to date');
        }
    } catch (error) {
        logger.error('Error syncing transaction history:', error.message);
    }
}