Skip to content

Commit

Permalink
feat: added filter to return only active validators and its balance
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Aug 31, 2024
1 parent 646f34b commit 7d2bd71
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
26 changes: 22 additions & 4 deletions server/api/[version]/index.get.ts
Original file line number Diff line number Diff line change
@@ -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
})
3 changes: 2 additions & 1 deletion server/utils/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
})
13 changes: 10 additions & 3 deletions server/utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Validator[]> {
export async function fetchValidators({ onlyPools = false, addresses = [] }: FetchValidatorsOptions): Result<Validator[]> {
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 }
Expand Down

0 comments on commit 7d2bd71

Please sign in to comment.