diff --git a/packages/cli/src/index-block.ts b/packages/cli/src/index-block.ts new file mode 100644 index 00000000..2535f296 --- /dev/null +++ b/packages/cli/src/index-block.ts @@ -0,0 +1,100 @@ +// +// Copyright 2022 Vulcanize, Inc. +// + +import yargs from 'yargs'; +import 'reflect-metadata'; +import assert from 'assert'; +import { ConnectionOptions } from 'typeorm'; + +import { JsonRpcProvider } from '@ethersproject/providers'; +import { GraphWatcher } from '@cerc-io/graph-node'; +import { + DEFAULT_CONFIG_PATH, + JobQueue, + DatabaseInterface, + IndexerInterface, + ServerConfig, + Clients, + indexBlock +} from '@cerc-io/util'; + +import { BaseCmd } from './base'; + +interface Arguments { + configFile: string; + block: number; +} + +export class IndexBlockCmd { + _argv?: Arguments; + _baseCmd: BaseCmd; + + constructor () { + this._baseCmd = new BaseCmd(); + } + + async initConfig (): Promise { + this._argv = this._getArgv(); + assert(this._argv); + + return this._baseCmd.initConfig(this._argv.configFile); + } + + async init ( + Database: new ( + config: ConnectionOptions, + serverConfig?: ServerConfig + ) => DatabaseInterface, + Indexer: new ( + serverConfig: ServerConfig, + db: DatabaseInterface, + clients: Clients, + ethProvider: JsonRpcProvider, + jobQueue: JobQueue, + graphWatcher?: GraphWatcher + ) => IndexerInterface, + clients: { [key: string]: any } = {} + ): Promise { + await this.initConfig(); + + await this._baseCmd.init(Database, Indexer, clients); + } + + async exec (): Promise { + assert(this._argv); + + const config = this._baseCmd.config; + const indexer = this._baseCmd.indexer; + const database = this._baseCmd.database; + + assert(config); + assert(indexer); + assert(database); + + await indexBlock(indexer, config.jobQueue.eventsInBatch, this._argv); + + await database.close(); + } + + _getArgv (): any { + return yargs.parserConfiguration({ + 'parse-numbers': false + }).options({ + configFile: { + alias: 'f', + type: 'string', + require: true, + demandOption: true, + describe: 'Configuration file path (toml)', + default: DEFAULT_CONFIG_PATH + }, + block: { + type: 'number', + require: true, + demandOption: true, + describe: 'Block number to index' + } + }).argv; + } +} diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 5255efd4..2ce4d508 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -12,3 +12,4 @@ export * from './import-state'; export * from './export-state'; export * from './server'; export * from './job-runner'; +export * from './index-block'; diff --git a/packages/eden-watcher/src/cli/index-block.ts b/packages/eden-watcher/src/cli/index-block.ts index 9c54bbc8..6b59df5e 100644 --- a/packages/eden-watcher/src/cli/index-block.ts +++ b/packages/eden-watcher/src/cli/index-block.ts @@ -2,67 +2,21 @@ // Copyright 2022 Vulcanize, Inc. // -import yargs from 'yargs'; import 'reflect-metadata'; import debug from 'debug'; -import assert from 'assert'; -import { Config, DEFAULT_CONFIG_PATH, getConfig, initClients, JobQueue, indexBlock } from '@cerc-io/util'; -import { GraphWatcher, Database as GraphDatabase } from '@cerc-io/graph-node'; +import { IndexBlockCmd } from '@cerc-io/cli'; -import { Database, ENTITY_TO_LATEST_ENTITY_MAP } from '../database'; +import { Database } from '../database'; import { Indexer } from '../indexer'; const log = debug('vulcanize:index-block'); const main = async (): Promise => { - const argv = await yargs.parserConfiguration({ - 'parse-numbers': false - }).options({ - configFile: { - alias: 'f', - type: 'string', - require: true, - demandOption: true, - describe: 'Configuration file path (toml)', - default: DEFAULT_CONFIG_PATH - }, - block: { - type: 'number', - require: true, - demandOption: true, - describe: 'Block number to index' - } - }).argv; + const indexBlockCmd = new IndexBlockCmd(); + await indexBlockCmd.init(Database, Indexer); - const config: Config = await getConfig(argv.configFile); - const { ethClient, ethProvider } = await initClients(config); - - const db = new Database(config.database); - await db.init(); - - const graphDb = new GraphDatabase(config.server, db.baseDatabase, ENTITY_TO_LATEST_ENTITY_MAP); - await graphDb.init(); - - const graphWatcher = new GraphWatcher(graphDb, ethClient, ethProvider, config.server); - - const jobQueueConfig = config.jobQueue; - assert(jobQueueConfig, 'Missing job queue config'); - - const { dbConnectionString, maxCompletionLagInSecs } = jobQueueConfig; - assert(dbConnectionString, 'Missing job queue db connection string'); - - const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); - - const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue, graphWatcher); - await indexer.init(); - - graphWatcher.setIndexer(indexer); - await graphWatcher.init(); - - await indexBlock(indexer, jobQueueConfig.eventsInBatch, argv); - - await db.close(); + await indexBlockCmd.exec(); }; main().catch(err => { diff --git a/packages/erc721-watcher/src/cli/index-block.ts b/packages/erc721-watcher/src/cli/index-block.ts index 5139818c..6b59df5e 100644 --- a/packages/erc721-watcher/src/cli/index-block.ts +++ b/packages/erc721-watcher/src/cli/index-block.ts @@ -2,12 +2,10 @@ // Copyright 2022 Vulcanize, Inc. // -import yargs from 'yargs'; import 'reflect-metadata'; import debug from 'debug'; -import assert from 'assert'; -import { Config, DEFAULT_CONFIG_PATH, getConfig, initClients, JobQueue, indexBlock } from '@cerc-io/util'; +import { IndexBlockCmd } from '@cerc-io/cli'; import { Database } from '../database'; import { Indexer } from '../indexer'; @@ -15,45 +13,10 @@ import { Indexer } from '../indexer'; const log = debug('vulcanize:index-block'); const main = async (): Promise => { - const argv = await yargs.parserConfiguration({ - 'parse-numbers': false - }).options({ - configFile: { - alias: 'f', - type: 'string', - require: true, - demandOption: true, - describe: 'Configuration file path (toml)', - default: DEFAULT_CONFIG_PATH - }, - block: { - type: 'number', - require: true, - demandOption: true, - describe: 'Block number to index' - } - }).argv; + const indexBlockCmd = new IndexBlockCmd(); + await indexBlockCmd.init(Database, Indexer); - const config: Config = await getConfig(argv.configFile); - const { ethClient, ethProvider } = await initClients(config); - - const db = new Database(config.database); - await db.init(); - - const jobQueueConfig = config.jobQueue; - assert(jobQueueConfig, 'Missing job queue config'); - - const { dbConnectionString, maxCompletionLagInSecs } = jobQueueConfig; - assert(dbConnectionString, 'Missing job queue db connection string'); - - const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); - - const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); - await indexer.init(); - - await indexBlock(indexer, jobQueueConfig.eventsInBatch, argv); - - await db.close(); + await indexBlockCmd.exec(); }; main().catch(err => { diff --git a/packages/graph-test-watcher/src/cli/index-block.ts b/packages/graph-test-watcher/src/cli/index-block.ts index 9c54bbc8..6b59df5e 100644 --- a/packages/graph-test-watcher/src/cli/index-block.ts +++ b/packages/graph-test-watcher/src/cli/index-block.ts @@ -2,67 +2,21 @@ // Copyright 2022 Vulcanize, Inc. // -import yargs from 'yargs'; import 'reflect-metadata'; import debug from 'debug'; -import assert from 'assert'; -import { Config, DEFAULT_CONFIG_PATH, getConfig, initClients, JobQueue, indexBlock } from '@cerc-io/util'; -import { GraphWatcher, Database as GraphDatabase } from '@cerc-io/graph-node'; +import { IndexBlockCmd } from '@cerc-io/cli'; -import { Database, ENTITY_TO_LATEST_ENTITY_MAP } from '../database'; +import { Database } from '../database'; import { Indexer } from '../indexer'; const log = debug('vulcanize:index-block'); const main = async (): Promise => { - const argv = await yargs.parserConfiguration({ - 'parse-numbers': false - }).options({ - configFile: { - alias: 'f', - type: 'string', - require: true, - demandOption: true, - describe: 'Configuration file path (toml)', - default: DEFAULT_CONFIG_PATH - }, - block: { - type: 'number', - require: true, - demandOption: true, - describe: 'Block number to index' - } - }).argv; + const indexBlockCmd = new IndexBlockCmd(); + await indexBlockCmd.init(Database, Indexer); - const config: Config = await getConfig(argv.configFile); - const { ethClient, ethProvider } = await initClients(config); - - const db = new Database(config.database); - await db.init(); - - const graphDb = new GraphDatabase(config.server, db.baseDatabase, ENTITY_TO_LATEST_ENTITY_MAP); - await graphDb.init(); - - const graphWatcher = new GraphWatcher(graphDb, ethClient, ethProvider, config.server); - - const jobQueueConfig = config.jobQueue; - assert(jobQueueConfig, 'Missing job queue config'); - - const { dbConnectionString, maxCompletionLagInSecs } = jobQueueConfig; - assert(dbConnectionString, 'Missing job queue db connection string'); - - const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); - - const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue, graphWatcher); - await indexer.init(); - - graphWatcher.setIndexer(indexer); - await graphWatcher.init(); - - await indexBlock(indexer, jobQueueConfig.eventsInBatch, argv); - - await db.close(); + await indexBlockCmd.exec(); }; main().catch(err => { diff --git a/packages/mobymask-watcher/src/cli/index-block.ts b/packages/mobymask-watcher/src/cli/index-block.ts index 5139818c..6b59df5e 100644 --- a/packages/mobymask-watcher/src/cli/index-block.ts +++ b/packages/mobymask-watcher/src/cli/index-block.ts @@ -2,12 +2,10 @@ // Copyright 2022 Vulcanize, Inc. // -import yargs from 'yargs'; import 'reflect-metadata'; import debug from 'debug'; -import assert from 'assert'; -import { Config, DEFAULT_CONFIG_PATH, getConfig, initClients, JobQueue, indexBlock } from '@cerc-io/util'; +import { IndexBlockCmd } from '@cerc-io/cli'; import { Database } from '../database'; import { Indexer } from '../indexer'; @@ -15,45 +13,10 @@ import { Indexer } from '../indexer'; const log = debug('vulcanize:index-block'); const main = async (): Promise => { - const argv = await yargs.parserConfiguration({ - 'parse-numbers': false - }).options({ - configFile: { - alias: 'f', - type: 'string', - require: true, - demandOption: true, - describe: 'Configuration file path (toml)', - default: DEFAULT_CONFIG_PATH - }, - block: { - type: 'number', - require: true, - demandOption: true, - describe: 'Block number to index' - } - }).argv; + const indexBlockCmd = new IndexBlockCmd(); + await indexBlockCmd.init(Database, Indexer); - const config: Config = await getConfig(argv.configFile); - const { ethClient, ethProvider } = await initClients(config); - - const db = new Database(config.database); - await db.init(); - - const jobQueueConfig = config.jobQueue; - assert(jobQueueConfig, 'Missing job queue config'); - - const { dbConnectionString, maxCompletionLagInSecs } = jobQueueConfig; - assert(dbConnectionString, 'Missing job queue db connection string'); - - const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); - - const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); - await indexer.init(); - - await indexBlock(indexer, jobQueueConfig.eventsInBatch, argv); - - await db.close(); + await indexBlockCmd.exec(); }; main().catch(err => {