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

feature(poc): add ui helper to create and vote (do not merge) #1686

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion src/pages/governance/[proposalId]/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export default function ProposalDetailPage({
}
}, [isChangeVoteClicked]);

console.log(proposal);

return (
<>
<Head title="Proposal Details" />
Expand Down Expand Up @@ -320,7 +322,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
id: proposalId,
masternode: MasternodeType.ALL,
cycle: proposal.currentCycle,
size: 10,
size: 1,
next: next,
});
const pages = CursorPagination.getPages(
Expand Down
68 changes: 67 additions & 1 deletion src/pages/governance/_components/ProposalTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
isValidOCGGithubUrl,
isValidOCGRedditUrl,
} from "utils/commons/LinkValidator";
import { PlaygroundRpcClient } from "@defichain/playground-api-client";
import { newPlaygroundClient } from "@contexts/WhaleContext";
import { isPlayground } from "@waveshq/walletkit-core";
import { ProposalDisplayName } from "./ProposalCard";
import { VoteModal } from "./VoteModal";
import { useCycleEndDate } from "../shared/useCycleEndTime";
Expand All @@ -27,14 +30,36 @@ export function ProposalTable({
currentBlockHeight,
currentBlockMedianTime,
userQueryProposalStatus,
masternodeId,
}: {
proposals: GovernanceProposal[];
currentBlockHeight: number;
currentBlockMedianTime: number;
userQueryProposalStatus: ListProposalsStatus;
masternodeId: string;
}) {
const [displayVoteModal, setDisplayVoteModal] = useState(false);

// TODO: remove testing code
const connection = useNetwork().connection;
async function voteDummyProposals(
proposalId: string,
masternodeId: string,
vote: string
): Promise<void> {
const playgroundRPC = new PlaygroundRpcClient(
newPlaygroundClient(connection)
);
console.log(
`Voted proposal: ${proposalId} with ${vote}, tx hash:`,
await playgroundRPC.call(
"votegov",
[proposalId, masternodeId, vote],
"number"
)
);
}

return (
<div>
<OverflowTable
Expand Down Expand Up @@ -67,6 +92,9 @@ export function ProposalTable({
currentBlockHeight={currentBlockHeight}
currentBlockMedianTime={currentBlockMedianTime}
userQueryProposalStatus={userQueryProposalStatus}
onDummyVote={(vote: string) =>
voteDummyProposals(proposal.proposalId, masternodeId, vote)
}
/>
{displayVoteModal && (
<VoteModal
Expand All @@ -89,11 +117,13 @@ function ProposalRow({
currentBlockHeight,
currentBlockMedianTime,
userQueryProposalStatus,
onDummyVote,
}: {
proposal: GovernanceProposal;
currentBlockHeight: number;
currentBlockMedianTime: number;
userQueryProposalStatus: ListProposalsStatus;
onDummyVote: (vote: string) => void;
}) {
const router = useRouter();
const { connection } = useNetwork();
Expand Down Expand Up @@ -212,7 +242,6 @@ function ProposalRow({
</div>
</a>
</OverflowTable.Cell>

{(userQueryProposalStatus === ListProposalsStatus.COMPLETED ||
userQueryProposalStatus === ListProposalsStatus.REJECTED) && (
<OverflowTable.Cell className="align-middle">
Expand All @@ -230,6 +259,43 @@ function ProposalRow({
</div>
</OverflowTable.Cell>
)}
{isPlayground(connection) && (
<OverflowTable.Cell className="align-middle dark:text-gray-100">
<button
type="button"
onClick={(e) => {
onDummyVote("yes");
e.stopPropagation();
}}
>
<div className="text-gray-900 dark:text-gray-100 font-medium flex flex-row items-center gap-x-1 px-1 pr-2 py-[2px] border hover:border-primary-200 focus:border-primary-400 rounded-[30px] w-fit">
Yes
</div>
</button>
<button
type="button"
onClick={(e) => {
onDummyVote("no");
e.stopPropagation();
}}
>
<div className="text-gray-900 dark:text-gray-100 font-medium flex flex-row items-center gap-x-1 px-1 pr-2 py-[2px] border hover:border-primary-200 focus:border-primary-400 rounded-[30px] w-fit">
No
</div>
</button>
<button
type="button"
onClick={(e) => {
onDummyVote("neutral");
e.stopPropagation();
}}
>
<div className="text-gray-900 dark:text-gray-100 font-medium flex flex-row items-center gap-x-1 px-1 pr-2 py-[2px] border hover:border-primary-200 focus:border-primary-400 rounded-[30px] w-fit">
Neutral
</div>
</button>
</OverflowTable.Cell>
)}
</OverflowTable.Row>
);
}
83 changes: 80 additions & 3 deletions src/pages/governance/index.page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import React from "react";
import React, { useState } from "react";
import { isPlayground } from "@waveshq/walletkit-core";
import { Container } from "@components/commons/Container";
import { ApiPagedResponse } from "@defichain/whale-api-client";
import {
CursorPage,
CursorPagination,
} from "@components/commons/CursorPagination";
import { getWhaleApiClient, getWhaleRpcClient } from "@contexts/WhaleContext";
import {
getWhaleApiClient,
getWhaleRpcClient,
newPlaygroundClient,
} from "@contexts/WhaleContext";
import { GetServerSidePropsContext } from "next";
import {
ListProposalsType,
Expand All @@ -15,11 +20,18 @@ import {
GovernanceProposal,
GovernanceProposalStatus,
} from "@defichain/whale-api-client/dist/api/governance";
import { useNetwork } from "@contexts/NetworkContext";
import { PlaygroundRpcClient } from "@defichain/playground-api-client";
import classNames from "classnames";
import { Link } from "@components/commons/link/Link";
import { EmptySection } from "@components/commons/sections/EmptySection";
import { ProposalCards } from "./_components/ProposalCard";
import { ProposalTable } from "./_components/ProposalTable";
import { Button } from "./_components/Button";
import {
getLocalStorageItem,
setLocalStorage,
} from "./shared/localStorageHelper";
import { OnChainGovernanceTitles } from "./enum/onChainGovernanceTitles";

interface OCGProps {
Expand All @@ -43,11 +55,48 @@ export default function OnChainGovernancePage({
allProposalsDetails,
proposals,
}: OCGProps) {
const connection = useNetwork().connection;
const userQueryProposalStatus = allProposalsDetails.userQueryProposalStatus;
const userQueryProposalType = allProposalsDetails.userQueryProposalType;
const [masternodeId, setMasterNodeID] = useState(
getLocalStorageItem("dummyMasternodeID") ?? ""
);

// const { currentYear, currentMonth } = getCurrentYearMonth();

// TODO remove this before release to prod
async function createDummyProposals(): Promise<void> {
const playgroundRPC = new PlaygroundRpcClient(
newPlaygroundClient(connection)
);
for (let i = 0; i < 5; i += 1) {
const governanceType = ["creategovvoc", "creategovcfp"];
const proposalType =
governanceType[Math.floor(Math.random() * governanceType.length)]; // get random governance type
const cfpData = {
title: `Title testing proposal ${new Date().getTime()}`,
amount: "100000000",
context: "https://github.com/DeFiCh/dfips/issues/238",
payoutAddress: "mswsMVsyGMj1FzDMbbxw2QW3KvQAv2FKiy",
cycles: i + 1,
};
const vocData = {
title: `Title testing proposal ${new Date().getTime()}`,
context:
"https://www.reddit.com/r/defiblockchain/comments/10l6451/cfp_defichainwiki_9000_dfi/",
};
const proposal = await playgroundRPC.call(
proposalType,
[proposalType === "creategovvoc" ? vocData : cfpData, []],
"number"
);
console.log(
`proposal created with id:${proposal} is created with ${proposalType}`
);
}
}
console.log(proposals);

return (
<div>
{/* TODO: uncomment to use announcement banner */
Expand All @@ -65,6 +114,33 @@ export default function OnChainGovernancePage({
</span>
</Container>
</div> */}

{isPlayground(connection) && (
<div className="text-center">
<Button
testId="dummy-proposal"
label="Create dummy proposal"
onClick={createDummyProposals}
customStyle="bg-primary-50 hover:bg-primary-100 rounded m-4"
/>
<input
className="border"
placeholder="set masternode here"
value={masternodeId}
onChange={(v) => {
setMasterNodeID(v.target.value);
}}
/>
<Button
label="set masternode"
testId="dummy-submit"
customStyle="bg-primary-50 hover:bg-primary-100 rounded m-4"
onClick={() => {
setLocalStorage("dummyMasternodeID", masternodeId);
}}
/>
</div>
)}
<Container className="md:pt-11 pt-10 pb-20">
<div className="flex md:flex-row flex-col">
<div className="flex flex-col grow md:justify-center">
Expand Down Expand Up @@ -168,6 +244,7 @@ export default function OnChainGovernancePage({
allProposalsDetails.currentBlockMedianTime
}
userQueryProposalStatus={userQueryProposalStatus}
masternodeId={masternodeId}
/>
</div>
<div className="md:hidden block mt-4">
Expand Down Expand Up @@ -373,7 +450,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
.listGovProposals({
type: userQueryProposalType,
status: userQueryProposalStatus,
size: 10,
size: 2,
next: next,
})
.catch(() => {
Expand Down