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: api v1 proposal votes info #762

Merged
merged 1 commit into from
Feb 2, 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

- [#762](https://github.com/alleslabs/celatone-frontend/pull/762) api v1 - proposal validator votes info
- [#745](https://github.com/alleslabs/celatone-frontend/pull/745) Add proposal top
- [#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
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_INFO = "CELATONE_QUERY_PROPOSAL_VALIDATOR_VOTES_INFO",
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",
Expand Down
29 changes: 28 additions & 1 deletion src/lib/services/proposal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Coin } from "@cosmjs/stargate";
import axios from "axios";
import big from "big.js";
import { z } from "zod";

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

const zProposalVotesInfoResponse = z
.object({
yes: z.string(),
abstain: z.string(),
no: z.string(),
no_with_veto: z.string(),
total_voting_power: z.string(),
})
.transform((val) => ({
yes: big(val.yes),
abstain: big(val.abstain),
no: big(val.no),
noWithVeto: big(val.no_with_veto),
totalVotingPower: big(val.total_voting_power),
}));
export type ProposalVotesInfoResponse = z.infer<
typeof zProposalVotesInfoResponse
>;

export const getProposalVotesInfo = async (
endpoint: string,
id: number
): Promise<ProposalVotesInfoResponse> =>
axios
.get(`${endpoint}/${encodeURIComponent(id)}/votes-info`)
.then(({ data }) => zProposalVotesInfoResponse.parse(data));

const zProposalVotesResponseItem = z
.object({
proposal_id: z.number().nonnegative(),
Expand All @@ -230,7 +258,6 @@ const zProposalVotesResponse = z.object({
items: z.array(zProposalVotesResponseItem),
total: z.number().nonnegative(),
});

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

export const getProposalValidatorVotes = async (
Expand Down
38 changes: 24 additions & 14 deletions src/lib/services/proposalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import type {
RelatedProposalsResponse,
UploadAccess,
VotingParamsInternal,
ProposalVotesInfoResponse,
} from "./proposal";
import {
fetchGovVotingParams,
Expand All @@ -55,6 +56,7 @@ import {
getProposalTypes,
getProposalData,
getProposalValidatorVotes,
getProposalVotesInfo,
} from "./proposal";

export const useProposals = (
Expand Down Expand Up @@ -226,16 +228,6 @@ export const useRelatedProposalsCountByModuleId = (
);
};

export const useProposalData = (id: number) => {
const endpoint = useBaseApiRoute("proposals");

return useQuery<ProposalDataResponse>(
[CELATONE_QUERY_KEYS.PROPOSAL_DATA, endpoint, id],
async () => getProposalData(endpoint, id),
{ retry: 1, keepPreviousData: true }
);
};

export interface MinDeposit {
amount: U<Token<Big>>;
denom: string;
Expand Down Expand Up @@ -326,12 +318,30 @@ export const useUploadAccessParams = (): UseQueryResult<UploadAccess> => {
);
};

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

return useQuery(
return useQuery<ProposalDataResponse>(
[CELATONE_QUERY_KEYS.PROPOSAL_DATA, endpoint, id],
async () => getProposalData(endpoint, id),
{ retry: 1, keepPreviousData: true }
);
};

export const useProposalVotesInfo = (id: number) => {
const endpoint = useBaseApiRoute("proposals");

return useQuery<ProposalVotesInfoResponse>(
[CELATONE_QUERY_KEYS.PROPOSAL_VALIDATOR_VOTES, endpoint, id],
async () => getProposalVotesInfo(endpoint, id),
{ retry: 1, refetchOnWindowFocus: false }
);
};

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

return useQuery<ProposalVotesResponse>(
[CELATONE_QUERY_KEYS.PROPOSAL_VALIDATOR_VOTES, endpoint, id],
async () => getProposalValidatorVotes(endpoint, id),
{ retry: 1, refetchOnWindowFocus: false }
Expand Down
Loading