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

Use watch-contract CLI from cli package #402

Merged
merged 1 commit into from
Nov 21, 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ../..
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion packages/erc20-watcher/src/cli/reset-cmds/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const handler = async (argv: any): Promise<void> => {

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');
Expand Down
72 changes: 14 additions & 58 deletions packages/erc20-watcher/src/cli/watch-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
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);
});
4 changes: 4 additions & 0 deletions packages/erc20-watcher/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export class Database implements DatabaseInterface {
this._baseDatabase = new BaseDatabase(this._config);
}

get baseDatabase (): BaseDatabase {
return this._baseDatabase;
}

async init (): Promise<void> {
this._conn = await this._baseDatabase.init();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/erc20-watcher/src/fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const main = async (): Promise<any> => {
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);

Expand Down
16 changes: 11 additions & 5 deletions packages/erc20-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -99,6 +101,10 @@ export class Indexer implements IndexerInterface {
return this._storageLayoutMap;
}

async init (): Promise<void> {
await this._baseIndexer.fetchContracts();
}

getResultEvent (event: Event): EventResult {
const eventFields = JSON.parse(event.eventInfo);

Expand Down
3 changes: 2 additions & 1 deletion packages/erc20-watcher/src/job-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export const main = async (): Promise<any> => {
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();
Expand Down
3 changes: 2 additions & 1 deletion packages/erc20-watcher/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export const main = async (): Promise<any> => {

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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const handler = async (argv: any): Promise<void> => {
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);
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-info-watcher/src/cli/export-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const main = async (): Promise<void> => {
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 = {
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-info-watcher/src/cli/import-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const main = async (): Promise<any> => {
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);
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-info-watcher/src/cli/inspect-cid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const main = async (): Promise<void> => {
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);
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-info-watcher/src/cli/reset-cmds/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const handler = async (argv: any): Promise<void> => {
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);
Expand Down
79 changes: 15 additions & 64 deletions packages/uni-info-watcher/src/cli/watch-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
const watchContractCmd = new WatchContractCmd();

const config: Config = await watchContractCmd.initConfig();
const {
uniWatcher,
tokenWatcher
Expand All @@ -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);
});
4 changes: 2 additions & 2 deletions packages/uni-info-watcher/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
DeepPartial,
FindConditions,
FindManyOptions,
In,
LessThanOrEqual,
QueryRunner,
Repository,
Expand Down Expand Up @@ -122,8 +121,9 @@ export class Database implements DatabaseInterface {
_graphDatabase: GraphDatabase
_relationsMap: Map<any, { [key: string]: any }>

constructor (config: ConnectionOptions, serverConfig: ServerConfig) {
constructor (config: ConnectionOptions, serverConfig?: ServerConfig) {
assert(config);
assert(serverConfig);
const entitiesDir = path.join(__dirname, 'entity/*');

this._config = {
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-info-watcher/src/fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const main = async (): Promise<any> => {
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) {
Expand Down
21 changes: 12 additions & 9 deletions packages/uni-info-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -77,15 +79,16 @@ export class Indexer implements IndexerInterface {
_subgraphStateMap: Map<string, any> = 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);
Expand Down
Loading