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(components): get proposal data from api #757

Merged
merged 4 commits into from
Jan 30, 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

- [#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
- [#753](https://github.com/alleslabs/celatone-frontend/pull/753) api v1 - proposal type filter
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 @@ -51,6 +51,7 @@ export enum CELATONE_QUERY_KEYS {
// FAUCET
FAUCET_INFO = "CELATONE_QUERY_FAUCET_INFO",
// X/GOV
PROPOSAL_DATA = "CELATONE_QUERY_PROPOSAL_DATA",
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
7 changes: 2 additions & 5 deletions src/lib/components/table/proposals/ProposalsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ export const ProposalsTable = ({
return isMobile ? (
<MobileTableContainer>
{proposals.map((proposal) => (
<ProposalsTableMobileCard
key={proposal.proposalId}
proposal={proposal}
/>
<ProposalsTableMobileCard key={proposal.id} proposal={proposal} />
))}
</MobileTableContainer>
) : (
Expand All @@ -44,7 +41,7 @@ export const ProposalsTable = ({
/>
{proposals.map((proposal) => (
<ProposalsTableRow
key={proposal.proposalId}
key={proposal.id}
proposal={proposal}
templateColumns={templateColumns}
boxShadow={boxShadow}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const ProposalsTableMobileCard = ({
<ExplorerLink
isReadOnly={isDepositFailed}
type="proposal_id"
value={proposal.proposalId.toString()}
value={proposal.id.toString()}
showCopyOnHover
/>
</Flex>
Expand Down Expand Up @@ -101,7 +101,7 @@ export const ProposalsTableMobileCard = ({
getNavigationUrl({
type: "proposal_id",
explorerConfig: explorerLink,
value: proposal.proposalId.toString(),
value: proposal.id.toString(),
lcdEndpoint,
})
);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/table/proposals/ProposalsTableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const ProposalsTableRow = ({
getNavigationUrl({
type: "proposal_id",
explorerConfig: explorerLink,
value: proposal.proposalId.toString(),
value: proposal.id.toString(),
lcdEndpoint,
})
);
Expand All @@ -74,7 +74,7 @@ export const ProposalsTableRow = ({
<ExplorerLink
isReadOnly={isDepositFailed}
type="proposal_id"
value={proposal.proposalId.toString()}
value={proposal.id.toString()}
showCopyOnHover
ampCopierSection="proposal-list"
/>
Expand Down
83 changes: 59 additions & 24 deletions src/lib/services/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import type { Coin } from "@cosmjs/stargate";
import axios from "axios";
import { z } from "zod";

import { zUtcDate, zProposalType, zBechAddr, zProposalStatus } from "lib/types";
import {
zBechAddr,
zCoin,
zProposalStatus,
zProposalType,
zUtcDate,
} from "lib/types";
import type {
AccessConfigPermission,
BechAddr,
Expand All @@ -11,6 +17,7 @@ import type {
Proposal,
Option,
BechAddr20,
ProposalData,
ProposalStatus,
ProposalType,
} from "lib/types";
Expand Down Expand Up @@ -72,29 +79,18 @@ export const getProposalTypes = async (endpoint: string) =>
.get(`${endpoint}/types`)
.then(({ data }) => zProposalType.array().parse(data));

const zProposalsResponseItem = z
.object({
deposit_end_time: zUtcDate,
id: z.number().nonnegative(),
is_expedited: z.boolean(),
proposer: zBechAddr,
resolved_height: z.number().nullable(),
status: zProposalStatus,
title: z.string(),
types: zProposalType.array(),
voting_end_time: zUtcDate.nullable(),
})
.transform<Proposal>((val) => ({
depositEndTime: val.deposit_end_time,
proposalId: val.id,
isExpedited: val.is_expedited,
proposer: val.proposer,
resolvedHeight: val.resolved_height,
status: val.status,
title: val.title,
types: val.types,
votingEndTime: val.voting_end_time,
}));
const zProposal = z.object({
deposit_end_time: zUtcDate,
id: z.number().nonnegative(),
is_expedited: z.boolean(),
proposer: zBechAddr,
resolved_height: z.number().nullable(),
status: zProposalStatus,
title: z.string(),
types: zProposalType.array(),
voting_end_time: zUtcDate.nullable(),
});
const zProposalsResponseItem = zProposal.transform<Proposal>(snakeToCamel);

const zProposalsResponse = z.object({
items: z.array(zProposalsResponseItem),
Expand Down Expand Up @@ -164,3 +160,42 @@ export const getRelatedProposalsByContractAddress = async (
}
)
.then(({ data }) => zRelatedProposalsResponse.parse(data));

const zProposalDataResponse = z.object({
info: zProposal
.extend({
created_height: z.number().nullable(),
created_timestamp: zUtcDate.nullable(),
created_tx_hash: z.string().nullable(),
description: z.string(),
messages: z.unknown().array().nullable(),
metadata: z.string(),
proposal_deposits: z
.object({
amount: zCoin.array(),
depositor: zBechAddr,
timestamp: zUtcDate,
tx_hash: z.string(),
})
.array(),
resolved_timestamp: zUtcDate.nullable(),
submit_time: zUtcDate,
total_deposit: zCoin.array(),
version: z.string(),
voting_time: zUtcDate.nullable(),
})
.transform<ProposalData>(({ messages, ...val }) => ({
...snakeToCamel(val),
messages,
}))
.nullable(),
});
export type ProposalDataResponse = z.infer<typeof zProposalDataResponse>;

export const getProposalData = async (
endpoint: string,
id: number
): Promise<ProposalDataResponse> =>
axios
.get(`${endpoint}/${encodeURIComponent(id)}/info`)
.then(({ data }) => zProposalDataResponse.parse(data));
14 changes: 13 additions & 1 deletion src/lib/services/proposalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { useAssetInfos } from "./assetService";
import { useMovePoolInfos } from "./move";
import type {
DepositParamsInternal,
ProposalDataResponse,
ProposalsResponse,
RelatedProposalsResponse,
UploadAccess,
Expand All @@ -51,6 +52,7 @@ import {
getRelatedProposalsByContractAddress,
getProposals,
getProposalTypes,
getProposalData,
} from "./proposal";

export const useProposals = (
Expand Down Expand Up @@ -163,7 +165,7 @@ export const useRelatedProposalsByModuleIdPagination = (
})
.then(({ module_proposals }) =>
module_proposals.map<Proposal>((proposal) => ({
proposalId: proposal.proposal_id,
id: proposal.proposal_id,
title: proposal.proposal.title,
status: proposal.proposal.status as ProposalStatus,
votingEndTime: parseDate(proposal.proposal.voting_end_time),
Expand Down Expand Up @@ -222,6 +224,16 @@ 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
5 changes: 5 additions & 0 deletions src/lib/types/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { z } from "zod";

import type { PoolInfo, Option, Token, U, USD } from "lib/types";

export const zCoin = z.object({
denom: z.string(),
amount: z.string(),
});

export const zAssetInfo = z.object({
coingecko: z.string(),
description: z.string(),
Expand Down
25 changes: 24 additions & 1 deletion src/lib/types/proposal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Coin } from "@cosmjs/amino";
import { z } from "zod";

import type { BechAddr, Nullable, Option } from "lib/types";
Expand Down Expand Up @@ -57,7 +58,7 @@ export const zProposalType = z.union([
export type ProposalType = z.infer<typeof zProposalType>;

export interface Proposal {
proposalId: number;
id: number;
title: string;
status: ProposalStatus;
votingEndTime: Nullable<Date>;
Expand All @@ -67,3 +68,25 @@ export interface Proposal {
proposer: Option<BechAddr>;
isExpedited: boolean;
}

export interface ProposalDeposit {
amount: Coin[];
depositor: BechAddr;
timestamp: Date;
txHash: string;
}

export interface ProposalData extends Proposal {
createdHeight: Nullable<number>;
createdTimestamp: Nullable<Date>;
createdTxHash: Nullable<string>;
jennieramida marked this conversation as resolved.
Show resolved Hide resolved
description: string;
messages: Nullable<unknown[]>;
metadata: string;
proposalDeposits: ProposalDeposit[];
resolvedTimestamp: Nullable<Date>;
submitTime: Date;
totalDeposit: Coin[];
version: string;
votingTime: Nullable<Date>;
}
Loading