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

Merge stage to main #73

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,7 @@ mongo
# Docker compose ganarated files
db-data

.env
.env

# Ignore macOS files
.DS_Store
4 changes: 3 additions & 1 deletion src/modules/clusters/cli/cluster.transformer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import TimeAgo from 'javascript-time-ago';
import en from 'javascript-time-ago/locale/en';
import { CustomLogger } from '@cli/shared/services/logger.service';

TimeAgo.addLocale(en);
const timeAgo = new TimeAgo('en-US');
Expand Down Expand Up @@ -45,6 +46,7 @@ const textStatus = (item, extra: any) => {
* @returns List of custom formatted pod data
*/
export const transformClusterData = (items, extra: any) => {
const logger = new CustomLogger('ClusterDataTransformer');
const clusters = [];
try {
for (let i = 0; i < items.length; i++) {
Expand Down Expand Up @@ -80,7 +82,7 @@ export const transformClusterData = (items, extra: any) => {
return order.indexOf(a.status.text) - order.indexOf(b.status.text);
});
} catch (e) {
console.log(e);
logger.error(`Failed to transform cluster data. ${e}`);
}
return clusters;
};
5 changes: 4 additions & 1 deletion src/modules/earnings/cli/earning.transformer.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { CustomLogger } from '@cli/shared/services/logger.service';

/**
* It transforms the items object from sqlite into a custom format
* @param {Array<>} items list of clusters object from the sqlite
* @returns List of custom formatted pod data
*/
export const transformEarningData = items => {
const logger = new CustomLogger('EarningDataTransformer');
const earnings = [];
try {
for (let i = 0; i < items.length; i++) {
Expand All @@ -16,7 +19,7 @@ export const transformEarningData = items => {
});
}
} catch (e) {
console.log(e);
logger.error(`Failed to transform earning data. ${e}`);
}
return earnings;
};
2 changes: 1 addition & 1 deletion src/modules/webapp/metrics/metrics.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class MetricsController {
private async collectStats() {
const clustersActive = await this._clusterService.countActive();
const toLiquidate = await this._clusterService.countLiquidatable();
const liquidatorETHBalance = await this._web3Provider.getETHBalance();
const liquidatorETHBalance = await this._web3Provider.getLiquidatorETHBalance();
this._metricsService.totalActiveClusters.set(clustersActive);
this._metricsService.totalLiquidatableClusters.set(toLiquidate.total);
this._metricsService.burnt10LiquidatableClusters.set(toLiquidate.burnt10);
Expand Down
8 changes: 5 additions & 3 deletions src/services/worker/cron/burn-rates.cron.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { Injectable } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { Cron, CronExpression } from '@nestjs/schedule';

import { BurnRatesTask } from '../tasks/burn-rates.task';
import { CustomLogger } from '@cli/shared/services/logger.service';

@Injectable()
export class BurnRateCron {
private readonly _logger = new CustomLogger(BurnRatesTask.name);
constructor(private _burnRatesTask: BurnRatesTask) {}

@Cron('*/10 * * * * *')
@Cron(CronExpression.EVERY_10_SECONDS)
async syncBurnRates(): Promise<void> {
try {
await this._burnRatesTask.syncBurnRates();
} catch (e) {
console.log('syncBurnRates', e);
this._logger.error(e);
}
}
}
8 changes: 5 additions & 3 deletions src/services/worker/cron/fetch.cron.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { Injectable } from '@nestjs/common';

import { Cron } from '@nestjs/schedule';
import { Cron, CronExpression } from '@nestjs/schedule';
import { FetchTask } from '../tasks/fetch.task';
import { CustomLogger } from '@cli/shared/services/logger.service';

@Injectable()
export class FetchCron {
private readonly _logger = new CustomLogger(FetchCron.name);
constructor(private _fetchTask: FetchTask) {}

@Cron('* * * * * *')
@Cron(CronExpression.EVERY_SECOND)
async fetchNewValidators(): Promise<void> {
try {
await this._fetchTask.fetchAllEvents();
} catch (e) {
console.log(e);
this._logger.error(`Fetching new validators cron task failed. ${e}`);
}
}
}
8 changes: 5 additions & 3 deletions src/services/worker/cron/liquidation.cron.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { Injectable } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { Cron, CronExpression } from '@nestjs/schedule';

import { LiquidationTask } from '../tasks/liquidation.task';
import { CustomLogger } from '@cli/shared/services/logger.service';

@Injectable()
export class LiquidationCron {
private readonly _logger = new CustomLogger(LiquidationCron.name);
constructor(private _liquidationTask: LiquidationTask) {}

@Cron('*/10 * * * * *')
@Cron(CronExpression.EVERY_10_SECONDS)
async liquidate(): Promise<void> {
try {
await this._liquidationTask.liquidate();
} catch (e) {
console.log(e);
this._logger.error(e);
}
}
}
7 changes: 3 additions & 4 deletions src/services/worker/tasks/burn-rates.task.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { Injectable, Logger } from '@nestjs/common';
import { ConfService } from '@cli/shared/services/conf.service';
import { Injectable } from '@nestjs/common';
import {
Web3Provider,
ERROR_CLUSTER_LIQUIDATED,
} from '@cli/shared/services/web3.provider';
import { ClusterService } from '@cli/modules/clusters/cluster.service';
import { MetricsService } from '@cli/modules/webapp/metrics/services/metrics.service';
import { SystemService, SystemType } from '@cli/modules/system/system.service';
import { CustomLogger } from '@cli/shared/services/logger.service';

@Injectable()
export class BurnRatesTask {
private static CURRENT_BATCH_SIZE = 100;
private static BATCH_SIZE_RATIO = 0.9;
private static MINIMUM_BATCH_SIZE = 10;
private static isProcessLocked = false;
private readonly _logger = new Logger(BurnRatesTask.name);
private readonly _logger = new CustomLogger(BurnRatesTask.name);

constructor(
private _config: ConfService,
private _clusterService: ClusterService,
private _metricsService: MetricsService,
private _systemService: SystemService,
Expand Down
14 changes: 3 additions & 11 deletions src/services/worker/tasks/fetch.task.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Injectable, Logger } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { WorkerService } from '@cli/services/worker/worker.service';
import { SystemService, SystemType } from '@cli/modules/system/system.service';
import { MetricsService } from '@cli/modules/webapp/metrics/services/metrics.service';
import { Web3Provider } from '@cli/shared/services/web3.provider';
import { CustomLogger } from '@cli/shared/services/logger.service';

@Injectable()
export class FetchTask {
private static isProcessLocked = false;
private readonly _logger = new Logger(FetchTask.name);
private readonly _logger = new CustomLogger(FetchTask.name);

constructor(
private _systemService: SystemService,
Expand All @@ -33,15 +34,6 @@ export class FetchTask {
return;
}

try {
await this._web3Provider.getLiquidationThresholdPeriod();
// HERE we can validate the contract owner address
} catch (err) {
throw new Error(
`'The provided contract address is not valid. Error: ${err}`,
);
}

const latestSyncedBlockNumber = await this._systemService.get(
SystemType.GENERAL_LAST_BLOCK_NUMBER,
);
Expand Down
9 changes: 5 additions & 4 deletions src/services/worker/tasks/liquidation.task.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { LessThanOrEqual } from 'typeorm';
import { Injectable, Logger } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { Cluster } from '@cli/modules/clusters/cluster.entity';
import { ConfService } from '@cli/shared/services/conf.service';
import { ClusterService } from '@cli/modules/clusters/cluster.service';
import { MetricsService } from '@cli/modules/webapp/metrics/services/metrics.service';
import { SystemService, SystemType } from '@cli/modules/system/system.service';
import { Web3Provider } from '@cli/shared/services/web3.provider';
import { CustomLogger } from '@cli/shared/services/logger.service';

@Injectable()
export class LiquidationTask {
private static isProcessLocked = false;
private readonly _logger = new Logger(LiquidationTask.name);
private readonly _logger = new CustomLogger(LiquidationTask.name);

constructor(
private _config: ConfService,
Expand Down Expand Up @@ -316,12 +317,12 @@ export class LiquidationTask {
this._web3Provider.operatorIdsToArray(operatorIds).length;
transaction.gas = this._config.gasUsage(); // totalOperators
if (!transaction.gas) {
console.error(
this._logger.error(
`Gas group was not found for ${totalOperators} operators. Going to estimate transaction gas...`,
);
transaction.gas = await this.getGas(transaction);
} else {
console.info(
this._logger.log(
`Gas group was found for ${totalOperators} operators and is: ${transaction.gas}`,
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/services/worker/worker.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { Injectable, OnModuleInit } from '@nestjs/common';
import { SystemService, SystemType } from '@cli/modules/system/system.service';
import { ClusterService } from '@cli/modules/clusters/cluster.service';
import { EarningService } from '@cli/modules/earnings/earning.service';

import { Web3Provider } from '@cli/shared/services/web3.provider';
import { CustomLogger } from '@cli/shared/services/logger.service';

@Injectable()
export class WorkerService implements OnModuleInit {
private readonly _logger = new Logger(WorkerService.name);
private readonly _logger = new CustomLogger(WorkerService.name);

constructor(
private _clusterService: ClusterService,
Expand Down
31 changes: 15 additions & 16 deletions src/services/worker/worker.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dotenv/config';

import importJsx from 'import-jsx';
import React from 'react';
import path from 'path';
Expand All @@ -13,18 +11,20 @@ import {
import { Web3Provider } from '@cli/shared/services/web3.provider';
import { ConfService } from '@cli/shared/services/conf.service';
import { WorkerModule } from '@cli/services/worker/worker.module';
import { getAllowedLogLevels } from '@cli/shared/services/logging';
import { CustomLogger } from '@cli/shared/services/logger.service';
import { EarningService } from '@cli/modules/earnings/earning.service';
import { ClusterService } from '@cli/modules/clusters/cluster.service';
import { MetricsService } from '@cli/modules/webapp/metrics/services/metrics.service';

const logger = new CustomLogger('App');

async function bootstrapApi() {
const app = await NestFactory.create<NestExpressApplication>(
WorkerModule,
new ExpressAdapter(),
{ cors: true },
{ cors: true, logger: false },
);

app.enable('trust proxy');
app.enableCors();

Expand All @@ -42,22 +42,24 @@ async function bootstrapApi() {
);

const confService = app.select(WorkerModule).get(ConfService);

const port = confService.getNumber('PORT') || 3000;
await app.listen(port);

// eslint-disable-next-line no-console
console.info(`WebApp is running on port: ${port}`);
app.useLogger(logger);
logger.log(`WebApp is running on port: ${port}`);
logger.log(`Node url: ${confService.get('NODE_URL')}`);
logger.log(`Network: ${confService.get('SSV_SYNC')}`);
await app.get(Web3Provider).printConfig();
}

async function bootstrapCli() {
const app = await NestFactory.createApplicationContext(WorkerModule, {
logger: getAllowedLogLevels(),
logger: false,
autoFlushLogs: false,
bufferLogs: false,
});

app.useLogger(app.get(CustomLogger));
app.useLogger(logger);
logger.log('Starting Liquidation worker');

const confService = app.select(WorkerModule).get(ConfService);

Expand All @@ -80,15 +82,12 @@ async function bootstrapCli() {
}

async function bootstrap() {
process.on('unhandledRejection', error => {
console.error('[CRITICAL] unhandledRejection', error);
logger.log(`Liquidator starting`);
process.on('unhandledRejection', (error: Error) => {
logger.error(`[CRITICAL] unhandledRejection ${error} ${error.stack}`);
MetricsService.criticalStatus.set(0);
});

console.info('Starting API');
await bootstrapApi();

console.info('Starting Liquidator worker');
await bootstrapCli();
}

Expand Down
8 changes: 5 additions & 3 deletions src/shared/services/conf.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dotenv/config';
import { Injectable } from '@nestjs/common';
import { ArgumentParser } from 'argparse';
import { ConfigService } from '@nestjs/config';
Expand Down Expand Up @@ -61,14 +62,15 @@ export class ConfService extends ConfigService {
};

for (const envVarName of Object.keys(envVars)) {
process.env[envVarName] =
// First check if it exists in cli param
const found =
args[envVars[envVarName]] ||
// Then check if there is env variable
process.env[envVarName] ||
// Then check if there is default value
this[envVarName];

if (found) {
process.env[envVarName] = found;
}
if (!process.env[envVarName]) {
console.error(
'\x1b[31m%s\x1b[0m',
Expand Down
11 changes: 8 additions & 3 deletions src/shared/services/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import { ConsoleLogger, LogLevel } from '@nestjs/common';
import { getAllowedLogLevels } from '@cli/shared/services/logging';

export class CustomLogger extends ConsoleLogger {
protected context: string;
constructor(context: string) {
super();
this.context = context;
}
printMessages(messages, context = '', logLevel = 'log', writeStreamType) {
if (getAllowedLogLevels().indexOf(logLevel) === -1) {
return;
}
messages.forEach(message => {
const pidMessage = this.formatPid(process.pid);
const contextMessage = this.formatContext(context);
const pidMessage = ` ${process.pid} `;
const contextMessage = `${this.context} `;
const timestampDiff = this.updateAndGetTimestampDiff();
const formattedLogLevel = logLevel.toUpperCase().padStart(7, ' ');
const formattedLogLevel = logLevel.toUpperCase().padEnd(7, ' ');
const formattedMessage = this.formatMessage(
<LogLevel>logLevel,
message,
Expand Down
2 changes: 1 addition & 1 deletion src/shared/services/logging.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const getAllowedLogLevels = (): any => {
const logLevels: any = ['verbose', 'debug', 'log', 'warn', 'error'];
return logLevels.slice(
logLevels.indexOf(process.env.LOG_LEVEL || 'debug'),
logLevels.indexOf(process.env.LOG_LEVEL || 'log'),
logLevels.length,
);
};
Loading