Skip to content

Commit

Permalink
Validate voting power in vote form
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrossy committed Apr 27, 2024
1 parent 8e2b193 commit 4e9c649
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/features/governance/VoteForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { FormSubmitButton } from 'src/components/buttons/FormSubmitButton';
import { RadioField } from 'src/components/input/RadioField';
import { ProposalFormDetails } from 'src/features/governance/components/ProposalFormDetails';
import { useProposalDequeue } from 'src/features/governance/hooks/useProposalQueue';
import { useGovernanceVoteRecord } from 'src/features/governance/hooks/useVotingStatus';
import {
useGovernanceVoteRecord,
useGovernanceVotingPower,
} from 'src/features/governance/hooks/useVotingStatus';
import { VoteFormValues, VoteType, VoteTypes } from 'src/features/governance/types';
import { getVoteTxPlan } from 'src/features/governance/votePlan';
import { OnConfirmedFn } from 'src/features/transactions/types';
import { useTransactionPlan } from 'src/features/transactions/useTransactionPlan';
import { useWriteContractWithReceipt } from 'src/features/transactions/useWriteContractWithReceipt';
import { isNullish } from 'src/utils/typeof';
import { useAccount } from 'wagmi';

const initialValues: VoteFormValues = {
Expand All @@ -24,6 +28,7 @@ export function VoteForm({
onConfirmed: OnConfirmedFn;
}) {
const { address } = useAccount();
const { votingPower } = useGovernanceVotingPower(address);
const { dequeue } = useProposalDequeue();
const { refetch: refetchVoteRecord } = useGovernanceVoteRecord(
address,
Expand All @@ -49,8 +54,8 @@ export function VoteForm({
const onSubmit = (values: VoteFormValues) => writeContract(getNextTx(values));

const validate = (values: VoteFormValues) => {
if (!address || !dequeue) return { amount: 'Form data not ready' };
return validateForm(values, dequeue);
if (!address || !dequeue || isNullish(votingPower)) return { amount: 'Form data not ready' };
return validateForm(values, dequeue, votingPower);
};

return (
Expand Down Expand Up @@ -98,7 +103,11 @@ function VoteTypeField({
);
}

function validateForm(values: VoteFormValues, dequeue: number[]): FormikErrors<VoteFormValues> {
function validateForm(
values: VoteFormValues,
dequeue: number[],
votingPower: bigint,
): FormikErrors<VoteFormValues> {
const { vote, proposalId } = values;

if (!vote || !VoteTypes.includes(vote)) {
Expand All @@ -109,5 +118,9 @@ function validateForm(values: VoteFormValues, dequeue: number[]): FormikErrors<V
return { proposalId: 'Proposal ID not eligible' };
}

if (votingPower <= 0n) {
return { vote: 'No voting power available' };
}

return {};
}

0 comments on commit 4e9c649

Please sign in to comment.