Skip to content

Commit

Permalink
Merge pull request #813 from alleslabs/feature/cfe-348-historical-powers
Browse files Browse the repository at this point in the history
[CFE-348] Feat: add historical powers API
  • Loading branch information
Poafs1 authored Mar 7, 2024
2 parents 0c02d0b + a6513b8 commit 4c38bcc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- [#813](https://github.com/alleslabs/celatone-frontend/pull/813) api v1 - validator historical powers
- [#812](https://github.com/alleslabs/celatone-frontend/pull/812) api v1 - validator delegation related txs, proposed blocks
- [#807](https://github.com/alleslabs/celatone-frontend/pull/807) api v1 - validator uptime
- [#804](https://github.com/alleslabs/celatone-frontend/pull/804) api v1 - validator info
Expand Down
1 change: 1 addition & 0 deletions src/lib/app-provider/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export enum CELATONE_QUERY_KEYS {
VALIDATORS = "CELATONE_QUERY_VALIDATORS",
VALIDATOR_DATA = "CELATONE_QUERY_VALIDATOR_DATA",
VALIDATOR_UPTIME = "CELATONE_QUERY_VALIDATOR_UPTIME",
VALIDATOR_HISTORICAL_POWERS = "CELATONE_QUERY_VALIDATOR_HISTORICAL_POWERS",
VALIDATOR_DELEGATION_RELATED_TXS = "CELATONE_QUERY_VALIDATOR_DELEGATION_RELATED_TXS",
VALIDATOR_PROPOSED_BLOCKS = "CELATONE_QUERY_VALIDATOR_PROPOSED_BLOCKS",
// FAUCET
Expand Down
26 changes: 25 additions & 1 deletion src/lib/services/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios from "axios";
import { z } from "zod";

import { CURR_THEME } from "env";
import type { Option, StakingShare, Validator, ValidatorAddr } from "lib/types";
import {
BlockVote,
zBig,
Expand All @@ -11,7 +12,6 @@ import {
zValidatorAddr,
zValidatorData,
} from "lib/types";
import type { Option, StakingShare, Validator, ValidatorAddr } from "lib/types";
import { parseWithError, removeSpecialChars, snakeToCamel } from "lib/utils";

import { zBlocksResponse } from "./block";
Expand Down Expand Up @@ -86,6 +86,30 @@ export const resolveValIdentity = async (
);
};

const zHistoricalPowersItem = z.object({
hour_rounded_timestamp: zUtcDate,
timestamp: zUtcDate,
voting_power: zBig,
});

export const zHistoricalPowersResponse = z
.object({
items: z.array(zHistoricalPowersItem),
total: z.number(),
})
.transform(snakeToCamel);
export type HistoricalPowersResponse = z.infer<
typeof zHistoricalPowersResponse
>;

export const getHistoricalPowers = async (
endpoint: string,
validatorAddr: ValidatorAddr
): Promise<HistoricalPowersResponse> =>
axios
.get(`${endpoint}/${validatorAddr}/historical-powers`)
.then(({ data }) => parseWithError(zHistoricalPowersResponse, data));

const zValidatorsResponse = z
.object({
items: z.array(zValidatorData),
Expand Down
14 changes: 14 additions & 0 deletions src/lib/services/validatorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
ValidatorUptimeResponse,
} from "./validator";
import {
getHistoricalPowers,
getValidator,
getValidatorData,
getValidatorDelegationRelatedTxs,
Expand Down Expand Up @@ -77,6 +78,19 @@ export const useValidatorImage = (
});
};

export const useValidatorHistoricalPowers = (validatorAddr: ValidatorAddr) => {
const endpoint = useBaseApiRoute("validators");

return useQuery(
[CELATONE_QUERY_KEYS.VALIDATOR_HISTORICAL_POWERS, endpoint],
async () => getHistoricalPowers(endpoint, validatorAddr),
{
retry: 1,
refetchOnWindowFocus: false,
}
);
};

export const useValidators = (
limit: number,
offset: number,
Expand Down

0 comments on commit 4c38bcc

Please sign in to comment.