Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix casting snapshot vote on testnet #1320

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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