diff --git a/README.md b/README.md index 929b1bbe..4b021e89 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ This project uses [yarn workspaces](https://classic.yarnpkg.com/en/docs/workspac # Link packages cd packages/util && yarn link && cd ../.. + cd packages/cli && yarn link && cd ../.. cd packages/ipld-eth-client && yarn link && cd ../.. cd packages/solidity-mapper && yarn link && cd ../.. cd packages/graph-node && yarn link && cd ../.. @@ -32,6 +33,7 @@ This project uses [yarn workspaces](https://classic.yarnpkg.com/en/docs/workspac ```bash yarn link "@cerc-io/util" + yarn link "@cerc-io/cli" yarn link "@cerc-io/ipld-eth-client" yarn link "@cerc-io/solidity-mapper" yarn link "@cerc-io/graph-node" diff --git a/packages/erc20-watcher/src/cli/reset-cmds/watcher.ts b/packages/erc20-watcher/src/cli/reset-cmds/watcher.ts index 1d5df364..d3840cd1 100644 --- a/packages/erc20-watcher/src/cli/reset-cmds/watcher.ts +++ b/packages/erc20-watcher/src/cli/reset-cmds/watcher.ts @@ -40,7 +40,7 @@ export const handler = async (argv: any): Promise => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); const syncStatus = await indexer.getSyncStatus(); assert(syncStatus, 'Missing syncStatus'); diff --git a/packages/erc20-watcher/src/cli/watch-contract.ts b/packages/erc20-watcher/src/cli/watch-contract.ts index 4066d143..bf1e7834 100644 --- a/packages/erc20-watcher/src/cli/watch-contract.ts +++ b/packages/erc20-watcher/src/cli/watch-contract.ts @@ -2,69 +2,25 @@ // Copyright 2021 Vulcanize, Inc. // -import assert from 'assert'; -import yargs from 'yargs'; import 'reflect-metadata'; +import debug from 'debug'; -import { DEFAULT_CONFIG_PATH, JobQueue, initClients, getConfig } from '@cerc-io/util'; -import { Config } from '@vulcanize/util'; +import { WatchContractCmd } from '@cerc-io/cli'; import { Database } from '../database'; -import { CONTRACT_KIND, Indexer } from '../indexer'; +import { Indexer } from '../indexer'; -(async () => { - 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 - }, - address: { - type: 'string', - require: true, - demandOption: true, - describe: 'Address of the deployed contract' - }, - checkpoint: { - type: 'boolean', - require: true, - demandOption: true, - describe: 'Turn checkpointing on' - }, - startingBlock: { - type: 'number', - default: 1, - describe: 'Starting block' - } - }).argv; +const log = debug('vulcanize:watch-contract'); - const config: Config = await getConfig(argv.configFile); - const { database: dbConfig, jobQueue: jobQueueConfig } = config; - const { ethClient, ethProvider } = await initClients(config); +const main = async (): Promise => { + const watchContractCmd = new WatchContractCmd(); + await watchContractCmd.init(Database, Indexer); - assert(dbConfig); + await watchContractCmd.exec(); +}; - const db = new Database(dbConfig); - await db.init(); - - 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 }); - await jobQueue.start(); - - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); - - await indexer.watchContract(argv.address, CONTRACT_KIND, argv.checkpoint, argv.startingBlock); - - await db.close(); - await jobQueue.stop(); - process.exit(); -})(); +main().catch(err => { + log(err); +}).finally(() => { + process.exit(0); +}); diff --git a/packages/erc20-watcher/src/database.ts b/packages/erc20-watcher/src/database.ts index 34507864..7d1df13a 100644 --- a/packages/erc20-watcher/src/database.ts +++ b/packages/erc20-watcher/src/database.ts @@ -36,6 +36,10 @@ export class Database implements DatabaseInterface { this._baseDatabase = new BaseDatabase(this._config); } + get baseDatabase (): BaseDatabase { + return this._baseDatabase; + } + async init (): Promise { this._conn = await this._baseDatabase.init(); } diff --git a/packages/erc20-watcher/src/fill.ts b/packages/erc20-watcher/src/fill.ts index 9fd1c08e..9b6412c3 100644 --- a/packages/erc20-watcher/src/fill.ts +++ b/packages/erc20-watcher/src/fill.ts @@ -96,7 +96,7 @@ export const main = async (): Promise => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); await jobQueue.start(); - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); const eventWatcher = new EventWatcher(upstream, ethClient, indexer, pubsub, jobQueue); diff --git a/packages/erc20-watcher/src/indexer.ts b/packages/erc20-watcher/src/indexer.ts index cd32caba..19d06c6d 100644 --- a/packages/erc20-watcher/src/indexer.ts +++ b/packages/erc20-watcher/src/indexer.ts @@ -19,7 +19,9 @@ import { Where, QueryOptions, UNKNOWN_EVENT_NAME, - JobQueue + JobQueue, + DatabaseInterface, + Clients } from '@cerc-io/util'; import { StorageLayout, MappingKey } from '@cerc-io/solidity-mapper'; @@ -69,12 +71,12 @@ export class Indexer implements IndexerInterface { _contract: ethers.utils.Interface _serverMode: string - constructor (serverConfig: ServerConfig, db: Database, ethClient: EthClient, ethProvider: ethers.providers.BaseProvider, jobQueue: JobQueue) { + constructor (serverConfig: ServerConfig, db: DatabaseInterface, clients: Clients, ethProvider: ethers.providers.BaseProvider, jobQueue: JobQueue) { assert(db); - assert(ethClient); + assert(clients.ethClient); - this._db = db; - this._ethClient = ethClient; + this._db = db as Database; + this._ethClient = clients.ethClient; this._ethProvider = ethProvider; this._serverConfig = serverConfig; this._serverMode = this._serverConfig.mode; @@ -99,6 +101,10 @@ export class Indexer implements IndexerInterface { return this._storageLayoutMap; } + async init (): Promise { + await this._baseIndexer.fetchContracts(); + } + getResultEvent (event: Event): EventResult { const eventFields = JSON.parse(event.eventInfo); diff --git a/packages/erc20-watcher/src/job-runner.ts b/packages/erc20-watcher/src/job-runner.ts index 8c0e3122..57bc3744 100644 --- a/packages/erc20-watcher/src/job-runner.ts +++ b/packages/erc20-watcher/src/job-runner.ts @@ -102,7 +102,8 @@ export const main = async (): Promise => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); await jobQueue.start(); - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); + await indexer.init(); const jobRunner = new JobRunner(jobQueueConfig, indexer, jobQueue); await jobRunner.start(); diff --git a/packages/erc20-watcher/src/server.ts b/packages/erc20-watcher/src/server.ts index ed695fb8..27a7f22f 100644 --- a/packages/erc20-watcher/src/server.ts +++ b/packages/erc20-watcher/src/server.ts @@ -81,7 +81,8 @@ export const main = async (): Promise => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); + await indexer.init(); const eventWatcher = new EventWatcher(upstream, ethClient, indexer, pubsub, jobQueue); diff --git a/packages/uni-info-watcher/src/cli/checkpoint-cmds/create.ts b/packages/uni-info-watcher/src/cli/checkpoint-cmds/create.ts index ea01b5d6..05186089 100644 --- a/packages/uni-info-watcher/src/cli/checkpoint-cmds/create.ts +++ b/packages/uni-info-watcher/src/cli/checkpoint-cmds/create.ts @@ -56,7 +56,7 @@ export const handler = async (argv: any): Promise => { const uniClient = new UniClient(uniWatcher); const erc20Client = new ERC20Client(tokenWatcher); - const indexer = new Indexer(config.server, db, uniClient, erc20Client, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { uniClient, erc20Client, ethClient }, ethProvider, jobQueue); await indexer.init(); const blockHash = await indexer.processCLICheckpoint(argv.address, argv.blockHash); diff --git a/packages/uni-info-watcher/src/cli/export-state.ts b/packages/uni-info-watcher/src/cli/export-state.ts index 99c21d2c..bfa5842c 100644 --- a/packages/uni-info-watcher/src/cli/export-state.ts +++ b/packages/uni-info-watcher/src/cli/export-state.ts @@ -66,7 +66,7 @@ const main = async (): Promise => { const uniClient = new UniClient(uniWatcher); const erc20Client = new ERC20Client(tokenWatcher); - const indexer = new Indexer(config.server, db, uniClient, erc20Client, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { uniClient, erc20Client, ethClient }, ethProvider, jobQueue); await indexer.init(); const exportData: any = { diff --git a/packages/uni-info-watcher/src/cli/import-state.ts b/packages/uni-info-watcher/src/cli/import-state.ts index 649886ff..cb1acd11 100644 --- a/packages/uni-info-watcher/src/cli/import-state.ts +++ b/packages/uni-info-watcher/src/cli/import-state.ts @@ -71,7 +71,7 @@ export const main = async (): Promise => { const uniClient = new UniClient(uniWatcher); const erc20Client = new ERC20Client(tokenWatcher); - const indexer = new Indexer(config.server, db, uniClient, erc20Client, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { uniClient, erc20Client, ethClient }, ethProvider, jobQueue); await indexer.init(); const eventWatcher = new EventWatcher(config.upstream, ethClient, indexer, pubsub, jobQueue); diff --git a/packages/uni-info-watcher/src/cli/inspect-cid.ts b/packages/uni-info-watcher/src/cli/inspect-cid.ts index 79cc0566..07fc894b 100644 --- a/packages/uni-info-watcher/src/cli/inspect-cid.ts +++ b/packages/uni-info-watcher/src/cli/inspect-cid.ts @@ -61,7 +61,7 @@ const main = async (): Promise => { const uniClient = new UniClient(uniWatcher); const erc20Client = new ERC20Client(tokenWatcher); - const indexer = new Indexer(config.server, db, uniClient, erc20Client, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { uniClient, erc20Client, ethClient }, ethProvider, jobQueue); await indexer.init(); const state = await indexer.getStateByCID(argv.cid); diff --git a/packages/uni-info-watcher/src/cli/reset-cmds/watcher.ts b/packages/uni-info-watcher/src/cli/reset-cmds/watcher.ts index 1c8e4172..b74427eb 100644 --- a/packages/uni-info-watcher/src/cli/reset-cmds/watcher.ts +++ b/packages/uni-info-watcher/src/cli/reset-cmds/watcher.ts @@ -51,7 +51,7 @@ export const handler = async (argv: any): Promise => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); await jobQueue.start(); - const indexer = new Indexer(config.server, db, uniClient, erc20Client, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { uniClient, erc20Client, ethClient }, ethProvider, jobQueue); await indexer.resetWatcherToBlock(argv.blockNumber); await indexer.resetLatestEntities(argv.blockNumber); diff --git a/packages/uni-info-watcher/src/cli/watch-contract.ts b/packages/uni-info-watcher/src/cli/watch-contract.ts index 90062d44..f8ba8b2c 100644 --- a/packages/uni-info-watcher/src/cli/watch-contract.ts +++ b/packages/uni-info-watcher/src/cli/watch-contract.ts @@ -2,72 +2,23 @@ // Copyright 2022 Vulcanize, Inc. // -import assert from 'assert'; -import yargs from 'yargs'; import 'reflect-metadata'; +import debug from 'debug'; -import { DEFAULT_CONFIG_PATH, JobQueue, getConfig, initClients } from '@cerc-io/util'; -import { Config } from '@vulcanize/util'; +import { WatchContractCmd } from '@cerc-io/cli'; import { Client as ERC20Client } from '@vulcanize/erc20-watcher'; import { Client as UniClient } from '@vulcanize/uni-watcher'; +import { Config } from '@vulcanize/util'; import { Database } from '../database'; import { Indexer } from '../indexer'; -(async () => { - 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 - }, - address: { - type: 'string', - require: true, - demandOption: true, - describe: 'Address of the deployed contract' - }, - kind: { - type: 'string', - require: true, - demandOption: true, - describe: 'Kind of contract (factory|pool|nfpm)' - }, - checkpoint: { - type: 'boolean', - require: true, - demandOption: true, - describe: 'Turn checkpointing on' - }, - startingBlock: { - type: 'number', - default: 1, - describe: 'Starting block' - } - }).argv; - - const config: Config = await getConfig(argv.configFile); - const { database: dbConfig, jobQueue: jobQueueConfig } = config; - const { ethClient, ethProvider } = await initClients(config); - - assert(dbConfig); +const log = debug('vulcanize:watch-contract'); - const db = new Database(dbConfig, config.server); - await db.init(); - - 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 }); - await jobQueue.start(); +const main = async (): Promise => { + const watchContractCmd = new WatchContractCmd(); + const config: Config = await watchContractCmd.initConfig(); const { uniWatcher, tokenWatcher @@ -76,12 +27,12 @@ import { Indexer } from '../indexer'; const uniClient = new UniClient(uniWatcher); const erc20Client = new ERC20Client(tokenWatcher); - const indexer = new Indexer(config.server, db, uniClient, erc20Client, ethClient, ethProvider, jobQueue); - await indexer.init(); - - await indexer.watchContract(argv.address, argv.kind, argv.checkpoint, argv.startingBlock); + await watchContractCmd.init(Database, Indexer, { uniClient, erc20Client }); + await watchContractCmd.exec(); +}; - await db.close(); - await jobQueue.stop(); - process.exit(); -})(); +main().catch(err => { + log(err); +}).finally(() => { + process.exit(0); +}); diff --git a/packages/uni-info-watcher/src/database.ts b/packages/uni-info-watcher/src/database.ts index 8d410bef..8a5842c5 100644 --- a/packages/uni-info-watcher/src/database.ts +++ b/packages/uni-info-watcher/src/database.ts @@ -9,7 +9,6 @@ import { DeepPartial, FindConditions, FindManyOptions, - In, LessThanOrEqual, QueryRunner, Repository, @@ -122,8 +121,9 @@ export class Database implements DatabaseInterface { _graphDatabase: GraphDatabase _relationsMap: Map - constructor (config: ConnectionOptions, serverConfig: ServerConfig) { + constructor (config: ConnectionOptions, serverConfig?: ServerConfig) { assert(config); + assert(serverConfig); const entitiesDir = path.join(__dirname, 'entity/*'); this._config = { diff --git a/packages/uni-info-watcher/src/fill.ts b/packages/uni-info-watcher/src/fill.ts index 6d8b0a08..e903f407 100644 --- a/packages/uni-info-watcher/src/fill.ts +++ b/packages/uni-info-watcher/src/fill.ts @@ -107,7 +107,7 @@ export const main = async (): Promise => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); await jobQueue.start(); - const indexer = new Indexer(config.server, db, uniClient, erc20Client, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { uniClient, erc20Client, ethClient }, ethProvider, jobQueue); await indexer.init(); if (argv.state) { diff --git a/packages/uni-info-watcher/src/indexer.ts b/packages/uni-info-watcher/src/indexer.ts index 44cb9dcf..602522dc 100644 --- a/packages/uni-info-watcher/src/indexer.ts +++ b/packages/uni-info-watcher/src/indexer.ts @@ -27,7 +27,9 @@ import { getFullBlock, StateKind, JobQueue, - GraphDecimal + GraphDecimal, + DatabaseInterface, + Clients } from '@cerc-io/util'; import { EthClient } from '@cerc-io/ipld-eth-client'; import { StorageLayout, MappingKey } from '@cerc-io/solidity-mapper'; @@ -77,15 +79,16 @@ export class Indexer implements IndexerInterface { _subgraphStateMap: Map = new Map() _fullBlock?: Block - constructor (serverConfig: ServerConfig, db: Database, uniClient: UniClient, erc20Client: ERC20Client, ethClient: EthClient, ethProvider: providers.BaseProvider, jobQueue: JobQueue) { + constructor (serverConfig: ServerConfig, db: DatabaseInterface, clients: Clients, ethProvider: providers.BaseProvider, jobQueue: JobQueue) { assert(db); - assert(uniClient); - assert(erc20Client); - - this._db = db; - this._uniClient = uniClient; - this._erc20Client = erc20Client; - this._ethClient = ethClient; + assert(clients.ethClient); + assert(clients.erc20Client); + assert(clients.uniClient); + + this._db = db as Database; + this._uniClient = clients.uniClient; + this._erc20Client = clients.erc20Client; + this._ethClient = clients.ethClient; this._ethProvider = ethProvider; this._serverConfig = serverConfig; this._baseIndexer = new BaseIndexer(this._serverConfig, this._db, this._ethClient, this._ethProvider, jobQueue); diff --git a/packages/uni-info-watcher/src/job-runner.ts b/packages/uni-info-watcher/src/job-runner.ts index 69e67a1d..d16ab636 100644 --- a/packages/uni-info-watcher/src/job-runner.ts +++ b/packages/uni-info-watcher/src/job-runner.ts @@ -129,7 +129,7 @@ export const main = async (): Promise => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); await jobQueue.start(); - const indexer = new Indexer(config.server, db, uniClient, erc20Client, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { uniClient, erc20Client, ethClient }, ethProvider, jobQueue); await indexer.init(); if (mode !== 'demo') { diff --git a/packages/uni-info-watcher/src/server.ts b/packages/uni-info-watcher/src/server.ts index 626a6dcf..b7f7b1c7 100644 --- a/packages/uni-info-watcher/src/server.ts +++ b/packages/uni-info-watcher/src/server.ts @@ -81,7 +81,7 @@ export const main = async (): Promise => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); await jobQueue.start(); - const indexer = new Indexer(config.server, db, uniClient, erc20Client, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { uniClient, erc20Client, ethClient }, ethProvider, jobQueue); const pubSub = new PubSub(); const eventWatcher = new EventWatcher(upstream, ethClient, indexer, pubSub, jobQueue); diff --git a/packages/uni-info-watcher/test/init.ts b/packages/uni-info-watcher/test/init.ts index 90794622..a24c97d4 100644 --- a/packages/uni-info-watcher/test/init.ts +++ b/packages/uni-info-watcher/test/init.ts @@ -52,7 +52,7 @@ const main = async () => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); await jobQueue.start(); - const indexer = new Indexer(config.server, db, uniClient, erc20Client, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { uniClient, erc20Client, ethClient }, ethProvider, jobQueue); await indexer.init(); // Get the factory contract address. diff --git a/packages/uni-watcher/src/chain-pruning.test.ts b/packages/uni-watcher/src/chain-pruning.test.ts index a5f72711..c807626b 100644 --- a/packages/uni-watcher/src/chain-pruning.test.ts +++ b/packages/uni-watcher/src/chain-pruning.test.ts @@ -62,7 +62,7 @@ describe('chain pruning', () => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); - indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); + indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); assert(indexer, 'Could not create indexer object.'); jobRunner = new JobRunner(jobQueueConfig, indexer, jobQueue); diff --git a/packages/uni-watcher/src/cli/reset-cmds/watcher.ts b/packages/uni-watcher/src/cli/reset-cmds/watcher.ts index ff0a0bd7..40a11b3b 100644 --- a/packages/uni-watcher/src/cli/reset-cmds/watcher.ts +++ b/packages/uni-watcher/src/cli/reset-cmds/watcher.ts @@ -40,7 +40,7 @@ export const handler = async (argv: any): Promise => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); await indexer.resetWatcherToBlock(argv.blockNumber); log('Reset watcher successfully'); diff --git a/packages/uni-watcher/src/cli/watch-contract.ts b/packages/uni-watcher/src/cli/watch-contract.ts index 34dfadf6..bf1e7834 100644 --- a/packages/uni-watcher/src/cli/watch-contract.ts +++ b/packages/uni-watcher/src/cli/watch-contract.ts @@ -2,76 +2,25 @@ // Copyright 2021 Vulcanize, Inc. // -import assert from 'assert'; -import yargs from 'yargs'; import 'reflect-metadata'; +import debug from 'debug'; -import { DEFAULT_CONFIG_PATH, JobQueue, getConfig, initClients } from '@cerc-io/util'; -import { Config } from '@vulcanize/util'; +import { WatchContractCmd } from '@cerc-io/cli'; import { Database } from '../database'; import { Indexer } from '../indexer'; -(async () => { - 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 - }, - address: { - type: 'string', - require: true, - demandOption: true, - describe: 'Address of the deployed contract' - }, - kind: { - type: 'string', - require: true, - demandOption: true, - describe: 'Kind of contract (factory|pool|nfpm)' - }, - checkpoint: { - type: 'boolean', - require: true, - demandOption: true, - describe: 'Turn checkpointing on' - }, - startingBlock: { - type: 'number', - default: 1, - describe: 'Starting block' - } - }).argv; +const log = debug('vulcanize:watch-contract'); - const config: Config = await getConfig(argv.configFile); - const { database: dbConfig, jobQueue: jobQueueConfig } = config; - const { ethClient, ethProvider } = await initClients(config); +const main = async (): Promise => { + const watchContractCmd = new WatchContractCmd(); + await watchContractCmd.init(Database, Indexer); - assert(dbConfig); + await watchContractCmd.exec(); +}; - const db = new Database(dbConfig); - await db.init(); - - 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 }); - await jobQueue.start(); - - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); - await indexer.init(); - - await indexer.watchContract(argv.address, argv.kind, argv.checkpoint, argv.startingBlock); - - await db.close(); - await jobQueue.stop(); - process.exit(); -})(); +main().catch(err => { + log(err); +}).finally(() => { + process.exit(0); +}); diff --git a/packages/uni-watcher/src/database.ts b/packages/uni-watcher/src/database.ts index 686600de..8d80b7f3 100644 --- a/packages/uni-watcher/src/database.ts +++ b/packages/uni-watcher/src/database.ts @@ -31,6 +31,10 @@ export class Database implements DatabaseInterface { this._baseDatabase = new BaseDatabase(this._config); } + get baseDatabase (): BaseDatabase { + return this._baseDatabase; + } + async init (): Promise { this._conn = await this._baseDatabase.init(); } diff --git a/packages/uni-watcher/src/fill.ts b/packages/uni-watcher/src/fill.ts index 2c9ef784..21379611 100644 --- a/packages/uni-watcher/src/fill.ts +++ b/packages/uni-watcher/src/fill.ts @@ -96,7 +96,7 @@ export const main = async (): Promise => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); await jobQueue.start(); - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); await indexer.init(); const eventWatcher = new EventWatcher(upstream, ethClient, indexer, pubsub, jobQueue); diff --git a/packages/uni-watcher/src/indexer.ts b/packages/uni-watcher/src/indexer.ts index 50022879..5795fbee 100644 --- a/packages/uni-watcher/src/indexer.ts +++ b/packages/uni-watcher/src/indexer.ts @@ -18,7 +18,9 @@ import { UNKNOWN_EVENT_NAME, ResultEvent, getResultEvent, - JobQueue + JobQueue, + DatabaseInterface, + Clients } from '@cerc-io/util'; import { EthClient } from '@cerc-io/ipld-eth-client'; import { StorageLayout, MappingKey } from '@cerc-io/solidity-mapper'; @@ -49,9 +51,11 @@ export class Indexer implements IndexerInterface { _poolContract: ethers.utils.Interface _nfpmContract: ethers.utils.Interface - constructor (serverConfig: ServerConfig, db: Database, ethClient: EthClient, ethProvider: ethers.providers.BaseProvider, jobQueue: JobQueue) { - this._db = db; - this._ethClient = ethClient; + constructor (serverConfig: ServerConfig, db: DatabaseInterface, clients: Clients, ethProvider: ethers.providers.BaseProvider, jobQueue: JobQueue) { + assert(db); + + this._db = db as Database; + this._ethClient = clients.ethClient; this._ethProvider = ethProvider; this._serverConfig = serverConfig; this._baseIndexer = new BaseIndexer(this._serverConfig, this._db, this._ethClient, this._ethProvider, jobQueue); diff --git a/packages/uni-watcher/src/job-runner.ts b/packages/uni-watcher/src/job-runner.ts index e54e580c..5528ca91 100644 --- a/packages/uni-watcher/src/job-runner.ts +++ b/packages/uni-watcher/src/job-runner.ts @@ -105,7 +105,7 @@ export const main = async (): Promise => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); await jobQueue.start(); - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); await indexer.init(); const jobRunner = new JobRunner(jobQueueConfig, indexer, jobQueue); diff --git a/packages/uni-watcher/src/server.ts b/packages/uni-watcher/src/server.ts index 28af8c8a..66549aab 100644 --- a/packages/uni-watcher/src/server.ts +++ b/packages/uni-watcher/src/server.ts @@ -72,7 +72,7 @@ export const main = async (): Promise => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); await jobQueue.start(); - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); await indexer.init(); const eventWatcher = new EventWatcher(upstream, ethClient, indexer, pubsub, jobQueue); diff --git a/packages/uni-watcher/src/smoke.test.ts b/packages/uni-watcher/src/smoke.test.ts index cf5e1c82..3b9452f6 100644 --- a/packages/uni-watcher/src/smoke.test.ts +++ b/packages/uni-watcher/src/smoke.test.ts @@ -124,7 +124,7 @@ describe('uni-watcher', () => { factory = new Contract(factoryContract.address, FACTORY_ABI, signer); // Verifying with the db. - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); await indexer.init(); assert(indexer.isWatchedContract(factory.address), 'Factory contract not added to the database.'); }); @@ -260,7 +260,7 @@ describe('uni-watcher', () => { nfpm = new Contract(nfpmContract.address, NFPM_ABI, signer); // Verifying with the db. - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); await indexer.init(); assert(await indexer.isWatchedContract(nfpm.address), 'NFPM contract not added to the database.'); }); diff --git a/packages/uni-watcher/test/init.ts b/packages/uni-watcher/test/init.ts index 3cb14826..73b4b2c4 100644 --- a/packages/uni-watcher/test/init.ts +++ b/packages/uni-watcher/test/init.ts @@ -80,7 +80,7 @@ const main = async () => { const jobQueue = new JobQueue({ dbConnectionString, maxCompletionLag: maxCompletionLagInSecs }); await jobQueue.start(); - const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue); + const indexer = new Indexer(config.server, db, { ethClient }, ethProvider, jobQueue); let factory: Contract; // Checking whether factory is deployed.