Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor index-block CLI to cli package #258

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions packages/cli/src/index-block.ts
Original file line number Diff line number Diff line change
@@ -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<ConfigType> (): Promise<ConfigType> {
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<void> {
await this.initConfig();

await this._baseCmd.init(Database, Indexer, clients);
}

async exec (): Promise<void> {
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;
}
}
1 change: 1 addition & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './import-state';
export * from './export-state';
export * from './server';
export * from './job-runner';
export * from './index-block';
56 changes: 5 additions & 51 deletions packages/eden-watcher/src/cli/index-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
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 => {
Expand Down
45 changes: 4 additions & 41 deletions packages/erc721-watcher/src/cli/index-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +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 { IndexBlockCmd } from '@cerc-io/cli';

import { Database } from '../database';
import { Indexer } from '../indexer';

const log = debug('vulcanize:index-block');

const main = async (): Promise<void> => {
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 => {
Expand Down
56 changes: 5 additions & 51 deletions packages/graph-test-watcher/src/cli/index-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
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 => {
Expand Down
45 changes: 4 additions & 41 deletions packages/mobymask-watcher/src/cli/index-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +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 { IndexBlockCmd } from '@cerc-io/cli';

import { Database } from '../database';
import { Indexer } from '../indexer';

const log = debug('vulcanize:index-block');

const main = async (): Promise<void> => {
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 => {
Expand Down