Skip to content

Commit

Permalink
fix(frontend): fix creation allowance check for connected addresses (#…
Browse files Browse the repository at this point in the history
…451)

* fix(frontend): fix creation allowance check for connected addresses

* chore: add changeset

* fix(frontend): add creator to enabled check in use is creator allowed

---------

Co-authored-by: luzzifoss <fedeluzzi00@gmail.com>
  • Loading branch information
luzzif and luzzifoss authored Oct 23, 2023
1 parent ed8c198 commit e838ea3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-horses-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@carrot-kpi/host-frontend": patch
---

Fix creation allowance check for connected address while creating campaign
30 changes: 30 additions & 0 deletions packages/frontend/src/hooks/useIsCreatorAllowed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { Address } from "viem";
import { useChainId, useContractReads } from "wagmi";
import { CHAIN_ADDRESSES, FACTORY_ABI, ChainId } from "@carrot-kpi/sdk";

export const useIsCreatorAllowed = (creator?: Address) => {
const chainId = useChainId();

const factoryAddress = CHAIN_ADDRESSES[chainId as ChainId].factory;
const { data, isLoading } = useContractReads({
contracts: [
{
address: factoryAddress,
abi: FACTORY_ABI,
functionName: "permissionless",
},
{
address: factoryAddress,
abi: FACTORY_ABI,
functionName: "creatorAllowed",
args: [creator as Address],
},
],
enabled: !!creator && chainId in ChainId,
});

return {
loading: isLoading,
allowed: !!(data?.[0].result || data?.[1].result),
};
};
31 changes: 7 additions & 24 deletions packages/frontend/src/pages/create-with-template-id/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,16 @@ import {
} from "@carrot-kpi/react";
import { useLocation, useNavigate, useParams } from "react-router-dom";
import { useTranslation } from "react-i18next";
import {
useAccount,
useNetwork,
usePublicClient,
type Address,
useContractRead,
} from "wagmi";
import {
CHAIN_ADDRESSES,
ChainId,
FACTORY_ABI,
Fetcher,
ResolvedTemplate,
} from "@carrot-kpi/sdk";
import { useAccount, useNetwork, usePublicClient } from "wagmi";
import { Fetcher, ResolvedTemplate } from "@carrot-kpi/sdk";
import { ErrorFeedback, Loader } from "@carrot-kpi/ui";
import { useAddTransaction } from "../../hooks/useAddTransaction";
import { Authenticate } from "../../components/authenticate";
import { useIsPinningProxyAuthenticated } from "../../hooks/useIsPinningProxyAuthenticated";
import { useInvalidateLatestKPITokens } from "../../hooks/useInvalidateLatestKPITokens";
import { Layout } from "../../components/layout";
import { Permissioned } from "../../components/permissioned";
import { useIsCreatorAllowed } from "../../hooks/useIsCreatorAllowed";

export const CreateWithTemplateId = () => {
const { i18n, t } = useTranslation();
Expand All @@ -48,6 +37,9 @@ export const CreateWithTemplateId = () => {
const [formKey, setFormKey] = useState(0);
const pinningProxyAuthenticated = useIsPinningProxyAuthenticated();

const { allowed: creatorAllowed, loading: loadingPermission } =
useIsCreatorAllowed(address);

// every time the chain or the connected address changes,
// reset the creation form state
useEffect(() => {
Expand Down Expand Up @@ -124,15 +116,6 @@ export const CreateWithTemplateId = () => {
templateId,
]);

const { data: creatorAllowed, isLoading: loadingCreatorAllowed } =
useContractRead({
address: CHAIN_ADDRESSES[chain?.id as ChainId].factory,
abi: FACTORY_ABI,
functionName: "creatorAllowed",
args: [address as Address],
enabled: !!chain?.id && !!address,
});

const handleCreate = useCallback(() => {
invalidateLatestKPITokens();
}, [invalidateLatestKPITokens]);
Expand All @@ -144,7 +127,7 @@ export const CreateWithTemplateId = () => {
return (
<Layout navbarBgColor="green" noMarquee>
<div className="h-screen flex-grow bg-grid-light bg-left-top bg-green">
{loading || loadingCreatorAllowed ? (
{loading || loadingPermission ? (
<div className="py-20 text-black flex justify-center">
<Loader />
</div>
Expand Down

0 comments on commit e838ea3

Please sign in to comment.