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

Accommodate CLI refactoring changes to codegen #261

Merged
merged 12 commits into from
Nov 25, 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
6 changes: 3 additions & 3 deletions packages/codegen/src/checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const VERIFY_TEMPLATE_FILE = './templates/checkpoint-verify-template.handlebars'
* Writes the checkpoint file generated from a template to a stream.
* @param outStream A writable output stream to write the checkpoint file to.
*/
export function exportCheckpoint (checkpointOutStream: Writable, checkpointCreateOutStream: Writable, checkpointVerifyOutStream: Writable | undefined, subgraphPath: string): void {
export function exportCheckpoint (checkpointOutStream: Writable, checkpointCreateOutStream: Writable, checkpointVerifyOutStream: Writable | undefined): void {
const checkpointTemplateString = fs.readFileSync(path.resolve(__dirname, CHECKPOINT_TEMPLATE_FILE)).toString();
const checkpointTemplate = Handlebars.compile(checkpointTemplateString);
const checkpoint = checkpointTemplate({ subgraphPath });
const checkpoint = checkpointTemplate({});
checkpointOutStream.write(checkpoint);

const createCheckpointTemplateString = fs.readFileSync(path.resolve(__dirname, CREATE_TEMPLATE_FILE)).toString();
const createCheckpointTemplate = Handlebars.compile(createCheckpointTemplateString);
const createCheckpoint = createCheckpointTemplate({ subgraphPath });
const createCheckpoint = createCheckpointTemplate({});
checkpointCreateOutStream.write(createCheckpoint);

if (checkpointVerifyOutStream) {
Expand Down
3 changes: 3 additions & 0 deletions packages/codegen/src/data/entities/StateSyncStatus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ columns:
pgType: integer
tsType: number
columnType: Column
columnOptions:
- option: nullable
value: true
imports:
- toImport:
- Entity
Expand Down
4 changes: 2 additions & 2 deletions packages/codegen/src/export-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/export-state-template.handlebars';
* Writes the export-state file generated from a template to a stream.
* @param outStream A writable output stream to write the export-state file to.
*/
export function exportState (outStream: Writable, subgraphPath: string): void {
export function exportState (outStream: Writable): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const exportState = template({ subgraphPath });
const exportState = template({});
outStream.write(exportState);
}
11 changes: 1 addition & 10 deletions packages/codegen/src/fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,14 @@ import Handlebars from 'handlebars';
import { Writable } from 'stream';

const FILL_TEMPLATE_FILE = './templates/fill-template.handlebars';
const FILL_STATE_TEMPLATE_FILE = './templates/fill-state-template.handlebars';

/**
* Writes the fill file generated from a template to a stream.
* @param fillOutStream A writable output stream to write the fill file to.
* @param fillStateOutStream A writable output stream to write the fill state file to.
*/
export function exportFill (fillOutStream: Writable, fillStateOutStream: Writable | undefined, subgraphPath: string): void {
export function exportFill (fillOutStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, FILL_TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const fill = template({ subgraphPath });
fillOutStream.write(fill);

if (fillStateOutStream) {
const templateString = fs.readFileSync(path.resolve(__dirname, FILL_STATE_TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const fillState = template({});
fillStateOutStream.write(fillState);
}
}
35 changes: 14 additions & 21 deletions packages/codegen/src/generate-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { exportInspectCID } from './inspect-cid';
import { getSubgraphConfig } from './utils/subgraph';
import { exportIndexBlock } from './index-block';
import { exportSubscriber } from './subscriber';
import { exportReset } from './reset';

const main = async (): Promise<void> => {
const argv = await yargs(hideBin(process.argv))
Expand Down Expand Up @@ -238,7 +239,14 @@ function generateWatcher (visitor: Visitor, contracts: any[], config: any) {
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/cli/watch-contract.ts'))
: process.stdout;
exportWatchContract(outStream, config.subgraphPath);
exportWatchContract(outStream);

const resetOutStream = fs.createWriteStream(path.join(outputDir, 'src/cli/reset.ts'));
const resetJQOutStream = fs.createWriteStream(path.join(outputDir, 'src/cli/reset-cmds/job-queue.ts'));
const resetWatcherOutStream = fs.createWriteStream(path.join(outputDir, 'src/cli/reset-cmds/watcher.ts'));
const resetStateOutStream = fs.createWriteStream(path.join(outputDir, 'src/cli/reset-cmds/state.ts'));

exportReset(resetOutStream, resetJQOutStream, resetWatcherOutStream, resetStateOutStream);

let checkpointOutStream, checkpointCreateOutStream, checkpointVerifyOutStream;

Expand All @@ -256,7 +264,7 @@ function generateWatcher (visitor: Visitor, contracts: any[], config: any) {
}
}

exportCheckpoint(checkpointOutStream, checkpointCreateOutStream, checkpointVerifyOutStream, config.subgraphPath);
exportCheckpoint(checkpointOutStream, checkpointCreateOutStream, checkpointVerifyOutStream);

outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/hooks.ts'))
Expand All @@ -266,15 +274,7 @@ function generateWatcher (visitor: Visitor, contracts: any[], config: any) {
const fillOutStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/fill.ts'))
: process.stdout;

let fillStateOutStream;
if (config.subgraphPath) {
fillStateOutStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/fill-state.ts'))
: process.stdout;
}

exportFill(fillOutStream, fillStateOutStream, config.subgraphPath);
exportFill(fillOutStream, config.subgraphPath);

outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/types.ts'))
Expand All @@ -298,27 +298,20 @@ function generateWatcher (visitor: Visitor, contracts: any[], config: any) {
: process.stdout;
visitor.exportClient(outStream, schemaContent, path.join(outputDir, 'src/gql'));

const resetOutStream = fs.createWriteStream(path.join(outputDir, 'src/cli/reset.ts'));
const resetJQOutStream = fs.createWriteStream(path.join(outputDir, 'src/cli/reset-cmds/job-queue.ts'));
const resetWatcherOutStream = fs.createWriteStream(path.join(outputDir, 'src/cli/reset-cmds/watcher.ts'));
const resetStateOutStream = fs.createWriteStream(path.join(outputDir, 'src/cli/reset-cmds/state.ts'));

visitor.exportReset(resetOutStream, resetJQOutStream, resetWatcherOutStream, resetStateOutStream, config.subgraphPath);

outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/cli/export-state.ts'))
: process.stdout;
exportState(outStream, config.subgraphPath);
exportState(outStream);

outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/cli/import-state.ts'))
: process.stdout;
importState(outStream, config.subgraphPath);
importState(outStream);

outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/cli/inspect-cid.ts'))
: process.stdout;
exportInspectCID(outStream, config.subgraphPath);
exportInspectCID(outStream);

outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/cli/index-block.ts'))
Expand Down
4 changes: 2 additions & 2 deletions packages/codegen/src/import-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/import-state-template.handlebars';
* Writes the import-state file generated from a template to a stream.
* @param outStream A writable output stream to write the import-state file to.
*/
export function importState (outStream: Writable, subgraphPath: string): void {
export function importState (outStream: Writable): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const importState = template({ subgraphPath });
const importState = template({});
outStream.write(importState);
}
4 changes: 2 additions & 2 deletions packages/codegen/src/inspect-cid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/inspect-cid-template.handlebars';
* Writes the inspect-cid file generated from a template to a stream.
* @param outStream A writable output stream to write the inspect-cid file to.
*/
export function exportInspectCID (outStream: Writable, subgraphPath: string): void {
export function exportInspectCID (outStream: Writable): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const inspectCid = template({ subgraphPath });
const inspectCid = template({});
outStream.write(inspectCid);
}
113 changes: 27 additions & 86 deletions packages/codegen/src/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,90 +12,31 @@ const RESET_JQ_TEMPLATE_FILE = './templates/reset-job-queue-template.handlebars'
const RESET_WATCHER_TEMPLATE_FILE = './templates/reset-watcher-template.handlebars';
const RESET_STATE_TEMPLATE_FILE = './templates/reset-state-template.handlebars';

export class Reset {
_queries: Array<any>;
_resetTemplateString: string;
_resetJQTemplateString: string;
_resetWatcherTemplateString: string;
_resetStateTemplateString: string;

constructor () {
this._queries = [];
this._resetTemplateString = fs.readFileSync(path.resolve(__dirname, RESET_TEMPLATE_FILE)).toString();
this._resetJQTemplateString = fs.readFileSync(path.resolve(__dirname, RESET_JQ_TEMPLATE_FILE)).toString();
this._resetWatcherTemplateString = fs.readFileSync(path.resolve(__dirname, RESET_WATCHER_TEMPLATE_FILE)).toString();
this._resetStateTemplateString = fs.readFileSync(path.resolve(__dirname, RESET_STATE_TEMPLATE_FILE)).toString();
}

/**
* Stores the query to be passed to the template.
* @param name Name of the query.
*/
addQuery (name: string): void {
// Check if the query is already added.
if (this._queries.some(query => query.name === name)) {
return;
}

const queryObject = {
name,
entityName: ''
};

// eth_call mode: Capitalize first letter of entity name (balanceOf -> BalanceOf).
// storage mode: Capiltalize second letter of entity name (_balances -> _Balances).
queryObject.entityName = (name.charAt(0) === '_')
? `_${name.charAt(1).toUpperCase()}${name.slice(2)}`
: `${name.charAt(0).toUpperCase()}${name.slice(1)}`;

this._queries.push(queryObject);
}

addSubgraphEntities (subgraphSchemaDocument: any): void {
const subgraphTypeDefs = subgraphSchemaDocument.definitions;

subgraphTypeDefs.forEach((def: any) => {
if (def.kind !== 'ObjectTypeDefinition') {
return;
}

this._queries.push({
entityName: def.name.value
});
});
}

/**
* Writes the reset.ts, job-queue.ts, state.ts files generated from templates to respective streams.
* @param outStream A writable output stream to write the database file to.
*/

/**
* Writes the reset.ts, job-queue.ts, watcher.ts, state.ts files generated from templates to respective streams.
* @param resetOutStream A writable output stream to write the reset file to.
* @param resetJQOutStream A writable output stream to write the reset job-queue file to.
* @param resetWatcherOutStream A writable output stream to write the reset watcher file to.
* @param resetStateOutStream A writable output stream to write the reset state file to.
*/
exportReset (resetOutStream: Writable, resetJQOutStream: Writable, resetWatcherOutStream: Writable, resetStateOutStream: Writable, subgraphPath: string): void {
const resetTemplate = Handlebars.compile(this._resetTemplateString);
const resetString = resetTemplate({});
resetOutStream.write(resetString);

const resetJQTemplate = Handlebars.compile(this._resetJQTemplateString);
const resetJQString = resetJQTemplate({});
resetJQOutStream.write(resetJQString);

const resetWatcherTemplate = Handlebars.compile(this._resetWatcherTemplateString);
const obj = {
queries: this._queries,
subgraphPath
};
const resetWatcher = resetWatcherTemplate(obj);
resetWatcherOutStream.write(resetWatcher);

const resetStateTemplate = Handlebars.compile(this._resetStateTemplateString);
const resetState = resetStateTemplate({});
resetStateOutStream.write(resetState);
}
/**
* Writes the reset.ts, job-queue.ts, watcher.ts, state.ts files generated from templates to respective streams.
* @param resetOutStream A writable output stream to write the reset file to.
* @param resetJQOutStream A writable output stream to write the reset job-queue file to.
* @param resetWatcherOutStream A writable output stream to write the reset watcher file to.
* @param resetStateOutStream A writable output stream to write the reset state file to.
*/
export function exportReset (resetOutStream: Writable, resetJQOutStream: Writable, resetWatcherOutStream: Writable, resetStateOutStream: Writable): void {
const resetTemplateString = fs.readFileSync(path.resolve(__dirname, RESET_TEMPLATE_FILE)).toString();
const resetTemplate = Handlebars.compile(resetTemplateString);
const resetString = resetTemplate({});
resetOutStream.write(resetString);

const resetJQTemplateString = fs.readFileSync(path.resolve(__dirname, RESET_JQ_TEMPLATE_FILE)).toString();
const resetJQTemplate = Handlebars.compile(resetJQTemplateString);
const resetJQString = resetJQTemplate({});
resetJQOutStream.write(resetJQString);

const resetWatcherTemplateString = fs.readFileSync(path.resolve(__dirname, RESET_WATCHER_TEMPLATE_FILE)).toString();
const resetWatcherTemplate = Handlebars.compile(resetWatcherTemplateString);
const resetWatcher = resetWatcherTemplate({});
resetWatcherOutStream.write(resetWatcher);

const resetStateTemplateString = fs.readFileSync(path.resolve(__dirname, RESET_STATE_TEMPLATE_FILE)).toString();
const resetStateTemplate = Handlebars.compile(resetStateTemplateString);
const resetState = resetStateTemplate({});
resetStateOutStream.write(resetState);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@
// Copyright 2022 Vulcanize, Inc.
//

import debug from 'debug';
import assert from 'assert';

import { getConfig, initClients, JobQueue, Config } from '@cerc-io/util';
{{#if (subgraphPath)}}
import { GraphWatcher, Database as GraphDatabase } from '@cerc-io/graph-node';
{{/if}}
import { CreateCheckpointCmd } from '@cerc-io/cli';

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

const log = debug('vulcanize:checkpoint-create');

export const command = 'create';

export const desc = 'Create checkpoint';
Expand All @@ -33,39 +25,8 @@ export const builder = {
};

export const handler = async (argv: any): Promise<void> => {
const config: Config = await getConfig(argv.configFile);
const { ethClient, ethProvider } = await initClients(config);

const db = new Database(config.database);
await db.init();

{{#if (subgraphPath)}}
const graphDb = new GraphDatabase(config.server, db.baseDatabase);
await graphDb.init();

const graphWatcher = new GraphWatcher(graphDb, ethClient, ethProvider, config.server);

{{/if}}
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 });
await jobQueue.start();

const indexer = new Indexer(config.server, db, ethClient, ethProvider, jobQueue{{#if (subgraphPath)}}, graphWatcher{{/if}});
await indexer.init();
{{#if (subgraphPath)}}

graphWatcher.setIndexer(indexer);
await graphWatcher.init();
{{/if}}

const blockHash = await indexer.processCLICheckpoint(argv.address, argv.blockHash);

log(`Created a checkpoint for contract ${argv.address} at block-hash ${blockHash}`);
const createCheckpointCmd = new CreateCheckpointCmd();
await createCheckpointCmd.init(argv, Database, Indexer);

await db.close();
await createCheckpointCmd.exec();
};
Loading