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(165,169): Participants percentage & Current vote bar #170

Merged
merged 8 commits into from
Jun 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import useApprove from "@/lib/contracts/mento/useApprove";
import { useContracts } from "@/lib/contracts/useContracts";
import React from "react";
import { parseEther } from "viem";
import { useAccount, useChainId } from "wagmi";
import { useAccount } from "wagmi";
import {
DEFAULT_CLIFF,
LOCKING_AMOUNT_FORM_KEY,
Expand Down Expand Up @@ -56,12 +56,11 @@ export const CreateLockProvider = ({
const { watch, reset: resetForm } = useFormContext();
const [isTxModalOpen, setIsTxModalOpen] = React.useState(false);

const { address } = useAccount();
const { address, chainId } = useAccount();
const amount = watch(LOCKING_AMOUNT_FORM_KEY);
const slope = watch(LOCKING_DURATION_FORM_KEY);

const contracts = useContracts();
const chainId = useChainId();
const parsedAmount = parseEther(amount);

const lock = useCreateLockOnChain({
Expand Down
13 changes: 9 additions & 4 deletions src/components/_shared/progress-bar/progress-bar.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ export const MultiProgressBar = ({
progress,
barColorString,
styles: {
borderRadius: 0,
backgroundColor: "transparent",
},
};
Expand Down Expand Up @@ -175,15 +174,15 @@ export const MultiProgressBar = ({
className,
)}
>
<div className="relative h-4 w-full overflow-hidden rounded-3xl border-[0.5px] border-solid border-black dark:border-gray">
<div className="relative flex h-4 w-full overflow-hidden rounded-3xl">
{progressBars
.toSorted((a, b) => b.progress - a.progress)
.filter((item) => item.progress !== 0)
.map(({ progress, barColorString }, index) => {
return (
<div
key={index}
className={cn(
"absolute left-[-1px] top-[-1px] h-[calc(100%_+_2px)] rounded-r-3xl border-[0.5px] border-black bg-gray dark:border-gray",
"h-100% relative top-0 flex-grow border-r-[1px] border-black bg-gray dark:border-gray",
)}
style={{
width: `${progress}%`,
Expand All @@ -192,6 +191,12 @@ export const MultiProgressBar = ({
></div>
);
})}
{/* More sensible border */}
<div
className={cn(
"absolute top-0 z-10 h-full w-full flex-grow rounded-3xl border-[0.5px] border-solid border-black bg-transparent dark:border-gray",
)}
></div>
</div>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/create-proposal/create-proposal-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import useCreateProposalOnChain, {
TransactionItem,
} from "@/lib/contracts/governor/useCreateProposalOnChain";
import { LocalStorageKeys, useLocalStorage } from "@/lib/hooks/useStorage";
import { useBlockNumber, useChainId } from "wagmi";
import { useAccount, useBlockNumber } from "wagmi";
import { Loader } from "@/components/_shared";
import { CreateProposalTxModal } from "@/components/create-proposal/create-proposal-transaction.model";
import { useRouter } from "next/navigation";
Expand Down Expand Up @@ -57,7 +57,7 @@ interface ICreateProposalProvider {
export const CreateProposalProvider = ({
children,
}: ICreateProposalProvider) => {
const chainId = useChainId();
const { chainId } = useAccount();
const router = useRouter();
const { proposalExists, refetchProposals } = useProposals();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export const ProposalCurrentVotes = ({
},
{
progress: Number(
(proposal.votes.against.total * 100n) / proposal.votes.total,
(proposal.votes.abstain.total * 100n) / proposal.votes.total,
),
type: "danger",
type: "info",
},
{
progress: Number(
(proposal.votes.abstain.total * 100n) / proposal.votes.total,
(proposal.votes.against.total * 100n) / proposal.votes.total,
),
type: "info",
type: "danger",
},
] as MultiProgressBarValue[];
}, [
Expand Down
16 changes: 15 additions & 1 deletion src/components/votes-list/votes-list.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ const getParticipantPercentage = (
) => {
// If there are no votes, we need to avoid division by zero
if (totalVotes > 0n) {
RyRy79261 marked this conversation as resolved.
Show resolved Hide resolved
return `${Number((participant.weight * 100n) / totalVotes).toFixed(2)}%`;
const decimals = 8;
const accuracy = 10n ** BigInt(decimals);

const weightScaled = participant.weight * 100n;

const valueInRawBN = (weightScaled * accuracy) / totalVotes;

const formatted = formatUnits(valueInRawBN, decimals);

if (formatted.includes(".")) {
const [integers, remainingDecimals] = formatted.split(".");
return `${integers}.${remainingDecimals.substring(0, 2)}%`;
} else {
return `${formatted}%`;
}
} else return "0%";
};
4 changes: 2 additions & 2 deletions src/lib/contracts/governor/useProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {
} from "@/lib/graphql/subgraph/generated/subgraph";
import { NetworkStatus } from "@apollo/client";
import { useEffect, useMemo } from "react";
import { useBlockNumber, useChainId, useReadContract } from "wagmi";
import { useAccount, useBlockNumber, useReadContract } from "wagmi";

export const ProposalQueryKey = "proposal";

const useProposal = (proposalId: bigint) => {
const chainId = useChainId();
const { chainId } = useAccount();
const contracts = useContracts();
const { data: blockNumber } = useBlockNumber({ watch: true });

Expand Down
4 changes: 2 additions & 2 deletions src/lib/contracts/governor/useProposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {
} from "@/lib/graphql/subgraph/generated/subgraph";
import { NetworkStatus } from "@apollo/client";
import { useCallback, useMemo } from "react";
import { useChainId, useReadContracts } from "wagmi";
import { useAccount, useReadContracts } from "wagmi";

export const GraphProposalsQueryKey = ["proposals-graph-query"];

const useProposals = () => {
const chainId = useChainId();
const { chainId } = useAccount();
const contracts = useContracts();

const {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/contracts/locking/useAllLocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {
Lock,
useGetAllLocksSuspenseQuery,
} from "@/lib/graphql/subgraph/generated/subgraph";
import { useChainId } from "wagmi";
import { useAccount } from "wagmi";

const useAllLocks = () => {
const chainId = useChainId();
const { chainId } = useAccount();
const {
data: { locks },
} = useGetAllLocksSuspenseQuery({
Expand Down
4 changes: 2 additions & 2 deletions src/lib/contracts/locking/useLocksByAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {
useGetLocksSuspenseQuery,
Lock,
} from "@/lib/graphql/subgraph/generated/subgraph";
import { useChainId } from "wagmi";
import { useAccount } from "wagmi";

interface UseLocksProps {
account: string;
}

const useLocksByAccount = ({ account }: UseLocksProps) => {
const chainId = useChainId();
const { chainId } = useAccount();
const {
data: { locks },
...rest
Expand Down
4 changes: 2 additions & 2 deletions src/lib/contracts/useContractsInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GetContractsInfoQuery, ProposalCall } from "@/lib/graphql";
import { useGetContractsInfoQuery } from "@/lib/graphql/celo-explorer/generated/celoGraph";
import { useMemo } from "react";
import { decodeFunctionData } from "viem";
import { useChainId } from "wagmi";
import { useAccount } from "wagmi";

/**
* The Celo Explorer API returns nothing (as in not even undefined) for
Expand Down Expand Up @@ -96,7 +96,7 @@ type ContractInfo = {
};

const useContractsInfo = ({ calls }: Props) => {
const chainId = useChainId();
const { chainId } = useAccount();

const { data, error: apolloError } = useGetContractsInfoQuery({
variables: {
Expand Down
Loading