mirror of
https://github.com/cerc-io/watcher-ts
synced 2026-05-08 00:04:17 +00:00
* Prune optional methods in indexer and database interfaces * Implement GQL optimization changes in codegen * Fix graph-node test indexer * Add demos to codegen package
124 lines
3.6 KiB
Handlebars
124 lines
3.6 KiB
Handlebars
//
|
|
// Copyright 2021 Vulcanize, Inc.
|
|
//
|
|
|
|
import assert from 'assert';
|
|
import 'reflect-metadata';
|
|
import yargs from 'yargs';
|
|
import { hideBin } from 'yargs/helpers';
|
|
import debug from 'debug';
|
|
import { PubSub } from 'graphql-subscriptions';
|
|
|
|
import { Config, getConfig, fillBlocks, JobQueue, DEFAULT_CONFIG_PATH, initClients } from '@cerc-io/util';
|
|
{{#if (subgraphPath)}}
|
|
import { GraphWatcher, Database as GraphDatabase } from '@cerc-io/graph-node';
|
|
{{/if}}
|
|
|
|
import { Database{{#if (subgraphPath)}}, ENTITY_QUERY_TYPE_MAP, ENTITY_TO_LATEST_ENTITY_MAP{{/if}} } from './database';
|
|
import { Indexer } from './indexer';
|
|
import { EventWatcher } from './events';
|
|
{{#if (subgraphPath)}}
|
|
import { fillState } from './fill-state';
|
|
{{/if}}
|
|
|
|
const log = debug('vulcanize:server');
|
|
|
|
export const main = async (): Promise<any> => {
|
|
const argv = await yargs(hideBin(process.argv)).parserConfiguration({
|
|
'parse-numbers': false
|
|
}).env(
|
|
'FILL'
|
|
).options({
|
|
configFile: {
|
|
alias: 'f',
|
|
type: 'string',
|
|
demandOption: true,
|
|
describe: 'configuration file path (toml)',
|
|
default: DEFAULT_CONFIG_PATH
|
|
},
|
|
startBlock: {
|
|
type: 'number',
|
|
demandOption: true,
|
|
describe: 'Block number to start processing at'
|
|
},
|
|
{{#if (subgraphPath)}}
|
|
state: {
|
|
type: 'boolean',
|
|
default: false,
|
|
describe: 'Fill state for subgraph entities'
|
|
},
|
|
{{/if}}
|
|
endBlock: {
|
|
type: 'number',
|
|
demandOption: true,
|
|
describe: 'Block number to stop processing at'
|
|
},
|
|
prefetch: {
|
|
type: 'boolean',
|
|
default: false,
|
|
describe: 'Block and events prefetch mode'
|
|
},
|
|
batchBlocks: {
|
|
type: 'number',
|
|
default: 10,
|
|
describe: 'Number of blocks prefetched in batch'
|
|
}
|
|
}).argv;
|
|
|
|
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, ENTITY_QUERY_TYPE_MAP, ENTITY_TO_LATEST_ENTITY_MAP);
|
|
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 (argv.state) {
|
|
assert(config.server.enableState, 'State creation disabled');
|
|
await fillState(indexer, graphDb, graphWatcher.dataSources, argv);
|
|
|
|
return;
|
|
}
|
|
{{/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
|
|
const pubsub = new PubSub();
|
|
|
|
const eventWatcher = new EventWatcher(ethClient, indexer, pubsub, jobQueue);
|
|
|
|
await fillBlocks(jobQueue, indexer, eventWatcher, jobQueueConfig.blockDelayInMilliSecs, argv);
|
|
};
|
|
|
|
main().catch(err => {
|
|
log(err);
|
|
}).finally(() => {
|
|
process.exit();
|
|
});
|
|
|
|
process.on('SIGINT', () => {
|
|
log(`Exiting process ${process.pid} with code 0`);
|
|
process.exit(0);
|
|
});
|