Skip to content

Commit

Permalink
Fix casting vote on testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
mudrila committed Jan 16, 2024
1 parent 4c25974 commit 152e2ac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
const { choices, type, privacy } = proposalQueryResult;

if (type === 'weighted') {
Object.keys(choices).forEach((choice: string) => {
votesBreakdown[choice] = {
Object.keys(choices).forEach((choice: string, choiceIndex) => {
votesBreakdown[choiceIndex] = {
votes: [],
total: 0,
};
Expand Down Expand Up @@ -146,7 +146,7 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
};
} else {
votesBreakdown[voteChoice] = {
total: vote.votingWeight,
total: voteChoices[voteChoice],
votes: [vote],
};
}
Expand Down
41 changes: 32 additions & 9 deletions src/hooks/DAO/proposal/useCastVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ import {
} from '../../../types';
import encryptWithShutter from '../../../utils/shutter';
import { useTransaction } from '../../utils/useTransaction';
import useSnapshotSpaceName from '../loaders/snapshot/useSnapshotSpaceName';
import useUserERC721VotingTokens from './useUserERC721VotingTokens';

const hub = 'https://hub.snapshot.org';
const client = new snapshot.Client712(hub);

const useCastVote = ({
proposal,
setPending,
Expand All @@ -40,7 +38,18 @@ const useCastVote = ({
user: { address },
},
} = useFractal();
const daoSnapshotSpaceName = useSnapshotSpaceName();
const { data: signer } = useSigner();
const client = useMemo(() => {
if (daoSnapshotURL) {
const isTestnetSnapshotURL = daoSnapshotURL.includes('testnet');
const hub = isTestnetSnapshotURL
? 'https://testnet.seq.snapshot.org' // This is not covered in Snapshot docs, but that's where they're sending request on testnet
: 'https://hub.snapshot.org';
return new snapshot.Client712(hub);
}
return undefined;
}, [daoSnapshotURL]);

const azoriusGovernance = useMemo(() => governance as AzoriusGovernance, [governance]);
const { type } = azoriusGovernance;
Expand Down Expand Up @@ -122,11 +131,24 @@ const useCastVote = ({

const castSnapshotVote = useCallback(
async (onSuccess?: () => Promise<void>) => {
if (signer && signer?.provider && address && daoSnapshotURL && extendedSnapshotProposal) {
if (
signer &&
signer?.provider &&
address &&
daoSnapshotSpaceName &&
extendedSnapshotProposal &&
client
) {
let toastId;
const mappedSnapshotWeightedChoice: { [choiceKey: number]: number } = {};
if (extendedSnapshotProposal.type === 'weighted') {
snapshotWeightedChoice.forEach((value, choiceIndex) => {
mappedSnapshotWeightedChoice[choiceIndex + 1] = value;
});
}
const choice =
extendedSnapshotProposal.type === 'weighted'
? snapshotWeightedChoice
? mappedSnapshotWeightedChoice
: (selectedChoice as number) + 1;
try {
toastId = toast(t('pendingCastVote'), {
Expand All @@ -142,7 +164,7 @@ const useCastVote = ({
extendedSnapshotProposal.proposalId
);
await client.vote(signer.provider as ethers.providers.Web3Provider, address, {
space: daoSnapshotURL,
space: daoSnapshotSpaceName,
proposal: extendedSnapshotProposal.proposalId,
type: extendedSnapshotProposal.type,
privacy: extendedSnapshotProposal.privacy,
Expand All @@ -151,7 +173,7 @@ const useCastVote = ({
});
} else {
await client.vote(signer.provider as ethers.providers.Web3Provider, address, {
space: daoSnapshotURL,
space: daoSnapshotSpaceName,
proposal: extendedSnapshotProposal.proposalId,
type: extendedSnapshotProposal.type,
choice,
Expand All @@ -167,19 +189,20 @@ const useCastVote = ({
}
} catch (e) {
toast.dismiss(toastId);
toast.error('failedCastVote');
toast.error(t('failedCastVote'));
logError('Error while casting Snapshot vote', e);
}
}
},
[
signer,
address,
daoSnapshotURL,
daoSnapshotSpaceName,
extendedSnapshotProposal,
t,
selectedChoice,
snapshotWeightedChoice,
client,
]
);

Expand Down

0 comments on commit 152e2ac

Please sign in to comment.