Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: proposal validator votes hook #758

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;
}
Loading