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

🌱 add query options to stakes and stakers endpoints #143

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 24 additions & 1 deletion src/modules/pos/dto/get-stakes.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { IsOptional, IsString } from 'class-validator';
import { Transform } from 'class-transformer';
import { IsNumber, IsOptional, IsString } from 'class-validator';
import { DEFAULT_STAKES_TO_FETCH } from 'src/utils/constants';

export class StakesQueryDto {
/**
Expand All @@ -21,4 +23,25 @@ export class StakesQueryDto {
@IsString()
@IsOptional()
name?: string;

/**
* Case-insensitive search by name, address or publicKey. Supports both partial and full text search.
*/
@IsString()
@IsOptional()
search?: string;

/**
* Limit the number of stakes fetched.
*/
@IsNumber()
@Transform(({ value }) => Number(value), { toClassOnly: true })
limit?: number = DEFAULT_STAKES_TO_FETCH;

/**
* Offset for the stakes fetched.
*/
@IsNumber()
@Transform(({ value }) => Number(value), { toClassOnly: true })
offset?: number = 0;
}
20 changes: 17 additions & 3 deletions src/modules/pos/pos.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
ValidatorSortTypes,
} from 'src/utils/controller-helpers';
import { ValidatorQueryDto } from './dto/get-validator.dto';
import { MAX_VALIDATORS_TO_FETCH } from 'src/utils/constants';
import { MAX_STAKES_TO_FETCH, MAX_VALIDATORS_TO_FETCH } from 'src/utils/constants';
import { Prisma } from '@prisma/client';
import { NodeApi, NodeApiService } from 'src/modules/node-api/node-api.service';
import {
Expand Down Expand Up @@ -216,7 +216,8 @@ export class PosController {
query: StakesQueryDto,
stakeKey: 'staker' | 'validatorAddress',
) {
const { address, publicKey, name } = query;
const { address, publicKey, name, search, limit, offset } = query;
const take = Math.min(limit, MAX_STAKES_TO_FETCH);

if (!address && !publicKey && !name) {
throw new BadRequestException('At least one of address, publicKey, or name must be provided');
Expand All @@ -225,8 +226,21 @@ export class PosController {
const account = await this.findAccount(this.prisma, query);
if (!account) throw new BadRequestException('Account not found');

const where: Prisma.StakeWhereInput = {
[stakeKey]: account.address,
...(search && {
OR: [
{ account: { address: { contains: search, mode: 'insensitive' } } },
{ account: { publicKey: { contains: search, mode: 'insensitive' } } },
{ account: { name: { contains: search, mode: 'insensitive' } } },
],
}),
};

const stakes = await this.prisma.stake.findMany({
where: { [stakeKey]: account.address },
where,
take,
skip: offset,
include: { account: true },
});

Expand Down
7 changes: 7 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ export const MAX_VALIDATORS_TO_FETCH = 100;
export const MINIMUM_VALIDATOR_WEIGHT = BigInt(1_000e8);
export const ACTIVE_VALIDATORS = 51;

////////////////////////////
/// Stakes ///
////////////////////////////

export const DEFAULT_STAKES_TO_FETCH = 20;
export const MAX_STAKES_TO_FETCH = 100;

////////////////////////////
/// Accounts ///
////////////////////////////
Expand Down
Loading