Skip to content

Commit

Permalink
feat: add ability to use qv voting
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed Jun 27, 2024
1 parent c280402 commit 92f6b88
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 180 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@ BLOB_READ_WRITE_TOKEN=""

# URL with tally-{pollId}.json hosted
NEXT_PUBLIC_TALLY_URL=https://upblxu2duoxmkobt.public.blob.vercel-storage.com

# Whether the poll is in qv or non qv mode
NEXT_PUBLIC_POLL_MODE="non-qv"
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const config = {
maciSubgraphUrl: process.env.NEXT_PUBLIC_MACI_SUBGRAPH_URL ?? "",
tallyUrl: process.env.NEXT_PUBLIC_TALLY_URL,
roundOrganizer: process.env.NEXT_PUBLIC_ROUND_ORGANIZER ?? "Optimism",
pollMode: process.env.NEXT_PUBLIC_POLL_MODE ?? "non-qv",
};

export const theme = {
Expand Down
11 changes: 9 additions & 2 deletions src/contexts/Ballot.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { createContext, useContext, useState, useEffect, useMemo, useCallback } from "react";
import { useAccount } from "wagmi";

import { config } from "~/config";

import type { BallotContextType, BallotProviderProps } from "./types";
import type { Ballot, Vote } from "~/features/ballot/types";

Expand All @@ -13,9 +15,14 @@ export const BallotProvider: React.FC<BallotProviderProps> = ({ children }: Ball

const { isDisconnected } = useAccount();

// when summing the ballot we take the individual vote and square it
// if the mode is quadratic voting, otherwise we just add the amount
const sumBallot = useCallback(
(votes?: Vote[]) =>
(votes ?? []).reduce((sum, x) => sum + (!Number.isNaN(Number(x.amount)) ? Number(x.amount) : 0), 0),
(votes ?? []).reduce((sum, x) => {
const amount = !Number.isNaN(Number(x.amount)) ? Number(x.amount) : 0;
return sum + (config.pollMode === "qv" ? amount ** 2 : amount);
}, 0),
[],
);

Expand Down Expand Up @@ -71,7 +78,7 @@ export const BallotProvider: React.FC<BallotProviderProps> = ({ children }: Ball
localStorage.removeItem("ballot");
}, [setBallot]);

// set published to tru
// set published to true
const publishBallot = useCallback(() => {
setBallot({ ...ballot, published: true });
}, [ballot, setBallot]);
Expand Down
4 changes: 4 additions & 0 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export const env = createEnv({
NEXT_PUBLIC_MACI_SUBGRAPH_URL: z.string().url().optional(),

NEXT_PUBLIC_TALLY_URL: z.string().url(),

NEXT_PUBLIC_POLL_MODE: z.enum(["qv", "non-qv"]).default("non-qv"),
},

/**
Expand Down Expand Up @@ -107,6 +109,8 @@ export const env = createEnv({
NEXT_PUBLIC_MACI_SUBGRAPH_URL: process.env.NEXT_PUBLIC_MACI_SUBGRAPH_URL,

NEXT_PUBLIC_TALLY_URL: process.env.NEXT_PUBLIC_TALLY_URL,

NEXT_PUBLIC_POLL_MODE: process.env.NEXT_PUBLIC_POLL_MODE,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
Expand Down
6 changes: 3 additions & 3 deletions src/features/projects/components/AddToBallot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const ProjectAllocation = ({
const form = useFormContext();
const formAmount = form.watch("amount") as string;
const amount = formAmount ? parseFloat(String(formAmount).replace(/,/g, "")) : 0;
const total = amount + current;
const total = (config.pollMode === "qv" ? amount ** 2 : amount) + current;
const { initialVoiceCredits } = useMaci();

const exceededProjectTokens = amount > initialVoiceCredits;
Expand Down Expand Up @@ -125,7 +125,7 @@ export const ProjectAddToBallot = ({ id = "", name = "" }: IProjectAddToBallotPr

{!ballot?.published && inBallot && (
<IconButton icon={Check} variant="primary" onClick={handleOpen}>
{formatNumber(inBallot.amount)} allocated
{formatNumber(config.pollMode === "qv" ? inBallot.amount ** 2 : inBallot.amount)} allocated
</IconButton>
)}

Expand All @@ -151,7 +151,7 @@ export const ProjectAddToBallot = ({ id = "", name = "" }: IProjectAddToBallotPr
amount: z
.number()
.min(0)
.max(Math.min(initialVoiceCredits, initialVoiceCredits - sum))
.max(Math.sqrt(Math.min(initialVoiceCredits, initialVoiceCredits - sum)))
.default(0),
})}
onSubmit={({ amount }) => {
Expand Down
1 change: 0 additions & 1 deletion src/features/projects/hooks/useSelectProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export function useSelectProjects(): IUseSelectProjectsReturn {

return {
count: toAdd.length,
// isLoading: add.isPending,
add: () => {
addToBallot(toAdd, pollId!);
setSelected({});
Expand Down
99 changes: 0 additions & 99 deletions src/utils/calculateResults.test.ts

This file was deleted.

75 changes: 0 additions & 75 deletions src/utils/calculateResults.ts

This file was deleted.

0 comments on commit 92f6b88

Please sign in to comment.