Skip to content

Commit

Permalink
Merge pull request #758 from alleslabs/feat/cfe-285-validator-votes
Browse files Browse the repository at this point in the history
feat: proposal validator votes hook
  • Loading branch information
evilpeach committed Feb 1, 2024
2 parents 4586ac9 + 5bd1bbf commit d07eb20
Show file tree
Hide file tree
Showing 5 changed files with 62 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

- [#758](https://github.com/alleslabs/celatone-frontend/pull/758) api v1 - proposal validator votes
- [#757](https://github.com/alleslabs/celatone-frontend/pull/757) api v1 - proposal data
- [#731](https://github.com/alleslabs/celatone-frontend/pull/731) Add proposal detail page structure
- [#749](https://github.com/alleslabs/celatone-frontend/pull/749) Add multi-type proposals
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 {
FAUCET_INFO = "CELATONE_QUERY_FAUCET_INFO",
// X/GOV
PROPOSAL_DATA = "CELATONE_QUERY_PROPOSAL_DATA",
PROPOSAL_VALIDATOR_VOTES = "CELATONE_QUERY_PROPOSAL_VALIDATOR_VOTES",
RELATED_PROPOSALS_BY_CONTRACT_ADDRESS = "CELATONE_QUERY_RELATED_PROPOSALS_BY_CONTRACT_ADDRESS",
PROPOSALS_BY_MODULE_ID = "CELATONE_QUERY_PROPOSALS_BY_MODULE_ID",
PROPOSALS_COUNT_BY_MODULE_ID = "CELATONE_QUERY_PROPOSALS_COUNT_BY_MODULE_ID",
Expand Down
32 changes: 32 additions & 0 deletions src/lib/services/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
zProposalStatus,
zProposalType,
zUtcDate,
zValidator,
} from "lib/types";
import type {
AccessConfigPermission,
Expand All @@ -20,6 +21,7 @@ import type {
ProposalData,
ProposalStatus,
ProposalType,
ProposalVote,
} from "lib/types";
import { snakeToCamel } from "lib/utils";

Expand Down Expand Up @@ -199,3 +201,33 @@ export const getProposalData = async (
axios
.get(`${endpoint}/${encodeURIComponent(id)}/info`)
.then(({ data }) => zProposalDataResponse.parse(data));

const zProposalVotesResponseItem = z
.object({
proposal_id: z.number().nonnegative(),
abstain: z.number().nonnegative(),
no: z.number().nonnegative(),
no_with_veto: z.number().nonnegative(),
yes: z.number().nonnegative(),
is_vote_weighted: z.boolean(),
validator: zValidator,
voter: zBechAddr.nullable(),
timestamp: zUtcDate.nullable(),
tx_hash: z.string().nullable(),
})
.transform<ProposalVote>(snakeToCamel);

const zProposalVotesResponse = z.object({
items: z.array(zProposalVotesResponseItem),
total: z.number().nonnegative(),
});

export type ProposalVotesResponse = z.infer<typeof zProposalVotesResponse>;

export const getProposalValidatorVotes = async (
endpoint: string,
id: number
): Promise<ProposalVotesResponse> =>
axios
.get(`${endpoint}/${encodeURIComponent(id)}/validator-votes`)
.then(({ data }) => zProposalVotesResponse.parse(data));
14 changes: 14 additions & 0 deletions src/lib/services/proposalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { useMovePoolInfos } from "./move";
import type {
DepositParamsInternal,
ProposalDataResponse,
ProposalVotesResponse,
ProposalsResponse,
RelatedProposalsResponse,
UploadAccess,
Expand All @@ -53,6 +54,7 @@ import {
getProposals,
getProposalTypes,
getProposalData,
getProposalValidatorVotes,
} from "./proposal";

export const useProposals = (
Expand Down Expand Up @@ -323,3 +325,15 @@ export const useUploadAccessParams = (): UseQueryResult<UploadAccess> => {
{ keepPreviousData: true, refetchOnWindowFocus: false }
);
};

export const useProposalValidatorVotes = (
id: number
): UseQueryResult<ProposalVotesResponse> => {
const endpoint = useBaseApiRoute("proposals");

return useQuery(
[CELATONE_QUERY_KEYS.PROPOSAL_VALIDATOR_VOTES, endpoint, id],
async () => getProposalValidatorVotes(endpoint, id),
{ retry: 1, refetchOnWindowFocus: false }
);
};
15 changes: 14 additions & 1 deletion src/lib/types/proposal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Coin } from "@cosmjs/amino";
import { z } from "zod";

import type { BechAddr, Nullable, Option } from "lib/types";
import type { BechAddr, Nullable, Option, Validator } from "lib/types";

export enum ProposalStatus {
DEPOSIT_PERIOD = "DepositPeriod",
Expand Down Expand Up @@ -90,3 +90,16 @@ export interface ProposalData extends Proposal {
version: string;
votingTime: Nullable<Date>;
}

export interface ProposalVote {
proposalId: number;
abstain: number;
no: number;
noWithVeto: number;
yes: number;
isVoteWeighted: boolean;
validator: Validator;
voter: Nullable<BechAddr>;
timestamp: Nullable<Date>;
txHash: Nullable<string>;
}

0 comments on commit d07eb20

Please sign in to comment.