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

Fix codegen for non subgraph watchers #120

Merged
merged 2 commits into from
May 26, 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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/
dist/
out/

.vscode
4 changes: 2 additions & 2 deletions packages/codegen/src/checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/checkpoint-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 (outStream: Writable): void {
export function exportCheckpoint (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const checkpoint = template({});
const checkpoint = template({ subgraphPath });
outStream.write(checkpoint);
}
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): void {
export function exportState (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const exportState = template({});
const exportState = template({ subgraphPath });
outStream.write(exportState);
}
4 changes: 2 additions & 2 deletions packages/codegen/src/fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/fill-template.handlebars';
* Writes the fill file generated from a template to a stream.
* @param outStream A writable output stream to write the fill file to.
*/
export function exportFill (outStream: Writable): void {
export function exportFill (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const fill = template({});
const fill = template({ subgraphPath });
outStream.write(fill);
}
18 changes: 9 additions & 9 deletions packages/codegen/src/generate-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function generateWatcher (visitor: Visitor, contracts: any[], config: any) {
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/server.ts'))
: process.stdout;
exportServer(outStream);
exportServer(outStream, config.subgraphPath);

outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'environments/local.toml'))
Expand Down Expand Up @@ -228,17 +228,17 @@ function generateWatcher (visitor: Visitor, contracts: any[], config: any) {
outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/job-runner.ts'))
: process.stdout;
exportJobRunner(outStream);
exportJobRunner(outStream, config.subgraphPath);

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

outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/cli/checkpoint.ts'))
: process.stdout;
exportCheckpoint(outStream);
exportCheckpoint(outStream, config.subgraphPath);

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

outStream = outputDir
? fs.createWriteStream(path.join(outputDir, 'src/types.ts'))
Expand Down Expand Up @@ -284,22 +284,22 @@ function generateWatcher (visitor: Visitor, contracts: any[], config: any) {
resetStateOutStream = process.stdout;
}

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

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

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

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

function getConfig (configFile: string): any {
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): void {
export function importState (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const importState = template({});
const importState = template({ subgraphPath });
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): void {
export function exportInspectCID (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const inspectCid = template({});
const inspectCid = template({ subgraphPath });
outStream.write(inspectCid);
}
4 changes: 2 additions & 2 deletions packages/codegen/src/job-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/job-runner-template.handlebars';
* Writes the job-runner file generated from a template to a stream.
* @param outStream A writable output stream to write the events file to.
*/
export function exportJobRunner (outStream: Writable): void {
export function exportJobRunner (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const events = template({});
const events = template({ subgraphPath });
outStream.write(events);
}
5 changes: 3 additions & 2 deletions packages/codegen/src/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class Reset {
* @param resetJQOutStream A writable output stream to write the reset job-queue file to.
* @param resetStateOutStream A writable output stream to write the reset state file to.
*/
exportReset (resetOutStream: Writable, resetJQOutStream: Writable, resetStateOutStream: Writable): void {
exportReset (resetOutStream: Writable, resetJQOutStream: Writable, resetStateOutStream: Writable, subgraphPath: string): void {
const resetTemplate = Handlebars.compile(this._resetTemplateString);
const resetString = resetTemplate({});
resetOutStream.write(resetString);
Expand All @@ -86,7 +86,8 @@ export class Reset {

const resetStateTemplate = Handlebars.compile(this._resetStateTemplateString);
const obj = {
queries: this._queries
queries: this._queries,
subgraphPath
};
const resetState = resetStateTemplate(obj);
resetStateOutStream.write(resetState);
Expand Down
4 changes: 2 additions & 2 deletions packages/codegen/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/server-template.handlebars';
* Writes the server file generated from a template to a stream.
* @param outStream A writable output stream to write the server file to.
*/
export function exportServer (outStream: Writable): void {
export function exportServer (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const server = template({});
const server = template({ subgraphPath });
outStream.write(server);
}
2 changes: 2 additions & 0 deletions packages/codegen/src/templates/checkpoint-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ const main = async (): Promise<void> => {
await indexer.init();

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ const main = async (): Promise<void> => {
await indexer.init();

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

const exportData: any = {
snapshotBlock: {},
Expand Down
2 changes: 2 additions & 0 deletions packages/codegen/src/templates/fill-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export const main = async (): Promise<any> => {
await indexer.init();

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

// Note: In-memory pubsub works fine for now, as each watcher is a single process anyway.
// Later: https://www.apollographql.com/docs/apollo-server/data/subscriptions/#production-pubsub-libraries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export const main = async (): Promise<any> => {
await indexer.init();

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

const eventWatcher = new EventWatcher(config.upstream, ethClient, postgraphileClient, indexer, pubsub, jobQueue);

Expand Down
13 changes: 9 additions & 4 deletions packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { EthClient } from '@vulcanize/ipld-eth-client';
import { StorageLayout } from '@vulcanize/solidity-mapper';
import {
IPLDIndexer as BaseIndexer,
IndexerInterface,
IPLDIndexerInterface,
ValueResult,
UNKNOWN_EVENT_NAME,
ServerConfig,
Expand All @@ -26,7 +26,8 @@ import {
updateStateForMappingType,
BlockHeight,
IPFSClient,
StateKind
StateKind,
IpldStatus as IpldStatusInterface
} from '@vulcanize/util';
import { GraphWatcher } from '@vulcanize/graph-node';

Expand Down Expand Up @@ -94,7 +95,7 @@ export type ResultIPLDBlock = {
data: string;
};

export class Indexer implements IndexerInterface {
export class Indexer implements IPLDIndexerInterface {
_db: Database
_ethClient: EthClient
_ethProvider: BaseProvider
Expand Down Expand Up @@ -563,11 +564,15 @@ export class Indexer implements IndexerInterface {
}

async watchContract (address: string, kind: string, checkpoint: boolean, startingBlock: number): Promise<void> {
this._baseIndexer.updateIPLDStatusMap(address, {});
await this.updateIPLDStatusMap(address, {});

return this._baseIndexer.watchContract(address, kind, checkpoint, startingBlock);
}

async updateIPLDStatusMap (address: string, ipldStatus: IpldStatusInterface): Promise<void> {
await this._baseIndexer.updateIPLDStatusMap(address, ipldStatus);
}

cacheContract (contract: Contract): void {
return this._baseIndexer.cacheContract(contract);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ const main = async (): Promise<void> => {
await indexer.init();

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

const ipldBlock = await indexer.getIPLDBlockByCid(argv.cid);
assert(ipldBlock, 'IPLDBlock for the provided CID doesn\'t exist.');
Expand Down
2 changes: 2 additions & 0 deletions packages/codegen/src/templates/job-runner-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,12 @@ export const main = async (): Promise<any> => {
await indexer.init();

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

// Watching all the contracts in the subgraph.
await graphWatcher.addContracts();
{{/if}}

const jobRunner = new JobRunner(jobQueueConfig, indexer, jobQueue);
await jobRunner.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export const handler = async (argv: any): Promise<void> => {
await indexer.init();

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

const blockProgresses = await indexer.getBlocksAtHeight(argv.blockNumber, false);
assert(blockProgresses.length, `No blocks at specified block number ${argv.blockNumber}`);
Expand Down
2 changes: 2 additions & 0 deletions packages/codegen/src/templates/server-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export const main = async (): Promise<any> => {
await indexer.init();

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

const eventWatcher = new EventWatcher(config.upstream, ethClient, postgraphileClient, indexer, pubsub, jobQueue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ const main = async (): Promise<void> => {
await indexer.init();

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

await indexer.watchContract(argv.address, argv.kind, argv.checkpoint, argv.startingBlock);

Expand Down
4 changes: 2 additions & 2 deletions packages/codegen/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ export class Visitor {
* @param resetJQOutStream A writable output stream to write the reset job-queue file to.
* @param resetStateOutStream A writable output stream to write the reset state file to.
*/
exportReset (resetOutStream: Writable, resetJQOutStream: Writable, resetStateOutStream: Writable): void {
this._reset.exportReset(resetOutStream, resetJQOutStream, resetStateOutStream);
exportReset (resetOutStream: Writable, resetJQOutStream: Writable, resetStateOutStream: Writable, subgraphPath: string): void {
this._reset.exportReset(resetOutStream, resetJQOutStream, resetStateOutStream, subgraphPath);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/codegen/src/watch-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const TEMPLATE_FILE = './templates/watch-contract-template.handlebars';
* Writes the watch-contract file generated from a template to a stream.
* @param outStream A writable output stream to write the watch-contract file to.
*/
export function exportWatchContract (outStream: Writable): void {
export function exportWatchContract (outStream: Writable, subgraphPath: string): void {
const templateString = fs.readFileSync(path.resolve(__dirname, TEMPLATE_FILE)).toString();
const template = Handlebars.compile(templateString);
const events = template({});
const events = template({ subgraphPath });
outStream.write(events);
}
13 changes: 9 additions & 4 deletions packages/eden-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import { EthClient } from '@vulcanize/ipld-eth-client';
import { StorageLayout } from '@vulcanize/solidity-mapper';
import {
IPLDIndexer as BaseIndexer,
IndexerInterface,
UNKNOWN_EVENT_NAME,
ServerConfig,
JobQueue,
Where,
QueryOptions,
BlockHeight,
IPFSClient,
StateKind
StateKind,
IPLDIndexerInterface,
IpldStatus as IpldStatusInterface
} from '@vulcanize/util';
import { GraphWatcher } from '@vulcanize/graph-node';

Expand Down Expand Up @@ -125,7 +126,7 @@ export type ResultIPLDBlock = {
data: string;
};

export class Indexer implements IndexerInterface {
export class Indexer implements IPLDIndexerInterface {
_db: Database
_ethClient: EthClient
_ethProvider: BaseProvider
Expand Down Expand Up @@ -899,11 +900,15 @@ export class Indexer implements IndexerInterface {
}

async watchContract (address: string, kind: string, checkpoint: boolean, startingBlock: number): Promise<void> {
this._baseIndexer.updateIPLDStatusMap(address, {});
await this.updateIPLDStatusMap(address, {});

return this._baseIndexer.watchContract(address, kind, checkpoint, startingBlock);
}

async updateIPLDStatusMap (address: string, ipldStatus: IpldStatusInterface): Promise<void> {
await this._baseIndexer.updateIPLDStatusMap(address, ipldStatus);
}

cacheContract (contract: Contract): void {
return this._baseIndexer.cacheContract(contract);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/erc20-watcher/test/tasks/token-approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ task('token-approve', 'Move tokens to recipient')
const TransferEvent = receipt.events.find(el => el.event === 'Approval');

if (TransferEvent && TransferEvent.args) {
console.log('Approval Event');
console.log('Approval Event at block:', receipt.blockNumber, receipt.blockHash);
console.log('owner:', TransferEvent.args.owner.toString());
console.log('spender:', TransferEvent.args.spender.toString());
console.log('value:', TransferEvent.args.value.toString());
Expand Down
2 changes: 2 additions & 0 deletions packages/erc20-watcher/test/tasks/token-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ task('token-deploy', 'Deploys GLD token')
const Token = await hre.ethers.getContractFactory('GLDToken');
const token = await Token.deploy(hre.ethers.BigNumber.from(initialSupply));

const receipt = await token.deployTransaction.wait();
console.log('GLD Token deployed to:', token.address);
console.log('Deployed at block:', receipt.blockNumber, receipt.blockHash);
});
Loading