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';
|
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
import { Config as CacheConfig, getCache } from '@vulcanize/cache';
|
|
|
|
import { EthClient } from '@vulcanize/ipld-eth-client';
|
2021-11-01 06:01:54 +00:00
|
|
|
import { BaseProvider } 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;
|
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-29 11:33:22 +00:00
|
|
|
ipfsApiAddr: string;
|
2021-10-28 11:01:56 +00:00
|
|
|
subgraphPath: string;
|
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;
|
|
|
|
gqlPostgraphileEndpoint: string;
|
2021-10-26 12:06:21 +00:00
|
|
|
rpcProviderEndpoint: string;
|
|
|
|
blockDelayInMilliSecs: number;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-08-18 10:20:44 +00:00
|
|
|
jobQueue: JobQueueConfig
|
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,
|
|
|
|
postgraphileClient: EthClient,
|
|
|
|
ethProvider: BaseProvider
|
|
|
|
}> => {
|
|
|
|
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
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
const { ethServer: { gqlApiEndpoint, gqlPostgraphileEndpoint, 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(gqlPostgraphileEndpoint, 'Missing upstream ethServer.gqlPostgraphileEndpoint');
|
|
|
|
assert(rpcProviderEndpoint, 'Missing upstream ethServer.rpcProviderEndpoint');
|
|
|
|
|
|
|
|
const cache = await getCache(cacheConfig);
|
|
|
|
|
|
|
|
const ethClient = new EthClient({
|
|
|
|
gqlEndpoint: gqlApiEndpoint,
|
|
|
|
gqlSubscriptionEndpoint: gqlPostgraphileEndpoint,
|
|
|
|
cache
|
|
|
|
});
|
|
|
|
|
|
|
|
const postgraphileClient = new EthClient({
|
|
|
|
gqlEndpoint: gqlPostgraphileEndpoint,
|
|
|
|
cache
|
|
|
|
});
|
|
|
|
|
2021-10-25 14:21:16 +00:00
|
|
|
const ethProvider = getCustomProvider(rpcProviderEndpoint);
|
2021-10-20 10:36:03 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
ethClient,
|
|
|
|
postgraphileClient,
|
|
|
|
ethProvider
|
|
|
|
};
|
|
|
|
};
|