2021-08-12 09:58:13 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 Vulcanize, Inc.
|
|
|
|
//
|
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
import assert from 'assert';
|
2021-07-02 10:56:32 +00:00
|
|
|
import fs from 'fs-extra';
|
|
|
|
import path from 'path';
|
|
|
|
import toml from 'toml';
|
|
|
|
import debug from 'debug';
|
|
|
|
import { ConnectionOptions } from 'typeorm';
|
|
|
|
|
2022-09-09 11:43:01 +00:00
|
|
|
import { Config as CacheConfig, getCache } from '@cerc-io/cache';
|
|
|
|
import { EthClient } from '@cerc-io/ipld-eth-client';
|
2022-07-22 07:47:56 +00:00
|
|
|
import { JsonRpcProvider } from '@ethersproject/providers';
|
2021-07-02 10:56:32 +00:00
|
|
|
|
2021-10-26 12:06:21 +00:00
|
|
|
import { getCustomProvider } from './misc';
|
2021-10-25 14:21:16 +00:00
|
|
|
|
2021-07-02 10:56:32 +00:00
|
|
|
const log = debug('vulcanize:config');
|
|
|
|
|
2021-08-18 10:20:44 +00:00
|
|
|
export interface JobQueueConfig {
|
|
|
|
dbConnectionString: string;
|
2021-08-24 06:25:29 +00:00
|
|
|
maxCompletionLagInSecs: number;
|
|
|
|
jobDelayInMilliSecs?: number;
|
2021-12-10 05:14:10 +00:00
|
|
|
eventsInBatch: number;
|
2022-10-11 08:11:26 +00:00
|
|
|
lazyUpdateBlockProgress?: boolean;
|
|
|
|
subgraphEventsOrder: boolean;
|
2022-10-20 13:16:56 +00:00
|
|
|
blockDelayInMilliSecs: number;
|
2022-10-13 11:37:46 +00:00
|
|
|
prefetchBlocksInMem: boolean;
|
|
|
|
prefetchBlockCount: number;
|
2021-08-18 10:20:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 10:32:56 +00:00
|
|
|
export interface ServerConfig {
|
2021-10-20 10:36:03 +00:00
|
|
|
host: string;
|
|
|
|
port: number;
|
|
|
|
mode: string;
|
|
|
|
kind: string;
|
2021-10-12 10:32:56 +00:00
|
|
|
checkpointing: boolean;
|
|
|
|
checkpointInterval: number;
|
2021-10-28 11:01:56 +00:00
|
|
|
subgraphPath: string;
|
2022-09-09 10:53:41 +00:00
|
|
|
disableSubgraphState: boolean;
|
2021-12-21 10:28:17 +00:00
|
|
|
wasmRestartBlocksInterval: number;
|
2022-07-18 08:34:59 +00:00
|
|
|
filterLogs: boolean;
|
2022-07-22 07:47:56 +00:00
|
|
|
maxEventsBlockRange: number;
|
2022-10-19 08:56:10 +00:00
|
|
|
clearEntitiesCacheInterval: number;
|
2021-10-20 10:36:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 12:06:21 +00:00
|
|
|
export interface UpstreamConfig {
|
2021-10-20 10:36:03 +00:00
|
|
|
cache: CacheConfig,
|
|
|
|
ethServer: {
|
|
|
|
gqlApiEndpoint: string;
|
2021-10-26 12:06:21 +00:00
|
|
|
rpcProviderEndpoint: string;
|
2021-10-20 10:36:03 +00:00
|
|
|
}
|
|
|
|
traceProviderEndpoint: string;
|
|
|
|
uniWatcher: {
|
|
|
|
gqlEndpoint: string;
|
|
|
|
gqlSubscriptionEndpoint: string;
|
2021-07-02 10:56:32 +00:00
|
|
|
};
|
2021-10-20 10:36:03 +00:00
|
|
|
tokenWatcher: {
|
|
|
|
gqlEndpoint: string;
|
|
|
|
gqlSubscriptionEndpoint: string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 08:47:43 +00:00
|
|
|
export interface GQLMetricsConfig {
|
|
|
|
port: number;
|
|
|
|
}
|
|
|
|
|
2022-08-03 10:56:51 +00:00
|
|
|
export interface MetricsConfig {
|
|
|
|
host: string;
|
|
|
|
port: number;
|
2022-09-01 08:47:43 +00:00
|
|
|
gql: GQLMetricsConfig;
|
2022-08-03 10:56:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
export interface Config {
|
|
|
|
server: ServerConfig;
|
2021-07-02 10:56:32 +00:00
|
|
|
database: ConnectionOptions;
|
2021-10-20 10:36:03 +00:00
|
|
|
upstream: UpstreamConfig,
|
2022-08-03 10:56:51 +00:00
|
|
|
jobQueue: JobQueueConfig,
|
|
|
|
metrics: MetricsConfig,
|
2021-07-02 10:56:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getConfig = async (configFile: string): Promise<Config> => {
|
|
|
|
const configFilePath = path.resolve(configFile);
|
|
|
|
const fileExists = await fs.pathExists(configFilePath);
|
|
|
|
if (!fileExists) {
|
|
|
|
throw new Error(`Config file not found: ${configFilePath}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const config = toml.parse(await fs.readFile(configFilePath, 'utf8'));
|
|
|
|
log('config', JSON.stringify(config, null, 2));
|
|
|
|
|
|
|
|
return config;
|
|
|
|
};
|
2021-10-20 10:36:03 +00:00
|
|
|
|
2021-11-01 06:01:54 +00:00
|
|
|
export const initClients = async (config: Config): Promise<{
|
2021-10-20 10:36:03 +00:00
|
|
|
ethClient: EthClient,
|
2022-07-20 05:07:17 +00:00
|
|
|
ethProvider: JsonRpcProvider
|
2021-10-20 10:36:03 +00:00
|
|
|
}> => {
|
|
|
|
const { database: dbConfig, upstream: upstreamConfig, server: serverConfig } = config;
|
|
|
|
|
|
|
|
assert(serverConfig, 'Missing server config');
|
|
|
|
assert(dbConfig, 'Missing database config');
|
|
|
|
assert(upstreamConfig, 'Missing upstream config');
|
2021-11-01 06:01:54 +00:00
|
|
|
|
2022-06-08 06:43:52 +00:00
|
|
|
const { ethServer: { gqlApiEndpoint, rpcProviderEndpoint }, cache: cacheConfig } = upstreamConfig;
|
2021-11-01 06:01:54 +00:00
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
assert(gqlApiEndpoint, 'Missing upstream ethServer.gqlApiEndpoint');
|
|
|
|
assert(rpcProviderEndpoint, 'Missing upstream ethServer.rpcProviderEndpoint');
|
|
|
|
|
|
|
|
const cache = await getCache(cacheConfig);
|
|
|
|
|
|
|
|
const ethClient = new EthClient({
|
|
|
|
gqlEndpoint: gqlApiEndpoint,
|
|
|
|
cache
|
|
|
|
});
|
|
|
|
|
2021-10-25 14:21:16 +00:00
|
|
|
const ethProvider = getCustomProvider(rpcProviderEndpoint);
|
2021-10-20 10:36:03 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
ethClient,
|
|
|
|
ethProvider
|
|
|
|
};
|
|
|
|
};
|