Skip to content

Commit

Permalink
Add sync data with indexer cronjob
Browse files Browse the repository at this point in the history
  • Loading branch information
ae2079 committed Oct 21, 2024
1 parent a3f8cd2 commit 8e43138
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 55 deletions.
10 changes: 10 additions & 0 deletions src/scripts/runScript.ts
Original file line number Diff line number Diff line change
@@ -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.');
Expand Down
100 changes: 45 additions & 55 deletions src/scripts/syncDataWithInverter.ts
Original file line number Diff line number Diff line change
@@ -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<Project>,
) {
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;
}
}
Expand All @@ -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,
);
Expand All @@ -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,
);
}
}
}
11 changes: 11 additions & 0 deletions src/server/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down
19 changes: 19 additions & 0 deletions src/services/cronJobs/syncDataWithInverter.ts
Original file line number Diff line number Diff line change
@@ -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();
});
};

0 comments on commit 8e43138

Please sign in to comment.