-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added filter to return only active validators and its balance
- Loading branch information
Showing
3 changed files
with
34 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters