Skip to content

Commit

Permalink
Fix readContract
Browse files Browse the repository at this point in the history
  • Loading branch information
xavikh committed Mar 26, 2024
1 parent fdc81a0 commit e4f21a9
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions plugins/dualGovernance/hooks/useCanCreateProposal.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { Address, keccak256, toHex } from "viem";
import { useState, useEffect } from "react";
import { useBalance, useAccount, useReadContracts, useConfig } from "wagmi";
import { readContract } from "@wagmi/core";
import { OptimisticTokenVotingPluginAbi } from "@/plugins/dualGovernance/artifacts/OptimisticTokenVotingPlugin.sol";
import { DaoAbi } from "@/artifacts/DAO.sol";
import { PUB_CHAIN, PUB_DUAL_GOVERNANCE_PLUGIN_ADDRESS } from "@/constants";

export function useCanCreateProposal() {
const config = useConfig();
const { address } = useAccount();
const [minProposerVotingPower, setMinProposerVotingPower] =
useState<bigint>();
const [votingToken, setVotingToken] = useState<Address>();
const [daoAddress, setDaoAddress] = useState<Address>();
const [hasCreatePermission, setHasCreatePermission] = useState(false);
const { data: balance } = useBalance({
address,
token: votingToken,
Expand All @@ -39,11 +36,30 @@ export function useCanCreateProposal() {
address: PUB_DUAL_GOVERNANCE_PLUGIN_ADDRESS,
abi: OptimisticTokenVotingPluginAbi,
functionName: "dao",
},
}
// TODO: This needs to be checking as well if address has the DAO permission to create props
],
});

// Check if PROPOSER_PERMISSION is granted to the current wallet
const { data: hasCreatePermission, refetch: hasCreatePermissionRefetch } = useReadContracts({
contracts: [
{
chainId: PUB_CHAIN.id,
address: daoAddress,
abi: DaoAbi,
functionName: "hasPermission",
// where, who, permissionId, data
args: [
PUB_DUAL_GOVERNANCE_PLUGIN_ADDRESS,
address,
keccak256(toHex("PROPOSER_PERMISSION")),
"0x",
],
},
],
});

useEffect(() => {
if (!contractReads?.length || contractReads?.length < 2) return;

Expand All @@ -59,26 +75,7 @@ export function useCanCreateProposal() {
// Check if PROPOSER_PERMISSION is granted to the current wallet
useEffect(() => {
if (!address || !daoAddress) return;

readContract(config, {
chainId: PUB_CHAIN.id,
address: daoAddress,
abi: DaoAbi,
functionName: "hasPermission",
// where, who, permissionId, data
args: [
PUB_DUAL_GOVERNANCE_PLUGIN_ADDRESS,
address,
keccak256(toHex("PROPOSER_PERMISSION")),
"0x",
],
})
.then((result) => {
setHasCreatePermission(!!result);
})
.catch((err) => {
console.error(err);
});
hasCreatePermissionRefetch();
}, [daoAddress, address]);

if (!address) return false;
Expand Down

0 comments on commit e4f21a9

Please sign in to comment.