From 5b55410c1003153b539e10265617fe3966e8a465 Mon Sep 17 00:00:00 2001 From: J M Rossy Date: Sat, 2 Mar 2024 10:26:38 -0500 Subject: [PATCH] Skip revocation event fetching for performance --- src/features/governance/VoteForm.tsx | 2 +- .../governance/hooks/useProposalVoters.ts | 35 +++++++++---------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/features/governance/VoteForm.tsx b/src/features/governance/VoteForm.tsx index 07cf964..346dea9 100644 --- a/src/features/governance/VoteForm.tsx +++ b/src/features/governance/VoteForm.tsx @@ -35,7 +35,7 @@ export function VoteForm({ onStepSuccess: () => refetchVoteRecord(), onPlanSuccess: (v, r) => onConfirmed({ - message: `Vote successful`, + message: 'Vote successful', receipt: r, properties: [ { label: 'Vote', value: v.vote }, diff --git a/src/features/governance/hooks/useProposalVoters.ts b/src/features/governance/hooks/useProposalVoters.ts index 9f52e88..bf6f6f8 100644 --- a/src/features/governance/hooks/useProposalVoters.ts +++ b/src/features/governance/hooks/useProposalVoters.ts @@ -39,20 +39,20 @@ export async function fetchProposalVoters(id: number): Promise<{ totals: VoteAmounts; }> { // Get encoded topics - const { castVoteTopics, revokeVoteTopics } = - id > PROPOSAL_V1_MAX_ID ? getV2Topics(id) : getV1Topics(id); + const { castVoteTopics } = id > PROPOSAL_V1_MAX_ID ? getV2Topics(id) : getV1Topics(id); - // Prep query URLs + const voterToVotes: AddressTo = {}; const castVoteParams = `topic0=${castVoteTopics[0]}&topic1=${castVoteTopics[1]}&topic0_1_opr=and`; - const revokeVoteParams = `topic0=${revokeVoteTopics[0]}&topic1=${revokeVoteTopics[1]}&topic0_1_opr=and`; const castVoteEvents = await queryCeloscanLogs(Addresses.Governance, castVoteParams); - const revokeVoteEvents = await queryCeloscanLogs(Addresses.Governance, revokeVoteParams); - - const voterToVotes: AddressTo = {}; reduceLogs(voterToVotes, castVoteEvents, true); - reduceLogs(voterToVotes, revokeVoteEvents, false); - // Filter out stakers who have already revoked all votes + // Skipping revoke vote events for now for performance since they are almost never used + // Much more common is for a voter to re-vote, replacing the old vote + // const revokeVoteParams = `topic0=${revokeVoteTopics[0]}&topic1=${revokeVoteTopics[1]}&topic0_1_opr=and`; + // const revokeVoteEvents = await queryCeloscanLogs(Addresses.Governance, revokeVoteParams); + // reduceLogs(voterToVotes, revokeVoteEvents, false); + + // Filter out voters with no current votes const voters = objFilter( voterToVotes, (_, votes): votes is VoteAmounts => bigIntSum(Object.values(votes)) > 0n, @@ -71,7 +71,7 @@ export async function fetchProposalVoters(id: number): Promise<{ return { voters, totals }; } -function reduceLogs(voterToVotes: AddressTo, logs: TransactionLog[], isAdd: boolean) { +function reduceLogs(voterToVotes: AddressTo, logs: TransactionLog[], isCast: boolean) { for (const log of logs) { try { if (!log.topics || log.topics.length < 3) continue; @@ -104,15 +104,14 @@ function reduceLogs(voterToVotes: AddressTo, logs: TransactionLog[] if (!account || !isValidAddress(account)) continue; - voterToVotes[account] ||= { ...EmptyVoteAmounts }; - if (isAdd) { - voterToVotes[account][VoteType.Yes] += yesVotes || 0n; - voterToVotes[account][VoteType.No] += noVotes || 0n; - voterToVotes[account][VoteType.Abstain] += abstainVotes || 0n; + if (isCast) { + voterToVotes[account] = { + [VoteType.Yes]: yesVotes || 0n, + [VoteType.No]: noVotes || 0n, + [VoteType.Abstain]: abstainVotes || 0n, + }; } else { - voterToVotes[account][VoteType.Yes] -= yesVotes || 0n; - voterToVotes[account][VoteType.No] -= noVotes || 0n; - voterToVotes[account][VoteType.Abstain] -= abstainVotes || 0n; + voterToVotes[account] = EmptyVoteAmounts; } } catch (error) { logger.warn('Error decoding event log', error, log);