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

feat: refactor startup #37

Merged
merged 11 commits into from
Dec 20, 2024
2 changes: 1 addition & 1 deletion db/migrations/1734354978596-Data.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 2 additions & 19 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,14 @@ services:
ports:
- "23798:${DB_PORT}"
env_file: ./envs/.env.init
indexer-init:
indexer:
build:
context: .
dockerfile: ./Dockerfile
restart: on-failure
depends_on:
- db
env_file: ./envs/.env.init
indexer-sepolia:
build:
context: .
dockerfile: ./Dockerfile
restart: on-failure
depends_on:
- indexer-init
env_file: ./envs/.env.sepolia
indexer-holesky:
build:
context: .
dockerfile: ./Dockerfile
restart: on-failure
depends_on:
- indexer-init
env_file: ./envs/.env.holesky
env_file: ./envs/.env.indexer.example
mj52951 marked this conversation as resolved.
Show resolved Hide resolved
api:
build:
context: .
Expand All @@ -42,4 +26,3 @@ services:
env_file: ./envs/.env.api
ports:
- "8000:8000"

10 changes: 0 additions & 10 deletions envs/.env.init.example

This file was deleted.

21 changes: 0 additions & 21 deletions envs/.env.sepolia.example

This file was deleted.

15 changes: 8 additions & 7 deletions src/api/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ import fastifyHealthCheck from "fastify-healthcheck";
import type { DataSource } from "typeorm";

import { initDatabase } from "../utils";
import { logger } from "../utils/logger";
import { getLogger } from "../utils/logger";

import { config as envPluginConfig } from "./config/env.config";
import { SWAGGER_CONFIG, SWAGGER_UI_CONFIG } from "./config/swagger.config";
import { routesPlugin } from "./services/plugins/routes";

export class App {
public readonly instance: FastifyInstance;

logger;
protected constructor(instance: FastifyInstance) {
this.instance = instance;
this.logger = getLogger();
}

public static async init(): Promise<App> {
Expand All @@ -44,13 +45,13 @@ export class App {
public async start(): Promise<void> {
try {
await this.instance.ready();
logger.info(this.instance.printRoutes());
this.logger.info(this.instance.printRoutes());
await this.instance.listen({
port: this.instance.config.SERVER_PORT,
host: this.instance.config.SERVER_ADDRESS,
});
} catch (error) {
logger.error("Error occurred during app startup: ", error);
this.logger.error("Error occurred during app startup: ", error);
await this.stop(undefined);
}
}
Expand All @@ -59,14 +60,14 @@ export class App {
await this.instance.db
.destroy()
.catch((error: Error) =>
logger.error(
this.logger.error(
`Error occurred during database closing because: ${error.message}`,
),
);
try {
await this.instance.close();
} catch (error) {
logger.error("Error occurred during server closing: ", error);
this.logger.error("Error occurred during server closing: ", error);
}

if (signal !== "TEST") {
Expand Down Expand Up @@ -107,7 +108,7 @@ export class App {
await this.instance.db.query("SELECT 1");
return true;
} catch (error) {
logger.error(
this.logger.error(
"Healthcheck: database connection check failed: ",
error,
);
Expand Down
7 changes: 4 additions & 3 deletions src/api/controllers/TransfersController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { FastifyReply, FastifyRequest } from "fastify";
import type { DataSource, FindOptionsWhere } from "typeorm";

import type { Deposit, Transfer } from "../../model";
import { logger } from "../../utils/logger";
import { getLogger } from "../../utils/logger";
import {
type ITransferBySender,
type ITransferByTxHash,
Expand All @@ -16,9 +16,10 @@ import { TransfersService } from "../services/dataAccess/transfers.service";

export class TransfersController {
private transfersService: TransfersService;

logger;
constructor(dataSource: DataSource) {
this.transfersService = new TransfersService(dataSource);
this.logger = getLogger();
}

public async getTransfers(
Expand All @@ -45,7 +46,7 @@ export class TransfersController {
});
await reply.status(200).send(transfersResult);
} catch (error) {
logger.error("Error occurred when fetching transfers", error);
this.logger.error("Error occurred when fetching transfers", error);
await reply.status(500).send({ error: "Internal server error" });
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SPDX-License-Identifier: LGPL-3.0-only
*/
import nodeCleanup from "node-cleanup";

import { logger } from "../utils/logger";
import { getLogger } from "../utils/logger";

import { App } from "./App";

Expand All @@ -17,12 +17,12 @@ App.init()
nodeCleanup.uninstall();
})
.catch((error) => {
logger.error("Error occurred while stopping the app", error);
getLogger().error("Error occurred while stopping the app", error);
process.exit(1);
});
});
await app.start();
})
.catch((error) =>
logger.error("Error occurred while initializing the app", error),
getLogger().error("Error occurred while initializing the app", error),
);
29 changes: 12 additions & 17 deletions src/indexer/config/envLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ The Licensed Work is (c) 2024 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/

import { NotFoundError } from "../../utils/error";

export type DbConfig = {
host: string;
name: string;
Expand All @@ -19,7 +21,6 @@ export type DomainMetadata = {

export type EnvVariables = {
sharedConfigURL: string;
domainMetadata: DomainMetadata;
dbConfig: DbConfig;
logLevel: string;
version: string;
Expand All @@ -31,17 +32,6 @@ export function getEnv(): EnvVariables {
throw new Error(`SHARED_CONFIG_URL is not defined in the environment.`);
}

const domainId = Number(process.env.DOMAIN_ID);
if (isNaN(domainId)) {
throw new Error(`DOMAIN_ID environment variable is invalid or not set.`);
}

const domainMetadata = process.env[`${domainId}_METADATA`];
if (!domainMetadata) {
throw new Error(`Environment variable ${domainId}_METADATA is not set`);
}
const parsedDomainMetadata = JSON.parse(domainMetadata) as DomainMetadata;

const dbHost = process.env.DB_HOST;
if (!dbHost) {
throw new Error(`DB_HOST is not defined in the environment.`);
Expand Down Expand Up @@ -72,11 +62,6 @@ export function getEnv(): EnvVariables {

return {
sharedConfigURL,
domainMetadata: {
domainId: domainId,
domainGateway: parsedDomainMetadata.domainGateway,
rpcUrl: parsedDomainMetadata.rpcUrl,
},
dbConfig: {
host: dbHost,
name: dbName,
Expand All @@ -88,3 +73,13 @@ export function getEnv(): EnvVariables {
version,
};
}

export function getDomainMetadata(domainID: string): DomainMetadata {
const domainMetadata = process.env[`${domainID}_METADATA`];
if (!domainMetadata) {
throw new NotFoundError(
`domain metadata not configured for domain: ${domainID}`,
);
}
return JSON.parse(domainMetadata) as DomainMetadata;
}
21 changes: 2 additions & 19 deletions src/indexer/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import type {
Resource,
} from "@buildwithsygma/core";

import { logger } from "../../utils/logger";

import type { EnvVariables } from "./envLoader";
import { getLogger } from "../../utils/logger";

export type SharedConfig = {
domains: Array<Domain>;
Expand Down Expand Up @@ -42,21 +40,6 @@ type Handler = {
address: string;
};

export async function getDomainConfig(envVars: EnvVariables): Promise<Domain> {
const sharedConfig = await fetchSharedConfig(envVars.sharedConfigURL);

const domainConfig = sharedConfig.domains.find(
(domain) => domain.id === envVars.domainMetadata.domainId,
);
if (!domainConfig) {
throw new Error(
`No configuration found for domain ID: ${envVars.domainMetadata.domainId}`,
);
}
domainConfig.gateway = envVars.domainMetadata.domainGateway;
return domainConfig;
}

export async function fetchSharedConfig(url: string): Promise<SharedConfig> {
try {
const response = await fetch(url);
Expand All @@ -67,7 +50,7 @@ export async function fetchSharedConfig(url: string): Promise<SharedConfig> {
}
return (await response.json()) as SharedConfig;
} catch (error) {
logger.error(
getLogger().error(
`Failed to fetch shared config for stage: ${process.env.STAGE || "unknown"}`,
error,
);
Expand Down
8 changes: 5 additions & 3 deletions src/indexer/evmIndexer/evmParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Log } from "@subsquid/evm-processor";
import { assertNotNull } from "@subsquid/evm-processor";
import type { JsonRpcProvider, Provider } from "ethers";
import { ethers } from "ethers";
import type winston from "winston";

import * as bridge from "../../abi/bridge";
import {
Expand All @@ -19,7 +20,6 @@ import {
} from "../../indexer/utils";
import { Domain, Resource, Route, Token } from "../../model";
import { NotFoundError } from "../../utils/error";
import { logger } from "../../utils/logger";
import type { Domain as DomainType } from "../config";
import type { IParser } from "../indexer";
import type {
Expand All @@ -41,8 +41,10 @@ type FeeDataResponse = {
export class EVMParser implements IParser {
private STATIC_FEE_DATA = "0x00";
private provider: JsonRpcProvider;
constructor(provider: JsonRpcProvider) {
private logger: winston.Logger;
constructor(provider: JsonRpcProvider, logger: winston.Logger) {
this.provider = provider;
this.logger = logger;
}

public async parseDeposit(
Expand Down Expand Up @@ -241,7 +243,7 @@ export class EVMParser implements IParser {
amount: fee.fee.toString(),
};
} catch (err) {
logger.error("Calculating fee failed", err);
this.logger.error("Calculating fee failed", err);
return {
tokenAddress: "",
amount: "0",
Expand Down
13 changes: 9 additions & 4 deletions src/indexer/evmIndexer/evmProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import type {
} from "@subsquid/evm-processor";
import { EvmBatchProcessor } from "@subsquid/evm-processor";
import type { Store } from "@subsquid/typeorm-store";
import type winston from "winston";

import * as bridge from "../../abi/bridge";
import { NotFoundError } from "../../utils/error";
import { logger } from "../../utils/logger";
import type { Domain } from "../config";
import type { IParser, IProcessor } from "../indexer";
import type {
Expand All @@ -25,9 +25,11 @@ import type {
export class EVMProcessor implements IProcessor {
private parser: IParser;
private rpcUrl: string;
constructor(parser: IParser, rpcUrl: string) {
private logger: winston.Logger;
constructor(parser: IParser, rpcUrl: string, logger: winston.Logger) {
this.parser = parser;
this.rpcUrl = rpcUrl;
this.logger = logger;
}
public getProcessor(domain: Domain): EvmBatchProcessor {
const evmProcessor = new EvmBatchProcessor()
Expand Down Expand Up @@ -69,6 +71,9 @@ export class EVMProcessor implements IProcessor {
const fees: FeeCollectedData[] = [];

for (const block of ctx.blocks) {
this.logger.info(
`Processing block ${block.header.height} on networ ${domain.name}(${domain.id})`,
);
for (const log of block.logs) {
try {
switch (log.topics[0]) {
Expand Down Expand Up @@ -97,12 +102,12 @@ export class EVMProcessor implements IProcessor {
}

default:
logger.error(`Unsupported log topic: ${log.topics[0]}`);
this.logger.error(`Unsupported log topic: ${log.topics[0]}`);
break;
}
} catch (error) {
if (error instanceof NotFoundError) {
logger.error(error.message);
this.logger.error(error.message);
} else {
throw error;
}
Expand Down
Loading
Loading