Skip to content

Commit

Permalink
Merge pull request #1295 from decent-dao/fix/shutter-voting-fixes
Browse files Browse the repository at this point in the history
Snapshot proposal fixes
  • Loading branch information
mudrila authored Dec 19, 2023
2 parents e3569c5 + 8e0aa2e commit dddf7ae
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 28 deletions.
6 changes: 3 additions & 3 deletions app/daos/[daoAddress]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default function DaoPageLayout({
params: { daoAddress?: string };
}) {
const { node } = useFractal();
const loading = useDAOController({ daoAddress });
const { nodeLoading, reloadingDAO } = useDAOController({ daoAddress });
const { chain } = useNetwork();

const validSafe = node.safe;
Expand All @@ -84,7 +84,7 @@ export default function DaoPageLayout({
display = children;
} else if (!chain) {
// if we're disconnected
if (loading || validSafe) {
if (nodeLoading || reloadingDAO || validSafe) {
display = children;
} else {
display = <InvalidSafe />;
Expand All @@ -94,7 +94,7 @@ export default function DaoPageLayout({
const invalidChain = !supportedChains.map(c => c.chainId).includes(chain.id);
if (invalidChain) {
display = <InvalidChain />;
} else if (loading || validSafe) {
} else if (nodeLoading || reloadingDAO || validSafe) {
display = children;
} else {
display = <InvalidSafe />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function ProposalAction({
);

const showActionButton =
(isSnapshotProposal && canVote) ||
(isSnapshotProposal && canVote && isActiveProposal) ||
(user.votingWeight.gt(0) &&
(isActiveProposal ||
proposal.state === FractalProposalState.EXECUTABLE ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Grid, GridItem, Text, Flex } from '@chakra-ui/react';
import { useTranslation } from 'react-i18next';
import useSnapshotProposal from '../../../hooks/DAO/loaders/snapshot/useSnapshotProposal';
import useDisplayName from '../../../hooks/utils/useDisplayName';
import { useFractal } from '../../../providers/App/AppProvider';
import {
Expand All @@ -17,7 +16,6 @@ interface ISnapshotProposalVoteItem {

export default function SnapshotProposalVoteItem({ proposal, vote }: ISnapshotProposalVoteItem) {
const { t } = useTranslation();
const { getVoteWeight } = useSnapshotProposal(proposal);
const { displayName } = useDisplayName(vote.voter);
const {
readOnly: { user },
Expand Down Expand Up @@ -62,7 +60,7 @@ export default function SnapshotProposalVoteItem({ proposal, vote }: ISnapshotPr
</GridItem>
<GridItem colSpan={1}>
<Text textStyle="text-base-sans-regular">
{getVoteWeight(vote)} {proposal.strategies[0].params.symbol}
{vote.votingWeight} {proposal.strategies[0].params.symbol}
</Text>
</GridItem>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { useState, useEffect } from 'react';
import useSnapshotProposal from '../../../../hooks/DAO/loaders/snapshot/useSnapshotProposal';
import { ExtendedSnapshotProposal } from '../../../../types';

export default function useTotalVotes({ proposal }: { proposal?: ExtendedSnapshotProposal }) {
const [totalVotesCasted, setTotalVotesCasted] = useState(0);
const { getVoteWeight } = useSnapshotProposal(proposal);

useEffect(() => {
if (proposal) {
let newTotalVotesCasted = 0;
proposal.votes.forEach(vote => (newTotalVotesCasted += getVoteWeight(vote)));
proposal.votes.forEach(vote => (newTotalVotesCasted += vote.votingWeight));
setTotalVotesCasted(newTotalVotesCasted);
}
}, [proposal, totalVotesCasted, getVoteWeight]);
}, [proposal, totalVotesCasted]);

return { totalVotesCasted };
}
23 changes: 8 additions & 15 deletions src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
[snapshotProposal]
);

const getVoteWeight = useCallback(
(vote: SnapshotVote) =>
vote.votingWeight * vote.votingWeightByStrategy.reduce((prev, curr) => prev + curr, 0),
[]
);

const loadProposal = useCallback(async () => {
if (snapshotProposal?.snapshotProposalId) {
const proposalQueryResult = await client
Expand Down Expand Up @@ -78,7 +72,7 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
const votesQueryResult = await client
.query({
query: gql`query SnapshotProposalVotes {
votes(where: {proposal: "${snapshotProposal.snapshotProposalId}"}) {
votes(where: {proposal: "${snapshotProposal.snapshotProposalId}"}, first: 100) {
id
voter
vp
Expand Down Expand Up @@ -139,12 +133,12 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
const existingChoiceType = votesBreakdown[voteChoice];
if (existingChoiceType) {
votesBreakdown[voteChoice] = {
total: existingChoiceType.total + getVoteWeight(vote),
total: existingChoiceType.total + vote.votingWeight,
votes: [...existingChoiceType.votes, vote],
};
} else {
votesBreakdown[voteChoice] = {
total: getVoteWeight(vote),
total: vote.votingWeight,
votes: [vote],
};
}
Expand All @@ -156,12 +150,12 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
const existingChoiceType = votesBreakdown[voteChoice];
if (existingChoiceType) {
votesBreakdown[voteChoice] = {
total: existingChoiceType.total + getVoteWeight(vote),
total: existingChoiceType.total + vote.votingWeight,
votes: [...existingChoiceType.votes, vote],
};
} else {
votesBreakdown[voteChoice] = {
total: getVoteWeight(vote),
total: vote.votingWeight,
votes: [vote],
};
}
Expand All @@ -174,12 +168,12 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u

if (existingChoiceType) {
votesBreakdown[choiceKey] = {
total: existingChoiceType.total + getVoteWeight(vote),
total: existingChoiceType.total + vote.votingWeight,
votes: [...existingChoiceType.votes, vote],
};
} else {
votesBreakdown[choiceKey] = {
total: getVoteWeight(vote),
total: vote.votingWeight,
votes: [vote],
};
}
Expand All @@ -194,7 +188,7 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
votes: votesQueryResult,
} as ExtendedSnapshotProposal);
}
}, [snapshotProposal?.snapshotProposalId, proposal, snapshotProposal?.state, getVoteWeight]);
}, [snapshotProposal?.snapshotProposalId, proposal, snapshotProposal?.state]);

const loadVotingWeight = useCallback(async () => {
if (snapshotProposal?.snapshotProposalId) {
Expand Down Expand Up @@ -234,7 +228,6 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
return {
loadVotingWeight,
loadProposal,
getVoteWeight,
snapshotProposal,
isSnapshotProposal,
extendedSnapshotProposal,
Expand Down
1 change: 1 addition & 0 deletions src/hooks/DAO/loaders/snapshot/useSnapshotProposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const useSnapshotProposals = () => {
query: gql`
query Proposals {
proposals(
first: 50,
where: {
space_in: ["${daoSnapshotURL}"]
},
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/DAO/loaders/useFractalNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ export const useFractalNode = ({ daoAddress }: { daoAddress?: string }) => {
const chainId = chain ? chain.id : disconnectedChain.id;
useEffect(() => {
if (daoAddress && chainId + daoAddress !== currentValidSafe.current) {
setNodeLoading(true);
setDAO(chainId, daoAddress);
}
}, [daoAddress, setDAO, action, currentValidSafe, chainId]);
}, [daoAddress, setDAO, currentValidSafe, chainId]);

return nodeLoading;
};
5 changes: 4 additions & 1 deletion src/hooks/DAO/useDAOController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useGovernanceContracts } from './loaders/useGovernanceContracts';

export default function useDAOController({ daoAddress }: { daoAddress?: string }) {
const [currentDAOAddress, setCurrentDAOAddress] = useState<string>();
const [reloadingDAO, setReloadingDAO] = useState(false);
const {
node: {
nodeHierarchy: { parentAddress },
Expand All @@ -21,8 +22,10 @@ export default function useDAOController({ daoAddress }: { daoAddress?: string }
} = useFractal();
useEffect(() => {
if (daoAddress && currentDAOAddress !== daoAddress) {
setReloadingDAO(true);
action.resetDAO().then(() => {
setCurrentDAOAddress(daoAddress);
setReloadingDAO(false);
});
}
}, [daoAddress, currentDAOAddress, action]);
Expand All @@ -35,5 +38,5 @@ export default function useDAOController({ daoAddress }: { daoAddress?: string }
useFractalTreasury();
useERC20Claim();
useSnapshotProposals();
return nodeLoading;
return { nodeLoading, reloadingDAO };
}

0 comments on commit dddf7ae

Please sign in to comment.