From 8e4313882354e7b2cb65bd54d1002a2b898df137 Mon Sep 17 00:00:00 2001 From: ali ebrahimi Date: Tue, 22 Oct 2024 02:52:32 +0330 Subject: [PATCH] Add sync data with indexer cronjob --- src/scripts/runScript.ts | 10 ++ src/scripts/syncDataWithInverter.ts | 100 ++++++++---------- src/server/bootstrap.ts | 11 ++ src/services/cronJobs/syncDataWithInverter.ts | 19 ++++ 4 files changed, 85 insertions(+), 55 deletions(-) create mode 100644 src/services/cronJobs/syncDataWithInverter.ts diff --git a/src/scripts/runScript.ts b/src/scripts/runScript.ts index cfb796d38..be1cd5774 100644 --- a/src/scripts/runScript.ts +++ b/src/scripts/runScript.ts @@ -1,9 +1,19 @@ /* eslint-disable no-console */ import { syncDonationsWithIndexerData } from './syncDataWithInverter'; +import { AppDataSource } from '../orm'; // Main function to pull reports and sync donations async function main() { try { + console.debug( + 'run sync script before AppDataSource.initialize()', + new Date(), + ); + await AppDataSource.initialize(false); + console.debug( + 'run sync script after AppDataSource.initialize()', + new Date(), + ); console.info('Start syncing data with indexer...'); await syncDonationsWithIndexerData(); console.info('Data synced successfully.'); diff --git a/src/scripts/syncDataWithInverter.ts b/src/scripts/syncDataWithInverter.ts index 03c74f901..6e8fbba3c 100644 --- a/src/scripts/syncDataWithInverter.ts +++ b/src/scripts/syncDataWithInverter.ts @@ -1,66 +1,22 @@ -/* eslint-disable no-console */ import { FindOptionsWhere } from 'typeorm'; import { Project } from '../entities/project'; import { InverterAdapter } from '../adapters/inverter/inverterAdapter'; import { AppDataSource } from '../orm'; import { getProvider, QACC_NETWORK_ID } from '../provider'; +import { logger } from '../utils/logger'; const adapter = new InverterAdapter(getProvider(QACC_NETWORK_ID)); -async function updateTokenPriceAndTotalSupplyForProjects( - projectFilter: FindOptionsWhere, -) { - const datasource = AppDataSource.getDataSource(); - const projectRepository = datasource.getRepository(Project); - const allProjects = await projectRepository.find({ where: projectFilter }); - for (const project of allProjects) { - if (!project.abc) { - console.error( - `sync project token price failed. project ${project.id} don't have abc object!`, - ); - continue; - } - if (!project.abc.orchestratorAddress) { - console.error( - `sync project token price failed. can not find orchestratorAddress for project ${project.id}!`, - ); - continue; - } - try { - console.debug( - `start fetching token price and total supply of project ${project.id}`, - ); - const price = await fetchTokenPrice(project); - if (price) { - project.abc.tokenPrice = price; - } - const totalSupply = await fetchTokenTotalSupply(project); - if (totalSupply) { - project.abc.totalSupply = totalSupply; - } - await project.save(); - console.debug( - `token price and total supply of project ${project.id} saved successfully`, - ); - } catch (error) { - console.error( - `Error in update token price and total supply of project ${project.id}`, - error, - ); - } - } -} - async function fetchTokenPrice(project: Project) { try { - console.debug(`start fetching token price for project ${project.id}:`); + logger.debug(`start fetching token price for project ${project.id}:`); const tokenPrice = await adapter.getTokenPrice( project.abc.fundingManagerAddress, ); - console.debug(`Fetched token price for project ${project.id}:`, tokenPrice); + logger.debug(`Fetched token price for project ${project.id}:`, tokenPrice); return parseFloat(tokenPrice); } catch (error) { - console.error(`Error in fetch token price of project ${project.id}`, error); + logger.error(`Error in fetch token price of project ${project.id}`, error); return; } } @@ -70,13 +26,13 @@ async function fetchTokenTotalSupply(project: Project) { const tokenTotalSupply = await adapter.getTokenTotalSupplyByAddress( project.abc.orchestratorAddress, ); - console.debug( + logger.debug( `Fetched total supply for project ${project.id}:`, tokenTotalSupply, ); return parseFloat(tokenTotalSupply); } catch (error) { - console.error( + logger.error( `Error fetching total supply for project ${project.id}:`, error, ); @@ -93,9 +49,43 @@ export async function syncDonationsWithIndexerData( projectFilter: {}, }, ) { - console.debug('bootstrap() before AppDataSource.initialize()', new Date()); - await AppDataSource.initialize(false); - console.debug('bootstrap() after AppDataSource.initialize()', new Date()); - - await updateTokenPriceAndTotalSupplyForProjects(projectFilter); + const datasource = AppDataSource.getDataSource(); + const projectRepository = datasource.getRepository(Project); + const allProjects = await projectRepository.find({ where: projectFilter }); + for (const project of allProjects) { + if (!project.abc) { + logger.error( + `sync project token price failed. project ${project.id} don't have abc object!`, + ); + continue; + } + if (!project.abc.orchestratorAddress) { + logger.error( + `sync project token price failed. can not find orchestratorAddress for project ${project.id}!`, + ); + continue; + } + try { + logger.debug( + `start fetching token price and total supply of project ${project.id}`, + ); + const price = await fetchTokenPrice(project); + if (price) { + project.abc.tokenPrice = price; + } + const totalSupply = await fetchTokenTotalSupply(project); + if (totalSupply) { + project.abc.totalSupply = totalSupply; + } + await project.save(); + logger.debug( + `token price and total supply of project ${project.id} saved successfully`, + ); + } catch (error) { + logger.error( + `Error in update token price and total supply of project ${project.id}`, + error, + ); + } + } } diff --git a/src/server/bootstrap.ts b/src/server/bootstrap.ts index 349c500b5..608e128dc 100644 --- a/src/server/bootstrap.ts +++ b/src/server/bootstrap.ts @@ -65,6 +65,7 @@ import { QACC_NETWORK_ID } from '../provider'; import { Token } from '../entities/token'; import { ChainType } from '../types/network'; import { runFetchRoundTokenPrice } from '../services/cronJobs/fetchRoundTokenPrice'; +import { runSyncDataWithInverter } from '../services/cronJobs/syncDataWithInverter'; Resource.validate = validate; @@ -378,6 +379,16 @@ export async function bootstrap() { 'initializeCronJobs() after runFetchRoundTokenPrice() ', new Date(), ); + + logger.debug( + 'initializeCronJobs() before runSyncDataWithInverter() ', + new Date(), + ); + await runSyncDataWithInverter(); + logger.debug( + 'initializeCronJobs() after runSyncDataWithInverter() ', + new Date(), + ); } async function addQAccToken() { diff --git a/src/services/cronJobs/syncDataWithInverter.ts b/src/services/cronJobs/syncDataWithInverter.ts new file mode 100644 index 000000000..cc10c04d4 --- /dev/null +++ b/src/services/cronJobs/syncDataWithInverter.ts @@ -0,0 +1,19 @@ +import { schedule } from 'node-cron'; +import config from '../../config'; +import { logger } from '../../utils/logger'; +import { syncDonationsWithIndexerData } from '../../scripts/syncDataWithInverter'; + +const cronJobTime = + (config.get('SYNC_DATA_WITH_INVERTER_CRONJOB_EXPRESSION') as string) || + '*/7 * * * *'; // every 7 minutes + +export const runSyncDataWithInverter = async () => { + logger.debug( + 'runSyncDataWithInverter() has been called, cronJobTime', + cronJobTime, + ); + await syncDonationsWithIndexerData(); + schedule(cronJobTime, async () => { + await syncDonationsWithIndexerData(); + }); +};