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

feat: cancel vote for the same option in second time #110

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ENV NEXT_TELEMETRY_DISABLED=1 \
CHAIN_ID=$CHAIN_ID

WORKDIR /app
RUN apk add --no-cache curl=~8.5.0-r0
RUN apk add --no-cache curl=~8.9.0-r0
COPY --from=build /app /app

USER node
Expand Down
21 changes: 21 additions & 0 deletions features/aragon/useAragon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,24 @@ export const useGetVoting = () => {
[contractWeb3],
);
};

export const useGetVoterState = () => {
const { contractWeb3 } = useAragon();

return useCallback(
async (voteId: number, voter = '') => {
if (contractWeb3 == null) {
return undefined;
}
try {
return await runWithFunctionLogger('Aragon: get voter info', () =>
contractWeb3['getVoterState'](voteId, voter),
);
} catch (e) {
console.error(e);
return undefined;
}
},
[contractWeb3],
);
};
51 changes: 41 additions & 10 deletions features/aragon/vote/aragonVoteForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,31 @@ import { useForm } from 'react-hook-form';
import { InputGroupStyled, InputNumber } from 'shared/ui';
import { useEncodeAragonCalldata } from 'features/votingAdapter';
import { ButtonsGroup, Form, Links, LinkWrapper } from '../aragonFormStyles';
import { useGetVoting } from '../useAragon';
import { useGetVoterState, useGetVoting } from '../useAragon';
import { VotingLink } from './votingLink';

type AragonFormData = {
voteId: string;
success: boolean;
};

enum voterStates {
Absent, // Voter has not voted
Yea, // Voter has voted for
Nay, // Voter has voted against
DelegateYea, // Delegate has voted for on behalf of the voter
DelegateNay, // Delegate has voted against on behalf of the voter
}
/*
* Search for VotePhase on
* https://etherscan.io/address/0x72fb5253ad16307b9e773d2a78cac58e309d5ba4#code
*/
enum votePhases {
Main,
Objection,
Closed,
}

const validateVoteId = (value: string) => {
const number = parseInt(value);
if (number.toString() !== value) {
Expand All @@ -38,10 +55,15 @@ export const AragonVoteForm = () => {
const encodeCalldata = useEncodeAragonCalldata();
const aragonVote = useAragonVote(activeVesting?.escrow);
const getVoting = useGetVoting();
const getVoterState = useGetVoterState();

const runTransaction = useCallback(
async ({ voteId, success }: AragonFormData) => {
const vote = await getVoting(parseInt(voteId));
const voteIdNum = parseInt(voteId);
const [vote, voterState] = await Promise.all([
getVoting(voteIdNum),
getVoterState(voteIdNum, activeVesting?.escrow),
]);
if (vote == null) {
ToastError(`Voting doesn't exists`);
return;
Expand All @@ -50,19 +72,28 @@ export const AragonVoteForm = () => {
ToastError('Voting is closed');
return;
}
/*
* Search for VotePhase on
* https://etherscan.io/address/0x72fb5253ad16307b9e773d2a78cac58e309d5ba4#code
*/
if (success && vote?.phase === 1) {
if (success && vote?.phase === votePhases.Objection) {
ToastError('Voting is in objection phase');
return;
}

const callData = await encodeCalldata(parseInt(voteId), success);
if (success && voterState == voterStates.Yea) {
ToastError('You have already voted "YES" in this vote');
return;
}
if (!success && voterState == voterStates.Nay) {
ToastError('You have already voted "NO" in this vote');
return;
}
const callData = await encodeCalldata(voteIdNum, success);
await aragonVote(callData);
},
[getVoting, encodeCalldata, aragonVote],
[
getVoting,
getVoterState,
activeVesting?.escrow,
encodeCalldata,
aragonVote,
],
);

const handleYesButton = useCallback(() => {
Expand Down
Loading