Skip to content

Commit

Permalink
fix: update voting method to support runtime upgrade and prevent app …
Browse files Browse the repository at this point in the history
…breakage (#11344)
  • Loading branch information
ap211unitech authored Feb 28, 2025
1 parent 020c188 commit 628947e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/page-referenda/src/Referenda/useVotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ function filterEvents (records: EventRecord[], _: ApiPromise, id?: BN): Changes<

function useVotesImpl (palletVote: PalletVote, id: BN, isConvictionVote: boolean): Record<string, PalletRankedCollectiveVoteRecord> | undefined {
const { api } = useApi();
const startAccounts = useMapKeys(isConvictionVote === false && api.query[palletVote].voting, [id], OPT_ACCOUNTID);

// After v1.4.0 runtime upgrade, Relay chains i.e. Kusama and Polkadot, or other parachains chains, replaced `voting` method with `votingFor`.
// Adding a safety check here so that app doesn't break
const query = useMemo(() => api.query[palletVote].voting ?? api.query[palletVote].votingFor, [api.query, palletVote]);

const startAccounts = useMapKeys(isConvictionVote === false && query, [id], OPT_ACCOUNTID);
const allAccounts = useEventChanges([
api.events[palletVote].Voted
], filterEvents, startAccounts, id);
Expand All @@ -54,7 +59,7 @@ function useVotesImpl (palletVote: PalletVote, id: BN, isConvictionVote: boolean
[allAccounts, id]
);

return useCall(params && api.query[palletVote].voting.multi, [params], OPT_VOTES);
return useCall(params && query?.multi, [params], OPT_VOTES);
}

export default createNamedHook('useVotes', useVotesImpl);

0 comments on commit 628947e

Please sign in to comment.