From 353a3a344b012500a22aa8cfd5aa4403c3d6523e Mon Sep 17 00:00:00 2001 From: songwongtp <16089160+songwongtp@users.noreply.github.com> Date: Mon, 4 Mar 2024 18:16:42 +0700 Subject: [PATCH 1/2] feat: api v1 validator info --- CHANGELOG.md | 1 + src/lib/app-provider/env.ts | 1 + src/lib/services/validator.ts | 22 ++++++++++++++++++++++ src/lib/services/validatorService.ts | 21 +++++++++++++++++++-- 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c5d135b4..6999ca024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Features +- [#804](https://github.com/alleslabs/celatone-frontend/pull/804) api v1 - validator info - [#803](https://github.com/alleslabs/celatone-frontend/pull/803) api v1 - validator list - [#799](https://github.com/alleslabs/celatone-frontend/pull/799) Add extra config single denom staking - [#796](https://github.com/alleslabs/celatone-frontend/pull/796) Depositors list in vote details section diff --git a/src/lib/app-provider/env.ts b/src/lib/app-provider/env.ts index bca6cd5ab..310487df5 100644 --- a/src/lib/app-provider/env.ts +++ b/src/lib/app-provider/env.ts @@ -50,6 +50,7 @@ export enum CELATONE_QUERY_KEYS { VALIDATOR_IDENTITY_BY_ADDRESS = "CELATONE_QUERY_VALIDATOR_IDENTITY_BY_ADDRESS", // VALIDATOR VALIDATORS = "CELATONE_QUERY_VALIDATORS", + VALIDATOR_DATA = "CELATONE_QUERY_VALIDATOR_DATA", // FAUCET FAUCET_INFO = "CELATONE_QUERY_FAUCET_INFO", // X/GOV diff --git a/src/lib/services/validator.ts b/src/lib/services/validator.ts index 95e391903..c5e767597 100644 --- a/src/lib/services/validator.ts +++ b/src/lib/services/validator.ts @@ -108,3 +108,25 @@ export const getValidators = async ( }, }) .then(({ data }) => parseWithError(zValidatorsResponse, data)); + +const zValidatorDataResponse = z + .object({ + info: zValidatorData.nullable(), + self_voting_power: z.string(), + total_voting_power: z.string(), + }) + .transform(({ info, self_voting_power, total_voting_power }) => ({ + info, + selfVotingPower: big(self_voting_power), + totalVotingPower: big(total_voting_power), + })); + +export type ValidatorDataResponse = z.infer; + +export const getValidatorData = async ( + endpoint: string, + validatorAddress: ValidatorAddr +) => + axios + .get(`${endpoint}/${encodeURIComponent(validatorAddress)}/info`) + .then(({ data }) => parseWithError(zValidatorDataResponse, data)); diff --git a/src/lib/services/validatorService.ts b/src/lib/services/validatorService.ts index 3aa7cb99d..9568ded3a 100644 --- a/src/lib/services/validatorService.ts +++ b/src/lib/services/validatorService.ts @@ -12,8 +12,13 @@ import { } from "lib/app-provider"; import type { Nullable, Option, Validator, ValidatorAddr } from "lib/types"; -import type { ValidatorsResponse } from "./validator"; -import { getValidator, getValidators, resolveValIdentity } from "./validator"; +import type { ValidatorDataResponse, ValidatorsResponse } from "./validator"; +import { + getValidator, + getValidatorData, + getValidators, + resolveValIdentity, +} from "./validator"; export const useValidator = ( validatorAddr: ValidatorAddr, @@ -93,3 +98,15 @@ export const useValidators = ( } ); }; + +export const useValidatorData = (validatorAddress: ValidatorAddr) => { + const endpoint = useBaseApiRoute("validators"); + + return useQuery( + [CELATONE_QUERY_KEYS.VALIDATOR_DATA, endpoint, validatorAddress], + async () => getValidatorData(endpoint, validatorAddress), + { + retry: 1, + } + ); +}; From b5b6affe86d847790cbd4acfa5b897e9bc8f0fa9 Mon Sep 17 00:00:00 2001 From: songwongtp <16089160+songwongtp@users.noreply.github.com> Date: Tue, 5 Mar 2024 14:56:47 +0700 Subject: [PATCH 2/2] fix: zod type --- src/lib/services/validator.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/lib/services/validator.ts b/src/lib/services/validator.ts index c5e767597..e05125bc3 100644 --- a/src/lib/services/validator.ts +++ b/src/lib/services/validator.ts @@ -84,7 +84,6 @@ const zValidatorsResponse = z total_voting_power: zBig, }) .transform(snakeToCamel); - export type ValidatorsResponse = z.infer; export const getValidators = async ( @@ -112,15 +111,10 @@ export const getValidators = async ( const zValidatorDataResponse = z .object({ info: zValidatorData.nullable(), - self_voting_power: z.string(), - total_voting_power: z.string(), + self_voting_power: zBig, + total_voting_power: zBig, }) - .transform(({ info, self_voting_power, total_voting_power }) => ({ - info, - selfVotingPower: big(self_voting_power), - totalVotingPower: big(total_voting_power), - })); - + .transform(snakeToCamel); export type ValidatorDataResponse = z.infer; export const getValidatorData = async (