From 2b0663202692ddcc5ea290a873241da99bb1c938 Mon Sep 17 00:00:00 2001 From: Andy Espagnolo Date: Thu, 29 Jun 2023 13:20:31 -0300 Subject: [PATCH] fix: voting power on proposal fetch (#1059) --- .../ProposalVoting/ProposalVotingSection.tsx | 2 +- src/hooks/useVotingPowerOnProposal.ts | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/components/Proposal/View/ProposalVoting/ProposalVotingSection.tsx b/src/components/Proposal/View/ProposalVoting/ProposalVotingSection.tsx index 39cd42813..0db98d8fa 100644 --- a/src/components/Proposal/View/ProposalVoting/ProposalVotingSection.tsx +++ b/src/components/Proposal/View/ProposalVoting/ProposalVotingSection.tsx @@ -70,7 +70,7 @@ const ProposalVotingSection = ({ totalVpOnProposal, hasEnoughToVote, isLoadingVp, - } = useVotingPowerOnProposal(account, delegators, isDelegationResultLoading, votes, proposal) + } = useVotingPowerOnProposal(account, delegators, isDelegationResultLoading, proposal) const { matchResult } = useVotesMatch(account, delegate) const voteDifference = matchResult.voteDifference diff --git a/src/hooks/useVotingPowerOnProposal.ts b/src/hooks/useVotingPowerOnProposal.ts index cefea9a48..873fcda76 100644 --- a/src/hooks/useVotingPowerOnProposal.ts +++ b/src/hooks/useVotingPowerOnProposal.ts @@ -24,23 +24,23 @@ export default function useVotingPowerOnProposal( address: string | null, delegators: string[] | null, isLoadingDelegators: boolean, - votes?: Record | null, proposal?: ProposalAttributes | null ) { const { vpDistribution, isLoadingVpDistribution } = useVotingPowerDistribution(address, proposal?.snapshot_id) const { data, isLoading } = useQuery({ - queryKey: [`vpDistribution#${address}#${proposal?.snapshot_id}`], + queryKey: [`votingPowerOnProposal#${address}#${proposal?.snapshot_id}`], queryFn: async () => { - if (!address || !proposal || isLoadingDelegators || isLoadingVpDistribution || !vpDistribution) { - return initialVotingPowerOnProposal + if (proposal?.snapshot_id) { + const votes: SnapshotVote[] = await SnapshotGraphql.get().getProposalVotes(proposal.snapshot_id) + const delegatedVp = getDelegatedVotingPowerOnProposal(vpDistribution, delegators, votes) + const addressVp = vpDistribution.own || 0 + return { addressVp, delegatedVp } } - const votes: SnapshotVote[] = await SnapshotGraphql.get().getProposalVotes(proposal.snapshot_id) - const delegatedVp = getDelegatedVotingPowerOnProposal(vpDistribution, delegators, votes) - const addressVp = vpDistribution.own || 0 - return { addressVp, delegatedVp } }, staleTime: DEFAULT_QUERY_STALE_TIME, + enabled: !!address && !!proposal && !isLoadingDelegators && !isLoadingVpDistribution && !!vpDistribution, }) + const vpOnProposal = data ?? initialVotingPowerOnProposal const totalVpOnProposal = vpOnProposal.addressVp + vpOnProposal.delegatedVp const hasEnoughToVote = totalVpOnProposal > MINIMUM_VP_REQUIRED_TO_VOTE && !isLoading