-
Notifications
You must be signed in to change notification settings - Fork 479
Fix/gov cache vote #2874
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
base: main
Are you sure you want to change the base?
Fix/gov cache vote #2874
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,13 +2,13 @@ import { ChainId } from '@aave/contract-helpers'; | |||||||||||||||||||||||||
| import { GelatoRelay } from '@gelatonetwork/relay-sdk'; | ||||||||||||||||||||||||||
| import { Trans } from '@lingui/macro'; | ||||||||||||||||||||||||||
| import { useQueryClient } from '@tanstack/react-query'; | ||||||||||||||||||||||||||
| import { AbiCoder, keccak256, RLP } from 'ethers/lib/utils'; | ||||||||||||||||||||||||||
| import { AbiCoder, keccak256, parseUnits, RLP } from 'ethers/lib/utils'; | ||||||||||||||||||||||||||
| import { useState } from 'react'; | ||||||||||||||||||||||||||
| import { MOCK_SIGNED_HASH } from 'src/helpers/useTransactionHandler'; | ||||||||||||||||||||||||||
| import { useGovernanceTokensAndPowers } from 'src/hooks/governance/useGovernanceTokensAndPowers'; | ||||||||||||||||||||||||||
| import { useModalContext } from 'src/hooks/useModal'; | ||||||||||||||||||||||||||
| import { useWeb3Context } from 'src/libs/hooks/useWeb3Context'; | ||||||||||||||||||||||||||
| import { VoteProposalData } from 'src/modules/governance/types'; | ||||||||||||||||||||||||||
| import { ProposalDetailDisplay, VoteProposalData } from 'src/modules/governance/types'; | ||||||||||||||||||||||||||
| import { useRootStore } from 'src/store/root'; | ||||||||||||||||||||||||||
| import { governanceV3Config } from 'src/ui-config/governanceConfig'; | ||||||||||||||||||||||||||
| import { getProvider } from 'src/utils/marketsAndNetworksConfig'; | ||||||||||||||||||||||||||
|
|
@@ -194,6 +194,7 @@ export const GovVoteActions = ({ | |||||||||||||||||||||||||
| setApprovalTxState, | ||||||||||||||||||||||||||
| approvalTxState, | ||||||||||||||||||||||||||
| setTxError, | ||||||||||||||||||||||||||
| args, | ||||||||||||||||||||||||||
| } = useModalContext(); | ||||||||||||||||||||||||||
| const user = useRootStore((store) => store.account); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -266,17 +267,46 @@ export const GovVoteActions = ({ | |||||||||||||||||||||||||
| loading: false, | ||||||||||||||||||||||||||
| success: true, | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| queryClient.invalidateQueries({ queryKey: ['governance_proposal', proposalId, user] }); | ||||||||||||||||||||||||||
| queryClient.invalidateQueries({ | ||||||||||||||||||||||||||
| queryKey: ['governance-detail-cache', proposalId, user], | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const power = parseFloat(args?.power || '0'); | ||||||||||||||||||||||||||
| const votingPowerWei = args?.power ? parseUnits(args.power, 18).toString() : '1'; | ||||||||||||||||||||||||||
| const updater = (old: ProposalDetailDisplay | null | undefined) => { | ||||||||||||||||||||||||||
| if (!old?.voteProposalData) return old; | ||||||||||||||||||||||||||
| const forVotes = old.voteInfo.forVotes + (support ? power : 0); | ||||||||||||||||||||||||||
| const againstVotes = old.voteInfo.againstVotes + (support ? 0 : power); | ||||||||||||||||||||||||||
| const total = forVotes + againstVotes; | ||||||||||||||||||||||||||
| const currentDifferential = forVotes - againstVotes; | ||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||
| ...old, | ||||||||||||||||||||||||||
| voteInfo: { | ||||||||||||||||||||||||||
| ...old.voteInfo, | ||||||||||||||||||||||||||
| forVotes, | ||||||||||||||||||||||||||
| againstVotes, | ||||||||||||||||||||||||||
| forPercent: total > 0 ? forVotes / total : 0, | ||||||||||||||||||||||||||
| againstPercent: total > 0 ? againstVotes / total : 0, | ||||||||||||||||||||||||||
| currentDifferential, | ||||||||||||||||||||||||||
| quorumReached: forVotes >= old.voteInfo.quorum, | ||||||||||||||||||||||||||
| differentialReached: currentDifferential >= old.voteInfo.requiredDifferential, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| voteProposalData: { | ||||||||||||||||||||||||||
| ...old.voteProposalData, | ||||||||||||||||||||||||||
| votedInfo: { support, votingPower: votingPowerWei }, | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| queryClient.setQueryData(['governance-detail-cache', proposalId, user], updater); | ||||||||||||||||||||||||||
| queryClient.setQueryData(['governance-detail-graph', proposalId, user], updater); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| queryClient.setQueryData(['governance-detail-cache', proposalId, user], updater); | |
| queryClient.setQueryData(['governance-detail-graph', proposalId, user], updater); | |
| const governanceDetailCacheKey = ['governance-detail-cache', proposalId, user] as const; | |
| const governanceDetailGraphKey = ['governance-detail-graph', proposalId, user] as const; | |
| const updatedCacheDetail = queryClient.setQueryData(governanceDetailCacheKey, updater); | |
| const updatedGraphDetail = queryClient.setQueryData(governanceDetailGraphKey, updater); | |
| if (updatedCacheDetail == null) { | |
| queryClient.invalidateQueries({ queryKey: governanceDetailCacheKey }); | |
| } | |
| if (updatedGraphDetail == null) { | |
| queryClient.invalidateQueries({ queryKey: governanceDetailGraphKey }); | |
| } |
Copilot
AI
Feb 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When args.power is undefined or null, the power calculation defaults to 0, but votingPowerWei defaults to '1'. This inconsistency means the vote counts will be updated with 0 but the votedInfo will show a voting power of 1 wei. These should be consistent - either both should be '0' or the function should validate that args.power is present before proceeding with the optimistic update.
| const votingPowerWei = args?.power ? parseUnits(args.power, 18).toString() : '1'; | |
| const votingPowerWei = parseUnits(args?.power || '0', 18).toString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revalidate detail queries after optimistic vote write
These setQueryData writes update the proposal detail cache optimistically but never trigger a follow-up refetch for governance-detail-*, so the optimistic snapshot can become the long-lived source of truth. In this repo, queries default to refetchOnWindowFocus: false (pages/_app.page.tsx) and useGovernanceProposalDetail also disables refetchOnMount/refetchOnReconnect, so if other users voted after this page was loaded (or any power mismatch occurs), the displayed totals/quorum/differential can stay incorrect for the session; keep the optimistic update, but add a delayed or conditional invalidation/refetch for the detail keys.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updater function is duplicated in two places (lines 273-296 and lines 338-361). Consider extracting this logic into a reusable function to improve maintainability and reduce the risk of inconsistent updates if one location is changed but not the other.