Skip to content

Commit

Permalink
Merge pull request #35 from klayrHQ/update-validator
Browse files Browse the repository at this point in the history
Update validator
  • Loading branch information
Theezr authored Aug 18, 2024
2 parents 705ee97 + 0ed7762 commit a7be1c7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/validator/dto/get-validator-res.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,26 @@ export class GetValidatorResponseDto {
consecutiveMissedBlocks?: number;
reportMisbehaviorHeights?: string;
sharingCoefficients?: string;
status?: string;
}

export const getValidatorResponse: ApiResponseOptions = {
status: 200,
description: 'The validator have been successfully fetched.',
type: GetValidatorResponseDto,
isArray: false,
isArray: true,
};

export class GetValidatorCountsResponseDto {
active: number;
ineligible: number;
standby: number;
punished: number;
banned: number;
}

export const getValidatorCountsResponse: ApiResponseOptions = {
status: 200,
description: 'The validator counts have been successfully fetched.',
type: GetValidatorCountsResponseDto,
};
25 changes: 24 additions & 1 deletion src/validator/validator.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { Controller, Get, Query, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiTags, ApiResponse } from '@nestjs/swagger';
import { ValidatorRepoService } from './validator.repo-service';
import { GetValidatorResponseDto, getValidatorResponse } from './dto/get-validator-res.dto';
import {
GetValidatorCountsResponseDto,
GetValidatorResponseDto,
getValidatorCountsResponse,
getValidatorResponse,
} from './dto/get-validator-res.dto';
import { GatewayResponse, ValidatorSortTypes } from 'src/utils/controller-helpers';
import { ValidatorQueryDto } from './dto/get-validator.dto';
import { MAX_VALIDATORS_TO_FETCH } from 'src/utils/constants';
import { Prisma } from '@prisma/client';
import { NodeApiService } from 'src/node-api/node-api.service';
import { Generator } from 'src/node-api/types';
import { ValidatorStatus } from './types';

@ApiTags('Validators')
@Controller('pos')
Expand Down Expand Up @@ -62,6 +68,23 @@ export class ValidatorController {
});
}

@Get('validators/status-count')
@ApiResponse(getValidatorCountsResponse)
async countValidatorsByStatus(): Promise<GatewayResponse<{ [key: string]: number }>> {
const statuses = Object.values(ValidatorStatus);
const counts = await Promise.all(
statuses.map(async (status) => ({
[status]: await this.validatorRepoService.countValidators({ where: { status } }),
})),
);

const formattedCounts = counts.reduce((acc, countObj) => ({ ...acc, ...countObj }));
return {
data: formattedCounts,
meta: {},
};
}

private getValidatorResponse(
validator: Prisma.ValidatorGetPayload<{ include: { account: true } }>,
list: Generator[],
Expand Down

0 comments on commit a7be1c7

Please sign in to comment.