Skip to content

Commit

Permalink
Merge pull request #98 from hansmrtn/strategy-selection
Browse files Browse the repository at this point in the history
reworked strategy logic, split components
  • Loading branch information
hmrtn authored Jan 7, 2022
2 parents 45a2954 + e511a40 commit c9e0a1c
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 72 deletions.
9 changes: 0 additions & 9 deletions packages/react-app/src/routes/create/Create.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,13 @@ const Create = ({ address, mainnetProvider, userSigner, tx, readContracts, write
<FormControl id="create">
<FormLabel>Name</FormLabel>
<Input size="lg" placeholder="Party Name" onChange={e => (partyObj.name = e.currentTarget.value)} />

<FormLabel>Description</FormLabel>
<Textarea
size="lg"
placeholder="Describe your party"
rows={3}
onChange={e => (partyObj.description = e.currentTarget.value)}
/>

<FormLabel>Participants</FormLabel>
<Textarea
size="lg"
Expand All @@ -161,7 +159,6 @@ const Create = ({ address, mainnetProvider, userSigner, tx, readContracts, write
onChange={parseParticipants}
isInvalid={isInvalidParticipantInput}
/>

<FormLabel>Candidates</FormLabel>
<Textarea
size="lg"
Expand All @@ -170,12 +167,6 @@ const Create = ({ address, mainnetProvider, userSigner, tx, readContracts, write
onChange={parseCandidates}
isInvalid={isInvalidCandidateInput}
/>
<FormLabel>Strategy</FormLabel>
<Select size="lg" onChange={e => (partyObj.config.strategy = e.target.value)}>
<option>Linear</option>
<option>Quadratic</option>
</Select>

<Center pt={10}>
<Button size="lg" type="submit" isLoading={isLoading} loadingText={loadingText}>
Submit Party
Expand Down
93 changes: 70 additions & 23 deletions packages/react-app/src/routes/party/Party.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import React, { useEffect, useState, useMemo } from "react";
import { Button, Box, Divider, Center } from "@chakra-ui/react";
import { ArrowBackIcon } from "@chakra-ui/icons";
import React, { useEffect, useState } from "react";
import {
Button,
Box,
Center,
Menu,
MenuOptionGroup,
MenuButton,
MenuList,
MenuItemOption,
Spacer,
Text,
HStack,
} from "@chakra-ui/react";
import { ArrowBackIcon, ChevronDownIcon } from "@chakra-ui/icons";
import { useParams, useHistory } from "react-router-dom";
import MongoDBController from "../../controllers/mongodbController";
import { Vote, View, Distribute } from "./components";
import { VoteTable, ViewTable, Distribute, Metadata } from "./components";

export default function Party({
address,
Expand All @@ -25,6 +37,7 @@ export default function Party({
const [canVote, setCanVote] = useState(false);
const [isParticipant, setIsParticipant] = useState(false);
const [distribution, setDistribution] = useState();
const [strategy, setStrategy] = useState("linear");

const db = new MongoDBController();

Expand All @@ -49,20 +62,20 @@ export default function Party({
const votes = partyData.ballots.map(b => JSON.parse(b.data.ballot.votes.replace(/[ \n\r]/g, "")));
let sum = 0;
let processed = [];
let strategy = partyData.config.strategy;
// let strategy = partyData.config.strategy;
if (!strategy || strategy === "") {
strategy = "Linear";
strategy = "linear";
console.log("Reverted to linear strategy");
}
for (let i = 0; i < partyData.candidates.length; i++) {
const candidate = partyData.candidates[i];
// Strategy handling
// TODO: Switch statement
if (strategy === "Linear") {
if (strategy === "linear") {
let c = votes.reduce((total, vote) => vote[candidate] + total, 0);
sum += c;
processed.push({ address: candidate, reduced: c });
} else if (strategy === "Quadratic") {
} else if (strategy === "quadratic") {
let c = votes.reduce((total, vote) => vote[candidate] ** 0.5 + total, 0);
sum += c;
processed.push({ address: candidate, reduced: c });
Expand All @@ -77,10 +90,31 @@ export default function Party({
}
};

const StrategySelect = () => {
return (
<Menu closeOnSelect={false}>
<MenuButton as={Button} variant="link" rightIcon={<ChevronDownIcon />}>
{strategy}
</MenuButton>
<MenuList>
<MenuOptionGroup title="select strategy" type="radio" onChange={e => setStrategy(e)}>
<MenuItemOption key="linear-select" value="linear">
Linear
</MenuItemOption>
,
<MenuItemOption key="quadratic-select" value="quadratic">
Quadratic
</MenuItemOption>
</MenuOptionGroup>
</MenuList>
</Menu>
);
};

// Calculate the distribution on load
useEffect(() => {
calcDistribution();
}, [partyData]);
}, [partyData, strategy]);

return (
<Box>
Expand All @@ -103,11 +137,18 @@ export default function Party({
>
Debug
</Button>
<Box>
{showDebug && <p>{JSON.stringify(partyData)}</p>}
<Center p="5">
<Center p="5">
<Box borderWidth={"1px"} shadow="xl" rounded="md" p="10" w="4xl">
{showDebug && <p>{JSON.stringify(partyData)}</p>}
<Metadata
partyData={partyData}
mainnetProvider={mainnetProvider}
votesData={accountVoteData}
distribution={distribution}
strategy={strategy}
/>
{canVote ? (
<Vote
<VoteTable
dbInstance={db}
partyData={partyData}
address={address}
Expand All @@ -117,15 +158,20 @@ export default function Party({
mainnetProvider={mainnetProvider}
/>
) : (
<View
partyData={partyData}
mainnetProvider={mainnetProvider}
votesData={accountVoteData}
distribution={distribution}
/>
<Box>
<Center pb="1" pt="3">
<Text pr="3">Strategy:</Text>
<StrategySelect />
</Center>
<ViewTable
partyData={partyData}
mainnetProvider={mainnetProvider}
votesData={accountVoteData}
distribution={distribution}
strategy={strategy}
/>
</Box>
)}
</Center>
<Center p="5">
<Distribute
dbInstance={db}
partyData={partyData}
Expand All @@ -135,9 +181,10 @@ export default function Party({
readContracts={readContracts}
tx={tx}
distribution={distribution}
strategy={strategy}
/>
</Center>
</Box>
</Box>
</Center>
</Box>
);
}
12 changes: 6 additions & 6 deletions packages/react-app/src/routes/party/components/Distribute.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NumberInput, NumberInputField, Box, Button, Input, HStack, Spacer, Text, Center } from "@chakra-ui/react";
import React, { useEffect, useState } from "react";
import React, { useState } from "react";
import { toWei } from "web3-utils";
import { BigNumber, ethers } from "ethers";
import $ from "jquery";
Expand All @@ -13,6 +13,7 @@ export const Distribute = ({
writeContracts,
tx,
distribution,
strategy,
}) => {
const [tokenInstance, setTokenInstance] = useState(null);
const [amounts, setAmounts] = useState(null);
Expand Down Expand Up @@ -84,13 +85,12 @@ export const Distribute = ({
const amts = [];
let tot = BigNumber.from("0x00");
for (let i = 0; i < validAdrs.length; i++) {
let pay = (validScores[i] * amt).toFixed(18).toString();
let pay = (validScores[i] * amt).toFixed(16).toString();
const x = BigNumber.from(toWei(pay));
amts.push(x);
adrs.push(validAdrs[i]);
tot = tot.add(x);
}
console.log(adrs, amts);
setTotal(tot);
setAmounts(amts);
setAddresses(adrs);
Expand Down Expand Up @@ -151,8 +151,8 @@ export const Distribute = ({
};

return (
<Box borderWidth={"1px"} shadow="xl" rounded="md" p="10" w="4xl" minW='lg'>
<Center pb='10'>
<Box>
<Center pb="10" pt="10">
<Text fontSize="lg">Distribute Funds</Text>
</Center>
<HStack>
Expand All @@ -162,7 +162,7 @@ export const Distribute = ({
Load Token
</Button>
</HStack>
<HStack pt={4}>
<HStack pt={4}>
<Spacer />
<NumberInput onChange={handleAmountChange}>
<NumberInputField placeholder="1" />
Expand Down
24 changes: 24 additions & 0 deletions packages/react-app/src/routes/party/components/Metadata.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Box, Text, Center } from "@chakra-ui/react";
import React from "react";

export const Metadata = ({ partyData, mainnetProvider, votesData, distribution, strategy }) => {
return (
<Box>
<Center pt={4}>
<Text fontSize="lg">Party</Text>
</Center>
<Center pt={4}>
<Text fontSize="xl">{`${partyData.name}`}</Text>
</Center>
<Center pt={4} pl="5%" pr="5%">
<Text fontSize="sm">{`${partyData.description}`}</Text>
</Center>
<Center p="4">
<Text
fontWeight="semibold"
fontSize="lg"
>{`Voted: ${partyData?.ballots?.length}/${partyData?.participants?.length}`}</Text>
</Center>
</Box>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import React, { useState, useMemo, useEffect } from "react";
import AddressChakra from "../../../components/AddressChakra";

export const View = ({ partyData, mainnetProvider, votesData, distribution }) => {
export const ViewTable = ({ partyData, mainnetProvider, votesData, distribution, strategy }) => {
const [castVotes, setCastVotes] = useState(null);
const [currentDist, setCurrentDist] = useState(null);
const [isLoading, setIsLoading] = useState(true);
Expand All @@ -28,7 +28,7 @@ export const View = ({ partyData, mainnetProvider, votesData, distribution }) =>
const dist =
distribution && distribution.reduce((obj, item) => Object.assign(obj, { [item.address]: item.score }), {});
setCurrentDist(dist);
}, [votesData]);
}, [votesData, distribution]);

const candidateRows = useMemo(() => {
const row =
Expand All @@ -52,34 +52,20 @@ export const View = ({ partyData, mainnetProvider, votesData, distribution }) =>
);
});
return row;
}, [partyData, castVotes]);
}, [partyData, castVotes, distribution]);

return (
<Box borderWidth={"1px"} shadow='xl' rounded='md' p='10' w='4xl'>
<Center pt={4}>
<Text fontSize="lg">Party</Text>
</Center>
<Center pt={4}>
<Text fontSize='xl'>{`${partyData?.name}`}</Text>
</Center>
<Center pt={4} pl="5%" pr="5%">
<Text fontSize="sm">{`${partyData?.description}`}</Text>
</Center>
<Center p='4'>
<Text fontWeight='semibold' fontSize='lg'>{`Voted: ${partyData?.ballots?.length}/${partyData?.participants?.length}`}</Text>
</Center>

<Table borderWidth='1px'>
<Box>
<Table borderWidth="1px">
<Thead>
<Tr>
<Th>Address</Th>
<Th>Your Vote</Th>
<Th>Current Distribution</Th>
<Th>{`Score (${strategy})`}</Th>
</Tr>
</Thead>
{candidateRows}
<Tfoot>
</Tfoot>
<Tfoot></Tfoot>
</Table>
</Box>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ import {
import React, { useState, useMemo } from "react";
import AddressChakra from "../../../components/AddressChakra";

export const Vote = ({ dbInstance, partyData, address, userSigner, targetNetwork, readContracts, mainnetProvider }) => {
export const VoteTable = ({
dbInstance,
partyData,
address,
userSigner,
targetNetwork,
readContracts,
mainnetProvider,
}) => {
// Init votes data to 0 votes for each candidate
const [votesData, setVotesData] = useState(partyData.candidates.reduce((o, key) => ({ ...o, [key]: 0 }), {}));
// Init votes left to nvotes
Expand Down Expand Up @@ -126,16 +134,7 @@ export const Vote = ({ dbInstance, partyData, address, userSigner, targetNetwork
}, [partyData, votesLeft]);

return (
<Box borderWidth={"1px"} shadow="xl" rounded="md" p="10" w="4xl" minW="sm">
<Center pt={4}>
<Text fontSize="lg">Party</Text>
</Center>
<Center pt={4}>
<Text fontSize="xl">{`${partyData.name}`}</Text>
</Center>
<Center pt={4} pl="5%" pr="5%">
<Text fontSize="sm">{`${partyData.description}`}</Text>
</Center>
<Box>
<Center pt={4}>
<Text fontSize="lg">Cast Votes</Text>
</Center>
Expand Down
5 changes: 3 additions & 2 deletions packages/react-app/src/routes/party/components/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Vote } from "./Vote";
export { View } from "./View";
export { VoteTable } from "./VoteTable";
export { Metadata } from "./Metadata";
export { ViewTable } from "./ViewTable";
export { Distribute } from "./Distribute";

0 comments on commit c9e0a1c

Please sign in to comment.