From 152e2acfb2ec73730f2e94516284a17c907792e5 Mon Sep 17 00:00:00 2001 From: Kirill Klimenko Date: Tue, 16 Jan 2024 01:30:47 +0100 Subject: [PATCH] Fix casting vote on testnet --- .../loaders/snapshot/useSnapshotProposal.ts | 6 +-- src/hooks/DAO/proposal/useCastVote.ts | 41 +++++++++++++++---- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts b/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts index bbdd9904a6..f8cdcbe648 100644 --- a/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts +++ b/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts @@ -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, }; @@ -146,7 +146,7 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u }; } else { votesBreakdown[voteChoice] = { - total: vote.votingWeight, + total: voteChoices[voteChoice], votes: [vote], }; } diff --git a/src/hooks/DAO/proposal/useCastVote.ts b/src/hooks/DAO/proposal/useCastVote.ts index d077a7e714..8ea9724cba 100644 --- a/src/hooks/DAO/proposal/useCastVote.ts +++ b/src/hooks/DAO/proposal/useCastVote.ts @@ -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, @@ -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; @@ -122,11 +131,24 @@ const useCastVote = ({ const castSnapshotVote = useCallback( async (onSuccess?: () => Promise) => { - 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'), { @@ -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, @@ -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, @@ -167,7 +189,7 @@ const useCastVote = ({ } } catch (e) { toast.dismiss(toastId); - toast.error('failedCastVote'); + toast.error(t('failedCastVote')); logError('Error while casting Snapshot vote', e); } } @@ -175,11 +197,12 @@ const useCastVote = ({ [ signer, address, - daoSnapshotURL, + daoSnapshotSpaceName, extendedSnapshotProposal, t, selectedChoice, snapshotWeightedChoice, + client, ] );