Skip to content

Commit

Permalink
bugfix: use contract config ( abi & version ) from contest store acro…
Browse files Browse the repository at this point in the history
…ss codebase (#2289)

* bugfix: use contract config from contest store across codebase

* bugfix: use filter instead of find on chainId"

* bugfix: add args to fetchProposalsPage
  • Loading branch information
nakedfool authored Sep 9, 2024
1 parent c450df7 commit 4118283
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const isDev = process.env.NODE_ENV === "development";

async function getContestDetails(address: string, chainName: string) {
const chainId = chains.filter(
(chain: { name: string }) => chain.name.toLowerCase().replace(" ", "") === chainName,
(chain: { name: string }) => chain.name.toLowerCase().replace(" ", "") === chainName.toLowerCase(),
)?.[0]?.id;

const { abi } = await getContestContractVersion(address, chainId);
Expand Down
3 changes: 0 additions & 3 deletions packages/react-app-revamp/components/Comments/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import Collapsible from "@components/UI/Collapsible";
import { ChevronUpIcon } from "@heroicons/react/24/outline";
import useComments from "@hooks/useComments";
import { useCommentsStore } from "@hooks/useComments/store";
import useContractVersion from "@hooks/useContractVersion";
import { compareVersions } from "compare-versions";
import { COMMENTS_VERSION } from "lib/proposal";
import { useSearchParams } from "next/navigation";
import { FC, useEffect, useRef, useState } from "react";
import CommentsForm from "./components/Form";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { chains } from "@config/wagmi";
import { extractPathSegments } from "@helpers/extractPath";
import useContractVersion from "@hooks/useContractVersion";
import { useContestStore } from "@hooks/useContest/store";
import { compareVersions } from "compare-versions";
import { usePathname } from "next/navigation";
import { FC } from "react";
Expand All @@ -15,12 +15,12 @@ const ProposalStatisticsPanelVotingOpenOrClosed: FC<ProposalStatisticsPanelVotin
submissionsCount,
}) => {
const asPath = usePathname();
const { version } = useContestStore(state => state);
const { address, chainName } = extractPathSegments(asPath ?? "");
const chainId = chains.filter(
(chain: { name: string }) => chain.name.toLowerCase().replace(" ", "") === chainName,
)?.[0]?.id;
const { version, isLoading, isError } = useContractVersion(address, chainId);
const isV3OrHigher = !isLoading && !isError && version && compareVersions(version, "3.0") >= 0;
const isV3OrHigher = compareVersions(version, "3.0") >= 0;

return (
<div className="flex flex-col md:flex-row gap-1 md:items-center">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@ import { FC, useMemo } from "react";
import ProposalStatisticsPanel from "./components/Panel";
import SortProposalsDropdown from "./components/SortDropdown";
import { ContestStateEnum, useContestStateStore } from "@hooks/useContestState/store";
import { useContestStore } from "@hooks/useContest/store";
import { usePathname } from "next/navigation";
import { extractPathSegments } from "@helpers/extractPath";
import { chains } from "@config/wagmi";

interface ProposalStatisticsProps {
contestStatus: ContestStatus;
onMenuStateChange: (isOpen: boolean) => void;
}

const ProposalStatistics: FC<ProposalStatisticsProps> = ({ contestStatus, onMenuStateChange }) => {
const asPath = usePathname();
const { chainName, address } = extractPathSegments(asPath ?? "");
const chainId = chains.filter(chain => chain.name.toLowerCase() === chainName.toLowerCase())[0]?.id;
const { sortBy, submissionsCount } = useProposalStore(state => state);
const { sortProposalData } = useProposal();
const { contestAbi: abi, version } = useContestStore(state => state);
const isSubmissionOrVotingOpen =
contestStatus === ContestStatus.SubmissionOpen || contestStatus === ContestStatus.VotingOpen;
const { contestState } = useContestStateStore(state => state);
const isContestCanceled = contestState === ContestStateEnum.Canceled;

const handleSortTypeChange = (value: string) => {
sortProposalData(value as SortOptions);
sortProposalData({ address: address as `0x${string}`, abi, chainId }, version, value as SortOptions);
};

const contestStatusTitle = useMemo<string>(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/* eslint-disable react/no-unescaped-entities */
import ButtonV3, { ButtonSize } from "@components/UI/ButtonV3";
import { chains } from "@config/wagmi";
import { extractPathSegments } from "@helpers/extractPath";
import { useAccountChange } from "@hooks/useAccountChange";
import { ContractConfig } from "@hooks/useContest";
import { useContestStore } from "@hooks/useContest/store";
import { useContestStatusStore } from "@hooks/useContestStatus/store";
import useUser from "@hooks/useUser";
import { useUserStore } from "@hooks/useUser/store";
import { useConnectModal } from "@rainbow-me/rainbowkit";
import Image from "next/image";
Expand All @@ -12,9 +16,10 @@ import { useMediaQuery } from "react-responsive";
import { formatEther } from "viem";
import { useAccount } from "wagmi";
import VotingQualifierMessage from "./components/VotingQualifierMessage";
import { useEffect, useState } from "react";

const VotingContestQualifier = () => {
const { anyoneCanVote, charge } = useContestStore(state => state);
const { anyoneCanVote, charge, contestAbi, version } = useContestStore(state => state);
const { isConnected, address } = useAccount();
const { openConnectModal } = useConnectModal();
const {
Expand All @@ -28,7 +33,7 @@ const VotingContestQualifier = () => {
const isMobile = useMediaQuery({ maxWidth: 768 });
const costToVoteFormatted = formatEther(BigInt(charge?.type.costToVote ?? 0));
const asPath = usePathname();
const { chainName } = extractPathSegments(asPath ?? "");
const { address: contestAddress, chainName } = extractPathSegments(asPath ?? "");
const nativeCurrencySymbol = chains.find(chain => chain.name.toLowerCase() === chainName.toLowerCase())
?.nativeCurrency.symbol;
const chainId = chains.find(chain => chain.name.toLowerCase() === chainName.toLowerCase())?.id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
import { useContestStore } from "@hooks/useContest/store";
import { useEditorStore } from "@hooks/useEditor/store";
import useEmailSignup from "@hooks/useEmailSignup";
import useMetadataFields from "@hooks/useMetadataFields";
import { useMetadataStore } from "@hooks/useMetadataFields/store";
import useSubmitProposal from "@hooks/useSubmitProposal";
import { useSubmitProposalStore } from "@hooks/useSubmitProposal/store";
import { useUploadImageStore } from "@hooks/useUploadImage";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import ButtonV3, { ButtonSize } from "@components/UI/ButtonV3";
import ProposalContent from "@components/_pages/ProposalContent";
import { chains } from "@config/wagmi";
import { extractPathSegments } from "@helpers/extractPath";
import { useContestStore } from "@hooks/useContest/store";
import { ContestStatus, useContestStatusStore } from "@hooks/useContestStatus/store";
import useDeleteProposal from "@hooks/useDeleteProposal";
import useProposal, { PROPOSALS_PER_PAGE } from "@hooks/useProposal";
import { useProposalStore } from "@hooks/useProposal/store";
import { usePathname } from "next/navigation";
import { useState } from "react";
import InfiniteScroll from "react-infinite-scroll-component";
import Skeleton, { SkeletonTheme } from "react-loading-skeleton";
Expand All @@ -22,6 +25,12 @@ const ProposalSkeleton = ({ count, highlightColor }: { count?: number; highlight

export const ListProposals = () => {
const { address } = useAccount();
const asPath = usePathname();
const { address: contestAddress, chainName: contestChainName } = extractPathSegments(asPath);
const chainId = chains.filter(
(chain: { name: string }) => chain.name.toLowerCase() === contestChainName.toLowerCase(),
)?.[0]?.id;

const { fetchProposalsPage } = useProposal();
const { deleteProposal, isLoading: isDeleteInProcess, isSuccess: isDeleteSuccess } = useDeleteProposal();
const {
Expand All @@ -34,7 +43,7 @@ export const ListProposals = () => {
totalPagesPaginationProposals,
listProposalsData,
} = useProposalStore(state => state);
const { contestAuthorEthereumAddress } = useContestStore(state => state);
const { contestAuthorEthereumAddress, contestAbi: abi, version } = useContestStore(state => state);
const contestStatus = useContestStatusStore(state => state.contestStatus);
const allowDelete =
(contestStatus === ContestStatus.SubmissionOpen || contestStatus === ContestStatus.VotingOpen) &&
Expand Down Expand Up @@ -83,6 +92,12 @@ export const ListProposals = () => {
dataLength={listProposalsData.length}
next={() =>
fetchProposalsPage(
{
chainId,
address: contestAddress as `0x${string}`,
abi,
},
version,
currentPagePaginationProposals + 1,
indexPaginationProposals[currentPagePaginationProposals + 1],
totalPagesPaginationProposals,
Expand Down
3 changes: 2 additions & 1 deletion packages/react-app-revamp/hooks/useCastVotes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function useCastVotes() {
canUpdateVotesInRealTime,
charge,
contestAbi: abi,
version,
anyoneCanVote,
rewardsModuleAddress,
} = useContestStore(state => state);
Expand Down Expand Up @@ -184,7 +185,7 @@ export function useCastVotes() {
}
}

await updateCurrentUserVotes(anyoneCanVote);
await updateCurrentUserVotes(abi, version, anyoneCanVote);
refetchTotalVotesCastOnContest();
refetchCurrentUserVotesOnProposal();
setIsLoading(false);
Expand Down
16 changes: 9 additions & 7 deletions packages/react-app-revamp/hooks/useContest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface ContractConfigResult {
version: string;
}

interface ContractConfig {
export interface ContractConfig {
address: `0x${string}`;
abi: any;
chainId: number;
Expand Down Expand Up @@ -72,7 +72,6 @@ export function useContest() {
setSortingEnabled,
setVersion,
setRewardsModuleAddress,
rewardsModuleAddress,
setRewardsAbi,
} = useContestStore(state => state);
const { setIsListProposalsSuccess, setIsListProposalsLoading, setListProposalsIds } = useProposalStore(
Expand Down Expand Up @@ -224,7 +223,7 @@ export function useContest() {
setIsSuccess(true);
setIsLoading(false);

await fetchProposalsIdsList(contractConfig.abi, {
await fetchProposalsIdsList(contractConfig, version, {
submissionOpen: submissionsOpenDate,
votesOpen: votesOpenDate,
});
Expand All @@ -236,7 +235,7 @@ export function useContest() {

await Promise.all([
fetchContestContractData(contractConfig, version, rewardsModuleAddress),
processUserQualifications(),
processUserQualifications(contractConfig, version),
processRequirementsData(),
]);
} catch (e) {
Expand Down Expand Up @@ -284,7 +283,7 @@ export function useContest() {
setIsLoading(false);

// List of proposals for this contest
await fetchProposalsIdsList(contractConfig.abi, {
await fetchProposalsIdsList(contractConfig, version, {
submissionOpen: submissionsOpenDate,
votesOpen: votesOpenDate,
});
Expand Down Expand Up @@ -370,10 +369,13 @@ export function useContest() {
/**
* Fetch merkle tree data from DB and re-create the tree
*/
async function processUserQualifications() {
async function processUserQualifications(contractConfig: ContractConfig, version: string) {
if (contestStatus === ContestStatus.VotingClosed) return;

await Promise.all([checkIfCurrentUserQualifyToSubmit(), checkIfCurrentUserQualifyToVote()]);
await Promise.all([
checkIfCurrentUserQualifyToSubmit(contractConfig, version),
checkIfCurrentUserQualifyToVote(contractConfig, version),
]);
}

async function processRequirementsData() {
Expand Down
18 changes: 0 additions & 18 deletions packages/react-app-revamp/hooks/useContractVersion/index.ts

This file was deleted.

22 changes: 6 additions & 16 deletions packages/react-app-revamp/hooks/useMetadataFields/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { chains } from "@config/wagmi";
import { extractPathSegments } from "@helpers/extractPath";
import getContestContractVersion from "@helpers/getContestContractVersion";
import { useContestStore } from "@hooks/useContest/store";
import { MetadataField } from "@hooks/useDeployContest/store";
import { useQuery } from "@tanstack/react-query";
import { compareVersions } from "compare-versions";
import { usePathname } from "next/navigation";
import { useEffect } from "react";
Expand Down Expand Up @@ -42,28 +41,19 @@ const useMetadataFields = () => {
const pathname = usePathname();
const { address: contestAddress, chainName: contestChainName } = extractPathSegments(pathname ?? "");
const contestChainId = chains.find(chain => chain.name.toLowerCase().replace(" ", "") === contestChainName)?.id;

const {
data: contractData,
isLoading: isContractDataLoading,
isError: isContractDataError,
} = useQuery({
queryKey: ["contestContract", contestAddress, contestChainId],
queryFn: () => getContestContractVersion(contestAddress, contestChainId!),
enabled: Boolean(contestAddress && contestChainId),
});
const { contestAbi, version } = useContestStore(state => state);

const {
data: metadataSchema,
isError: isMetadataError,
isLoading: isMetadataLoading,
} = useReadContract({
abi: contractData?.abi as Abi,
abi: contestAbi as Abi,
address: contestAddress as `0x${string}`,
chainId: contestChainId,
functionName: "metadataFieldsSchema",
query: {
enabled: Boolean(contractData && compareVersions(contractData.version, METADATA_FIELDS_VERSION) >= 0),
enabled: Boolean(compareVersions(version, METADATA_FIELDS_VERSION) >= 0),
},
});

Expand All @@ -78,8 +68,8 @@ const useMetadataFields = () => {
}, [metadataSchema, isMetadataError, setFields]);

return {
isLoading: isContractDataLoading || isMetadataLoading,
isError: isContractDataError || isMetadataError,
isLoading: isMetadataLoading,
isError: isMetadataError,
};
};

Expand Down
Loading

0 comments on commit 4118283

Please sign in to comment.