Skip to content

Commit 637c4e8

Browse files
committed
fix: invalidate
1 parent e44cbab commit 637c4e8

1 file changed

Lines changed: 55 additions & 81 deletions

File tree

src/components/transactions/GovVote/GovVoteActions.tsx

Lines changed: 55 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,58 @@ export const GovVoteActions = ({
235235
});
236236
}
237237

238+
const optimisticallyUpdateVote = () => {
239+
const power = parseFloat(args?.power || '0');
240+
const votingPowerWei = parseUnits(args?.power || '0', 18).toString();
241+
const updater = (old: ProposalDetailDisplay | null | undefined) => {
242+
if (!old?.voteProposalData) return old;
243+
const forVotes = old.voteInfo.forVotes + (support ? power : 0);
244+
const againstVotes = old.voteInfo.againstVotes + (support ? 0 : power);
245+
const total = forVotes + againstVotes;
246+
const currentDifferential = forVotes - againstVotes;
247+
return {
248+
...old,
249+
voteInfo: {
250+
...old.voteInfo,
251+
forVotes,
252+
againstVotes,
253+
forPercent: total > 0 ? forVotes / total : 0,
254+
againstPercent: total > 0 ? againstVotes / total : 0,
255+
currentDifferential,
256+
quorumReached: forVotes >= old.voteInfo.quorum,
257+
differentialReached: currentDifferential >= old.voteInfo.requiredDifferential,
258+
},
259+
voteProposalData: {
260+
...old.voteProposalData,
261+
votedInfo: { support, votingPower: votingPowerWei },
262+
},
263+
};
264+
};
265+
queryClient.setQueryData(['governance-detail-cache', proposalId, user], updater);
266+
queryClient.setQueryData(['governance-detail-graph', proposalId, user], updater);
267+
268+
queryClient.invalidateQueries({ queryKey: ['proposalVotes', proposalId] });
269+
queryClient.invalidateQueries({
270+
queryKey: ['governance-voters-cache-for', proposalId],
271+
});
272+
queryClient.invalidateQueries({
273+
queryKey: ['governance-voters-cache-against', proposalId],
274+
});
275+
queryClient.invalidateQueries({
276+
queryKey: ['governance-voters-graph', proposalId],
277+
});
278+
279+
// Invalidate the same detail queries we just wrote to. setQueryData above
280+
// gives instant UI feedback, while this triggers a background refetch to
281+
// replace the optimistic snapshot with real indexed data.
282+
queryClient.invalidateQueries({
283+
queryKey: ['governance-detail-cache', proposalId, user],
284+
});
285+
queryClient.invalidateQueries({
286+
queryKey: ['governance-detail-graph', proposalId, user],
287+
});
288+
};
289+
238290
const action = async () => {
239291
setMainTxState({ ...mainTxState, loading: true });
240292
try {
@@ -268,45 +320,7 @@ export const GovVoteActions = ({
268320
success: true,
269321
});
270322

271-
const power = parseFloat(args?.power || '0');
272-
const votingPowerWei = args?.power ? parseUnits(args.power, 18).toString() : '1';
273-
const updater = (old: ProposalDetailDisplay | null | undefined) => {
274-
if (!old?.voteProposalData) return old;
275-
const forVotes = old.voteInfo.forVotes + (support ? power : 0);
276-
const againstVotes = old.voteInfo.againstVotes + (support ? 0 : power);
277-
const total = forVotes + againstVotes;
278-
const currentDifferential = forVotes - againstVotes;
279-
return {
280-
...old,
281-
voteInfo: {
282-
...old.voteInfo,
283-
forVotes,
284-
againstVotes,
285-
forPercent: total > 0 ? forVotes / total : 0,
286-
againstPercent: total > 0 ? againstVotes / total : 0,
287-
currentDifferential,
288-
quorumReached: forVotes >= old.voteInfo.quorum,
289-
differentialReached: currentDifferential >= old.voteInfo.requiredDifferential,
290-
},
291-
voteProposalData: {
292-
...old.voteProposalData,
293-
votedInfo: { support, votingPower: votingPowerWei },
294-
},
295-
};
296-
};
297-
queryClient.setQueryData(['governance-detail-cache', proposalId, user], updater);
298-
queryClient.setQueryData(['governance-detail-graph', proposalId, user], updater);
299-
300-
queryClient.invalidateQueries({ queryKey: ['proposalVotes', proposalId] });
301-
queryClient.invalidateQueries({
302-
queryKey: ['governance-voters-cache-for', proposalId],
303-
});
304-
queryClient.invalidateQueries({
305-
queryKey: ['governance-voters-cache-against', proposalId],
306-
});
307-
queryClient.invalidateQueries({
308-
queryKey: ['governance-voters-graph', proposalId],
309-
});
323+
optimisticallyUpdateVote();
310324
return;
311325
} else {
312326
setTimeout(checkForStatus, 5000);
@@ -324,54 +338,14 @@ export const GovVoteActions = ({
324338
const txWithEstimatedGas = await estimateGasLimit(tx, votingChainId);
325339

326340
const response = await sendTx(txWithEstimatedGas);
327-
await response.wait(1);
341+
await response.wait(3);
328342
setMainTxState({
329343
txHash: response.hash,
330344
loading: false,
331345
success: true,
332346
});
333347

334-
// Optimistically update votedInfo and vote counts so the UI reflects
335-
// the vote immediately without waiting for the cache service to index
336-
const power = parseFloat(args?.power || '0');
337-
const votingPowerWei = args?.power ? parseUnits(args.power, 18).toString() : '1';
338-
const updater = (old: ProposalDetailDisplay | null | undefined) => {
339-
if (!old?.voteProposalData) return old;
340-
const forVotes = old.voteInfo.forVotes + (support ? power : 0);
341-
const againstVotes = old.voteInfo.againstVotes + (support ? 0 : power);
342-
const total = forVotes + againstVotes;
343-
const currentDifferential = forVotes - againstVotes;
344-
return {
345-
...old,
346-
voteInfo: {
347-
...old.voteInfo,
348-
forVotes,
349-
againstVotes,
350-
forPercent: total > 0 ? forVotes / total : 0,
351-
againstPercent: total > 0 ? againstVotes / total : 0,
352-
currentDifferential,
353-
quorumReached: forVotes >= old.voteInfo.quorum,
354-
differentialReached: currentDifferential >= old.voteInfo.requiredDifferential,
355-
},
356-
voteProposalData: {
357-
...old.voteProposalData,
358-
votedInfo: { support, votingPower: votingPowerWei },
359-
},
360-
};
361-
};
362-
queryClient.setQueryData(['governance-detail-cache', proposalId, user], updater);
363-
queryClient.setQueryData(['governance-detail-graph', proposalId, user], updater);
364-
365-
queryClient.invalidateQueries({ queryKey: ['proposalVotes', proposalId] });
366-
queryClient.invalidateQueries({
367-
queryKey: ['governance-voters-cache-for', proposalId],
368-
});
369-
queryClient.invalidateQueries({
370-
queryKey: ['governance-voters-cache-against', proposalId],
371-
});
372-
queryClient.invalidateQueries({
373-
queryKey: ['governance-voters-graph', proposalId],
374-
});
348+
optimisticallyUpdateVote();
375349
}
376350
} catch (err) {
377351
setMainTxState({

0 commit comments

Comments
 (0)