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 { BaseProvider } from '@ethersproject/providers';
|
|
|
|
import { Config as CacheConfig, getCache } from '@vulcanize/cache';
|
|
|
|
import { EthClient } from '@vulcanize/ipld-eth-client';
|
2021-07-02 10:56:32 +00:00
|
|
|
|
2021-10-25 14:21:16 +00:00
|
|
|
import { getCustomProvider } from './index';
|
|
|
|
|
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-08-18 10:20:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 10:36:03 +00:00
|
|
|
interface ServerConfig {
|
|
|
|
host: string;
|
|
|
|
port: number;
|
|
|
|
mode: string;
|
|
|
|
kind: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface UpstreamConfig {
|
|
|
|
cache: CacheConfig,
|
|
|
|
ethServer: {
|
|
|
|
gqlApiEndpoint: string;
|
|
|
|
gqlPostgraphileEndpoint: string;
|
|
|
|
rpcProviderEndpoint: string
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
export const getResetConfig = async (config: Config): Promise<{
|
|
|
|
dbConfig: ConnectionOptions,
|
|
|
|
serverConfig: ServerConfig,
|
|
|
|
upstreamConfig: UpstreamConfig,
|
|
|
|
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');
|
|
|
|
const { ethServer: { gqlApiEndpoint, gqlPostgraphileEndpoint, rpcProviderEndpoint }, cache: cacheConfig } = upstreamConfig;
|
|
|
|
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 {
|
|
|
|
dbConfig,
|
|
|
|
serverConfig,
|
|
|
|
upstreamConfig,
|
|
|
|
ethClient,
|
|
|
|
postgraphileClient,
|
|
|
|
ethProvider
|
|
|
|
};
|
|
|
|
};
|