diff --git a/server/api/[version]/index.get.ts b/server/api/[version]/index.get.ts index 2007ccb..e324352 100644 --- a/server/api/[version]/index.get.ts +++ b/server/api/[version]/index.get.ts @@ -1,8 +1,26 @@ -import { poolQuerySchema } from '../../utils/schemas' +import { mainQuerySchema } from '../../utils/schemas' import { fetchValidators } from '../../utils/validators' +import { getRpcClient } from '~~/server/lib/client' export default defineEventHandler(async (event) => { - const { onlyPools } = await getValidatedQuery(event, poolQuerySchema.parse) - const validators = await fetchValidators({ onlyPools }) - return { validators } + const { onlyPools, onlyActive } = await getValidatedQuery(event, mainQuerySchema.parse) + + if (!onlyActive) + return await fetchValidators({ onlyPools }) + + const { data: activeValidators, error: errorActiveValidators } = await getRpcClient().blockchain.getActiveValidators() + if (errorActiveValidators) + return createError(errorActiveValidators) + const { data: validators, error: errorValidators } = await fetchValidators({ onlyPools, addresses: activeValidators.map(v => v.address) }) + if (errorValidators || !validators) + return createError(errorValidators) + + for (const validator of validators) { + // @ts-expect-error this is a hack to add the balance to the validator object + // A better solution would be to add a balance field to the Validator type + // and update the fetchValidators function to include the balance + validator.balance = activeValidators.find(v => v.address === validator.address)?.balance + } + + return validators }) diff --git a/server/utils/schemas.ts b/server/utils/schemas.ts index 0cc83a2..6054c18 100644 --- a/server/utils/schemas.ts +++ b/server/utils/schemas.ts @@ -22,6 +22,7 @@ export const validatorSchema = z.object({ icon: z.string().optional(), }) -export const poolQuerySchema = z.object({ +export const mainQuerySchema = z.object({ onlyPools: z.literal('true').or(z.literal('false')).default('false').transform(v => v === 'true'), + onlyActive: z.literal('true').or(z.literal('false')).default('true').transform(v => v === 'true'), }) diff --git a/server/utils/validators.ts b/server/utils/validators.ts index 78e5f58..eb7529e 100644 --- a/server/utils/validators.ts +++ b/server/utils/validators.ts @@ -119,14 +119,21 @@ export async function fetchValidatorsScoreByIds(validatorIds: number[]): Result< } export interface FetchValidatorsOptions { - onlyPools: boolean + onlyPools?: boolean + addresses?: string[] } -export async function fetchValidators({ onlyPools }: FetchValidatorsOptions): Result { +export async function fetchValidators({ onlyPools = false, addresses = [] }: FetchValidatorsOptions): Result { + const filters = [] + if (onlyPools) + filters.push(eq(tables.validators.isPool, true)) + if (addresses?.length > 0) + filters.push(inArray(tables.validators.address, addresses)) + const validators = await useDrizzle() .select() .from(tables.validators) - .where(onlyPools ? eq(tables.validators.isPool, true) : undefined) + .where(and(...filters)) .groupBy(tables.validators.id) .all() return { data: validators, error: undefined }