From 52135947cd5b49f038fa2eb593e4e635099afdb4 Mon Sep 17 00:00:00 2001 From: Kirill Klimenko Date: Thu, 7 Mar 2024 16:15:18 +0100 Subject: [PATCH] Reduce usages of useEthersProvider, fix lint errors, add more conditional logic whenever applicable to check that provider is not undefined --- .../DaoCreator/hooks/usePrepareFormData.ts | 215 ++++++++++-------- .../DaoDashboard/Info/InfoGovernance.tsx | 6 +- .../Signers/modals/AddSignerModal.tsx | 2 +- .../Signers/modals/RemoveSignerModal.tsx | 8 +- src/components/ui/modals/DelegateModal.tsx | 2 +- .../ui/modals/ForkProposalTemplateModal.tsx | 7 +- src/components/ui/modals/UnwrapToken.tsx | 2 +- src/components/ui/modals/WrapToken.tsx | 2 +- .../ui/proposal/useProposalCountdown.tsx | 11 +- src/graphql/utils/index.ts | 14 +- .../governance/useERC20LinearStrategy.ts | 2 +- .../governance/useERC721LinearStrategy.ts | 2 +- src/hooks/DAO/loaders/useFractalFreeze.ts | 4 +- .../DAO/loaders/useFractalGuardContracts.ts | 17 +- .../DAO/loaders/useGovernanceContracts.ts | 3 +- src/hooks/DAO/proposal/useSubmitProposal.ts | 6 +- src/hooks/DAO/useClawBack.ts | 2 +- src/hooks/DAO/useDAOName.ts | 19 +- src/hooks/safe/useSafeContracts.ts | 3 + src/hooks/utils/useAvatar.ts | 8 +- src/hooks/utils/useDisplayName.ts | 8 +- src/hooks/utils/useSafeTransactions.ts | 4 +- 22 files changed, 178 insertions(+), 169 deletions(-) diff --git a/src/components/DaoCreator/hooks/usePrepareFormData.ts b/src/components/DaoCreator/hooks/usePrepareFormData.ts index 54bece8582..0b4e2ac2ce 100644 --- a/src/components/DaoCreator/hooks/usePrepareFormData.ts +++ b/src/components/DaoCreator/hooks/usePrepareFormData.ts @@ -24,39 +24,43 @@ export function usePrepareFormData() { const prepareFreezeGuardData = useCallback( async ( freezeGuard: DAOFreezeGuardConfig - ): Promise => { - return { - executionPeriod: await getEstimatedNumberOfBlocks( - freezeGuard.executionPeriod.bigNumberValue!, - provider - ), - timelockPeriod: await getEstimatedNumberOfBlocks( - freezeGuard.timelockPeriod.bigNumberValue!, - provider - ), - freezeVotesThreshold: freezeGuard.freezeVotesThreshold.bigNumberValue!, - freezeProposalPeriod: await getEstimatedNumberOfBlocks( - freezeGuard.freezeProposalPeriod.bigNumberValue!, - provider - ), - freezePeriod: await getEstimatedNumberOfBlocks( - freezeGuard.freezePeriod.bigNumberValue!, - provider - ), - }; + ): Promise => { + if (provider) { + return { + executionPeriod: await getEstimatedNumberOfBlocks( + freezeGuard.executionPeriod.bigNumberValue!, + provider + ), + timelockPeriod: await getEstimatedNumberOfBlocks( + freezeGuard.timelockPeriod.bigNumberValue!, + provider + ), + freezeVotesThreshold: freezeGuard.freezeVotesThreshold.bigNumberValue!, + freezeProposalPeriod: await getEstimatedNumberOfBlocks( + freezeGuard.freezeProposalPeriod.bigNumberValue!, + provider + ), + freezePeriod: await getEstimatedNumberOfBlocks( + freezeGuard.freezePeriod.bigNumberValue!, + provider + ), + }; + } }, [provider] ); const checkVotesToken = useCallback( async (address: string) => { - try { - const votesContract = IVotes__factory.connect(address, provider); - await votesContract.delegates('0x0000000000000000000000000000000000000001'); - await votesContract.getVotes('0x0000000000000000000000000000000000000001'); - return true; - } catch (error) { - return false; + if (provider) { + try { + const votesContract = IVotes__factory.connect(address, provider); + await votesContract.delegates('0x0000000000000000000000000000000000000001'); + await votesContract.getVotes('0x0000000000000000000000000000000000000001'); + return true; + } catch (error) { + return false; + } } }, [provider] @@ -77,15 +81,17 @@ export function usePrepareFormData() { return inputValue; }) ); - let freezeGuardData: Partial = {}; + let freezeGuardData; if (freezeGuard) { freezeGuardData = await prepareFreezeGuardData(freezeGuard); } - return { - trustedAddresses: resolvedAddresses, - ...freezeGuardData, - ...rest, - }; + if (freezeGuardData) { + return { + trustedAddresses: resolvedAddresses, + ...freezeGuardData, + ...rest, + }; + } }, [signer, prepareFreezeGuardData] ); @@ -103,44 +109,48 @@ export function usePrepareFormData() { tokenImportAddress, tokenCreationType, ...rest - }: AzoriusERC20DAO & FreezeGuardConfigParam): Promise => { - const resolvedTokenAllocations = await Promise.all( - tokenAllocations.map(async allocation => { - let address = allocation.address; - if (couldBeENS(address)) { - address = await signer!.resolveName(allocation.address); - } - return { amount: allocation.amount.bigNumberValue!, address: address }; - }) - ); - let freezeGuardData: Partial = {}; - if (freezeGuard) { - freezeGuardData = await prepareFreezeGuardData(freezeGuard); - } - const isTokenImported = - tokenCreationType === TokenCreationType.IMPORTED && !!tokenImportAddress; - let isVotesToken = false; - if (isTokenImported) { - isVotesToken = await checkVotesToken(tokenImportAddress); + }: AzoriusERC20DAO & FreezeGuardConfigParam): Promise< + AzoriusERC20DAO | undefined + > => { + if (provider) { + const resolvedTokenAllocations = await Promise.all( + tokenAllocations.map(async allocation => { + let address = allocation.address; + if (couldBeENS(address)) { + address = await signer!.resolveName(allocation.address); + } + return { amount: allocation.amount.bigNumberValue!, address: address }; + }) + ); + let freezeGuardData; + if (freezeGuard) { + freezeGuardData = await prepareFreezeGuardData(freezeGuard); + } + const isTokenImported = + tokenCreationType === TokenCreationType.IMPORTED && !!tokenImportAddress; + let isVotesToken: boolean | undefined = false; + if (isTokenImported) { + isVotesToken = await checkVotesToken(tokenImportAddress); + } + return { + tokenSupply: tokenSupply.bigNumberValue!, + parentAllocationAmount: parentAllocationAmount?.bigNumberValue!, + quorumPercentage: quorumPercentage.bigNumberValue!, + timelock: await getEstimatedNumberOfBlocks(timelock.bigNumberValue!, provider), + executionPeriod: await getEstimatedNumberOfBlocks( + executionPeriod.bigNumberValue!, + provider + ), + votingPeriod: await getEstimatedNumberOfBlocks(votingPeriod.bigNumberValue!, provider), + tokenAllocations: resolvedTokenAllocations, + tokenImportAddress, + tokenCreationType, + isTokenImported, + isVotesToken, + ...freezeGuardData, + ...rest, + }; } - return { - tokenSupply: tokenSupply.bigNumberValue!, - parentAllocationAmount: parentAllocationAmount?.bigNumberValue!, - quorumPercentage: quorumPercentage.bigNumberValue!, - timelock: await getEstimatedNumberOfBlocks(timelock.bigNumberValue!, provider), - executionPeriod: await getEstimatedNumberOfBlocks( - executionPeriod.bigNumberValue!, - provider - ), - votingPeriod: await getEstimatedNumberOfBlocks(votingPeriod.bigNumberValue!, provider), - tokenAllocations: resolvedTokenAllocations, - tokenImportAddress, - tokenCreationType, - isTokenImported, - isVotesToken, - ...freezeGuardData, - ...rest, - }; }, [signer, checkVotesToken, provider, prepareFreezeGuardData] ); @@ -155,39 +165,42 @@ export function usePrepareFormData() { nfts, quorumThreshold, ...rest - }: AzoriusERC721DAO & - FreezeGuardConfigParam): Promise => { - let freezeGuardData: Partial = {}; - if (freezeGuard) { - freezeGuardData = await prepareFreezeGuardData(freezeGuard); - } + }: AzoriusERC721DAO & FreezeGuardConfigParam): Promise< + AzoriusERC721DAO | undefined + > => { + if (provider) { + let freezeGuardData; + if (freezeGuard) { + freezeGuardData = await prepareFreezeGuardData(freezeGuard); + } - const resolvedNFTs = await Promise.all( - nfts.map(async nft => { - let address = nft.tokenAddress; - if (couldBeENS(address)) { - address = await signer!.resolveName(nft.tokenAddress); - } - return { - tokenAddress: address, - tokenWeight: nft.tokenWeight.bigNumberValue!, - }; - }) - ); + const resolvedNFTs = await Promise.all( + nfts.map(async nft => { + let address = nft.tokenAddress; + if (couldBeENS(address)) { + address = await signer!.resolveName(nft.tokenAddress); + } + return { + tokenAddress: address, + tokenWeight: nft.tokenWeight.bigNumberValue!, + }; + }) + ); - return { - quorumPercentage: quorumPercentage.bigNumberValue!, - timelock: await getEstimatedNumberOfBlocks(timelock.bigNumberValue!, provider), - executionPeriod: await getEstimatedNumberOfBlocks( - executionPeriod.bigNumberValue!, - provider - ), - votingPeriod: await getEstimatedNumberOfBlocks(votingPeriod.bigNumberValue!, provider), - nfts: resolvedNFTs, - quorumThreshold: quorumThreshold.bigNumberValue!, - ...freezeGuardData, - ...rest, - }; + return { + quorumPercentage: quorumPercentage.bigNumberValue!, + timelock: await getEstimatedNumberOfBlocks(timelock.bigNumberValue!, provider), + executionPeriod: await getEstimatedNumberOfBlocks( + executionPeriod.bigNumberValue!, + provider + ), + votingPeriod: await getEstimatedNumberOfBlocks(votingPeriod.bigNumberValue!, provider), + nfts: resolvedNFTs, + quorumThreshold: quorumThreshold.bigNumberValue!, + ...freezeGuardData, + ...rest, + }; + } }, [prepareFreezeGuardData, provider, signer] ); diff --git a/src/components/pages/DaoDashboard/Info/InfoGovernance.tsx b/src/components/pages/DaoDashboard/Info/InfoGovernance.tsx index b954621778..611b02fbad 100644 --- a/src/components/pages/DaoDashboard/Info/InfoGovernance.tsx +++ b/src/components/pages/DaoDashboard/Info/InfoGovernance.tsx @@ -3,9 +3,9 @@ import { Govern } from '@decent-org/fractal-ui'; import { MultisigFreezeGuard } from '@fractal-framework/fractal-contracts'; import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { useEthersProvider } from '../../../../providers/Ethers/hooks/useEthersProvider'; import { useTimeHelpers } from '../../../../hooks/utils/useTimeHelpers'; import { useFractal } from '../../../../providers/App/AppProvider'; +import { useEthersProvider } from '../../../../providers/Ethers/hooks/useEthersProvider'; import { AzoriusGovernance, FreezeGuardType } from '../../../../types'; import { blocksToSeconds } from '../../../../utils/contract'; import { BarLoader } from '../../../ui/loaders/BarLoader'; @@ -25,7 +25,9 @@ export function InfoGovernance() { useEffect(() => { const setTimelockInfo = async () => { const formatBlocks = async (blocks: number): Promise => { - return getTimeDuration(await blocksToSeconds(blocks, provider)); + if (provider) { + return getTimeDuration(await blocksToSeconds(blocks, provider)); + } }; if (freezeGuardType == FreezeGuardType.MULTISIG) { if (freezeGuardContract) { diff --git a/src/components/pages/DaoSettings/components/Signers/modals/AddSignerModal.tsx b/src/components/pages/DaoSettings/components/Signers/modals/AddSignerModal.tsx index 55d7eb12cb..5f2ef2e9e4 100644 --- a/src/components/pages/DaoSettings/components/Signers/modals/AddSignerModal.tsx +++ b/src/components/pages/DaoSettings/components/Signers/modals/AddSignerModal.tsx @@ -16,8 +16,8 @@ import { useCallback, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import * as Yup from 'yup'; import { useValidationAddress } from '../../../../../../hooks/schemas/common/useValidationAddress'; -import { useEthersSigner } from '../../../../../../providers/Ethers/hooks/useEthersSigner'; import { useFractal } from '../../../../../../providers/App/AppProvider'; +import { useEthersSigner } from '../../../../../../providers/Ethers/hooks/useEthersSigner'; import { couldBeENS } from '../../../../../../utils/url'; import SupportTooltip from '../../../../../ui/badges/SupportTooltip'; import { CustomNonceInput } from '../../../../../ui/forms/CustomNonceInput'; diff --git a/src/components/pages/DaoSettings/components/Signers/modals/RemoveSignerModal.tsx b/src/components/pages/DaoSettings/components/Signers/modals/RemoveSignerModal.tsx index 4cbf5454f8..2831fd4772 100644 --- a/src/components/pages/DaoSettings/components/Signers/modals/RemoveSignerModal.tsx +++ b/src/components/pages/DaoSettings/components/Signers/modals/RemoveSignerModal.tsx @@ -12,8 +12,7 @@ import { } from '@chakra-ui/react'; import { useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { Address, useEnsName } from 'wagmi'; -import { useEthersProvider } from '../../../../../../providers/Ethers/hooks/useEthersProvider'; +import { Address, useEnsName, usePublicClient } from 'wagmi'; import { useFractal } from '../../../../../../providers/App/AppProvider'; import SupportTooltip from '../../../../../ui/badges/SupportTooltip'; import { CustomNonceInput } from '../../../../../ui/forms/CustomNonceInput'; @@ -37,11 +36,10 @@ function RemoveSignerModal({ const [prevSigner, setPrevSigner] = useState(''); const [threshold, setThreshold] = useState(currentThreshold); const [nonce, setNonce] = useState(safe!.nonce); - const provider = useEthersProvider(); - const networkId = provider.network.chainId; + const { chain } = usePublicClient(); const { data: ensName } = useEnsName({ address: selectedSigner as Address, - chainId: networkId, + chainId: chain.id, cacheTime: 1000 * 60 * 30, // 30 min }); const { t } = useTranslation(['modals', 'common']); diff --git a/src/components/ui/modals/DelegateModal.tsx b/src/components/ui/modals/DelegateModal.tsx index db041c035e..4cecf59d7d 100644 --- a/src/components/ui/modals/DelegateModal.tsx +++ b/src/components/ui/modals/DelegateModal.tsx @@ -7,8 +7,8 @@ import * as Yup from 'yup'; import useDelegateVote from '../../../hooks/DAO/useDelegateVote'; import { useValidationAddress } from '../../../hooks/schemas/common/useValidationAddress'; import useDisplayName from '../../../hooks/utils/useDisplayName'; -import { useEthersSigner } from '../../../providers/Ethers/hooks/useEthersSigner'; import { useFractal } from '../../../providers/App/AppProvider'; +import { useEthersSigner } from '../../../providers/Ethers/hooks/useEthersSigner'; import { AzoriusGovernance, DecentGovernance } from '../../../types'; import { formatCoin } from '../../../utils/numberFormats'; import { couldBeENS } from '../../../utils/url'; diff --git a/src/components/ui/modals/ForkProposalTemplateModal.tsx b/src/components/ui/modals/ForkProposalTemplateModal.tsx index 9a94189acd..60616de28d 100644 --- a/src/components/ui/modals/ForkProposalTemplateModal.tsx +++ b/src/components/ui/modals/ForkProposalTemplateModal.tsx @@ -7,8 +7,7 @@ import { DAO_ROUTES } from '../../../constants/routes'; import useSubmitProposal from '../../../hooks/DAO/proposal/useSubmitProposal'; import { useIsSafe } from '../../../hooks/safe/useIsSafe'; import { validateAddress } from '../../../hooks/schemas/common/useValidationAddress'; -import { useEthersProvider } from '../../../providers/Ethers/hooks/useEthersProvider'; -import { useEthersSigner } from '../../../providers/Ethers/hooks/useEthersSigner'; +import useSignerOrProvider from '../../../hooks/utils/useSignerOrProvider'; import { useFractal } from '../../../providers/App/AppProvider'; import { disconnectedChain } from '../../../providers/NetworkConfig/NetworkConfigProvider'; import { ProposalTemplate } from '../../../types/createProposalTemplate'; @@ -31,9 +30,7 @@ export default function ForkProposalTemplateModal({ const { t } = useTranslation('proposalTemplate'); const { push } = useRouter(); - const provider = useEthersProvider(); - const signer = useEthersSigner(); - const signerOrProvider = signer || provider; + const signerOrProvider = useSignerOrProvider(); const { chain } = useNetwork(); const { node: { proposalTemplatesHash }, diff --git a/src/components/ui/modals/UnwrapToken.tsx b/src/components/ui/modals/UnwrapToken.tsx index f88a5db66f..8fa4bdb1cd 100644 --- a/src/components/ui/modals/UnwrapToken.tsx +++ b/src/components/ui/modals/UnwrapToken.tsx @@ -9,10 +9,10 @@ import { useAccount } from 'wagmi'; import * as Yup from 'yup'; import { useERC20LinearToken } from '../../../hooks/DAO/loaders/governance/useERC20LinearToken'; import useApproval from '../../../hooks/utils/useApproval'; -import { useEthersSigner } from '../../../providers/Ethers/hooks/useEthersSigner'; import { useFormHelpers } from '../../../hooks/utils/useFormHelpers'; import { useTransaction } from '../../../hooks/utils/useTransaction'; import { useFractal } from '../../../providers/App/AppProvider'; +import { useEthersSigner } from '../../../providers/Ethers/hooks/useEthersSigner'; import { AzoriusGovernance, BigNumberValuePair } from '../../../types'; import { formatCoin } from '../../../utils'; import { BigNumberInput } from '../forms/BigNumberInput'; diff --git a/src/components/ui/modals/WrapToken.tsx b/src/components/ui/modals/WrapToken.tsx index ed502c7a3e..0d3eac4331 100644 --- a/src/components/ui/modals/WrapToken.tsx +++ b/src/components/ui/modals/WrapToken.tsx @@ -10,10 +10,10 @@ import * as Yup from 'yup'; import { logError } from '../../../helpers/errorLogging'; import { useERC20LinearToken } from '../../../hooks/DAO/loaders/governance/useERC20LinearToken'; import useApproval from '../../../hooks/utils/useApproval'; -import { useEthersSigner } from '../../../providers/Ethers/hooks/useEthersSigner'; import { useFormHelpers } from '../../../hooks/utils/useFormHelpers'; import { useTransaction } from '../../../hooks/utils/useTransaction'; import { useFractal } from '../../../providers/App/AppProvider'; +import { useEthersSigner } from '../../../providers/Ethers/hooks/useEthersSigner'; import { AzoriusGovernance, BigNumberValuePair } from '../../../types'; import { formatCoin } from '../../../utils'; import { BigNumberInput } from '../forms/BigNumberInput'; diff --git a/src/components/ui/proposal/useProposalCountdown.tsx b/src/components/ui/proposal/useProposalCountdown.tsx index a8d6ae538b..254cd1867d 100644 --- a/src/components/ui/proposal/useProposalCountdown.tsx +++ b/src/components/ui/proposal/useProposalCountdown.tsx @@ -5,8 +5,8 @@ import { logError } from '../../../helpers/errorLogging'; import useSnapshotProposal from '../../../hooks/DAO/loaders/snapshot/useSnapshotProposal'; import { useDAOProposals } from '../../../hooks/DAO/loaders/useProposals'; import useUpdateProposalState from '../../../hooks/DAO/proposal/useUpdateProposalState'; -import { useEthersProvider } from '../../../providers/Ethers/hooks/useEthersProvider'; import { useFractal } from '../../../providers/App/AppProvider'; +import { useEthersProvider } from '../../../providers/Ethers/hooks/useEthersProvider'; import { AzoriusGovernance, FractalProposal, @@ -119,7 +119,12 @@ export function useProposalCountdown(proposal: FractalProposal) { ) { startCountdown(votingDeadlineMs + Number(timeLockPeriod.value) * 1000); // If the proposal is timelocked start the countdown (for safe multisig proposals with guards) - } else if (proposal.state === FractalProposalState.TIMELOCKED && freezeGuard && isSafeGuard) { + } else if ( + proposal.state === FractalProposalState.TIMELOCKED && + freezeGuard && + isSafeGuard && + provider + ) { const safeGuard = freezeGuard as MultisigFreezeGuard; const timelockedTimestamp = await getTxTimelockedTimestamp(proposal, safeGuard, provider); const guardTimeLockPeriod = await blocksToSeconds( @@ -130,7 +135,7 @@ export function useProposalCountdown(proposal: FractalProposal) { // If the proposal is executable start the countdown (for safe multisig proposals with guards) } else if (proposal.state === FractalProposalState.EXECUTABLE && freezeGuard) { let guardTimelockPeriod: number = 0; - if (isSafeGuard) { + if (isSafeGuard && provider) { const safeGuard = freezeGuard as MultisigFreezeGuard; const timelockedTimestamp = (await getTxTimelockedTimestamp(proposal, safeGuard, provider)) * 1000; diff --git a/src/graphql/utils/index.ts b/src/graphql/utils/index.ts index f0df4aa7e6..7ce2f56bef 100644 --- a/src/graphql/utils/index.ts +++ b/src/graphql/utils/index.ts @@ -1,19 +1,19 @@ import { useMemo } from 'react'; -import { useEthersProvider } from '../../providers/Ethers/hooks/useEthersProvider'; +import { usePublicClient } from 'wagmi'; import { supportedChains } from '../../providers/NetworkConfig/NetworkConfigProvider'; export const useSubgraphChainName = () => { - const provider = useEthersProvider(); + const { chain } = usePublicClient(); const subgraphChainName = useMemo(() => { - let chainName = provider.network.name; - supportedChains.forEach(chain => { - if (chain.chainId === provider.network.chainId) { - chainName = chain.subgraphChainName; + let chainName = chain.name; + supportedChains.forEach(supportedChain => { + if (supportedChain.chainId === chain.id) { + chainName = supportedChain.subgraphChainName; } }); return chainName; - }, [provider]); + }, [chain.id, chain.name]); return subgraphChainName; }; diff --git a/src/hooks/DAO/loaders/governance/useERC20LinearStrategy.ts b/src/hooks/DAO/loaders/governance/useERC20LinearStrategy.ts index 28c8db99a2..061bcf4714 100644 --- a/src/hooks/DAO/loaders/governance/useERC20LinearStrategy.ts +++ b/src/hooks/DAO/loaders/governance/useERC20LinearStrategy.ts @@ -22,7 +22,7 @@ export const useERC20LinearStrategy = () => { const { getTimeDuration } = useTimeHelpers(); const loadERC20Strategy = useCallback(async () => { - if (!ozLinearVotingContract || !azoriusContract) { + if (!ozLinearVotingContract || !azoriusContract || !provider) { return {}; } const [votingPeriodBlocks, quorumNumerator, quorumDenominator, timeLockPeriod] = diff --git a/src/hooks/DAO/loaders/governance/useERC721LinearStrategy.ts b/src/hooks/DAO/loaders/governance/useERC721LinearStrategy.ts index 0ba0a1a0fc..915506d284 100644 --- a/src/hooks/DAO/loaders/governance/useERC721LinearStrategy.ts +++ b/src/hooks/DAO/loaders/governance/useERC721LinearStrategy.ts @@ -24,7 +24,7 @@ export const useERC721LinearStrategy = () => { const { getTimeDuration } = useTimeHelpers(); const loadERC721Strategy = useCallback(async () => { - if (!erc721LinearVotingContract || !azoriusContract) { + if (!erc721LinearVotingContract || !azoriusContract || !provider) { return {}; } const [votingPeriodBlocks, quorumThreshold, timeLockPeriod] = await Promise.all([ diff --git a/src/hooks/DAO/loaders/useFractalFreeze.ts b/src/hooks/DAO/loaders/useFractalFreeze.ts index c8c39f4489..997626f904 100644 --- a/src/hooks/DAO/loaders/useFractalFreeze.ts +++ b/src/hooks/DAO/loaders/useFractalFreeze.ts @@ -49,7 +49,7 @@ export const useFractalFreeze = ({ const loadFractalFreezeGuard = useCallback( async ({ freezeVotingContract, freezeVotingType: freezeVotingType }: FractalGuardContracts) => { - if (freezeVotingType == null || !freezeVotingContract || !account) return; + if (freezeVotingType == null || !freezeVotingContract || !account || !provider) return; let userHasVotes: boolean = false; const freezeCreatedBlock = await ( freezeVotingContract!.asProvider as @@ -164,7 +164,7 @@ export const useFractalFreeze = ({ }, [setFractalFreezeGuard, guardContracts, daoAddress, loadOnMount]); useEffect(() => { - if (!loadOnMount) return; + if (!loadOnMount || !provider) return; const { freezeVotingContract, freezeVotingType: freezeVotingType } = guardContracts; let votingRPC: MultisigFreezeVoting | ERC20FreezeVoting | ERC721FreezeVoting; const listenerCallback: TypedListener = async ( diff --git a/src/hooks/DAO/loaders/useFractalGuardContracts.ts b/src/hooks/DAO/loaders/useFractalGuardContracts.ts index b01033aef6..777f68ada6 100644 --- a/src/hooks/DAO/loaders/useFractalGuardContracts.ts +++ b/src/hooks/DAO/loaders/useFractalGuardContracts.ts @@ -1,18 +1,18 @@ import { AzoriusFreezeGuard, MultisigFreezeGuard } from '@fractal-framework/fractal-contracts'; import { constants } from 'ethers'; import { useCallback, useEffect, useRef } from 'react'; +import { usePublicClient } from 'wagmi'; import { useFractal } from '../../../providers/App/AppProvider'; import { GuardContractAction } from '../../../providers/App/guardContracts/action'; -import { useEthersProvider } from '../../../providers/Ethers/hooks/useEthersProvider'; import { ContractConnection, SafeInfoResponseWithGuard, FreezeGuardType, FreezeVotingType, } from '../../../types'; -import { useEthersProvider } from '../../utils/useEthersProvider'; import { useMasterCopy } from '../../utils/useMasterCopy'; import { FractalModuleData, FractalModuleType } from './../../../types/fractal'; + export const useFractalGuardContracts = ({ loadOnMount = true }: { loadOnMount?: boolean }) => { // load key for component; helps prevent unnecessary calls const loadKey = useRef(); @@ -28,8 +28,7 @@ export const useFractalGuardContracts = ({ loadOnMount = true }: { loadOnMount?: action, } = useFractal(); - const provider = useEthersProvider(); - const chainId = provider?.network.chainId; + const { chain } = usePublicClient(); const { getZodiacModuleProxyMasterCopyData } = useMasterCopy(); @@ -122,14 +121,10 @@ export const useFractalGuardContracts = ({ loadOnMount = true }: { loadOnMount?: }, [action, daoAddress, safe, fractalModules, loadFractalGuardContracts]); useEffect(() => { - if (chainId === undefined) { - return; - } - - if (daoAddress && chainId + daoAddress !== loadKey.current && loadOnMount && isModulesLoaded) { - loadKey.current = chainId + daoAddress; + if (daoAddress && chain.id + daoAddress !== loadKey.current && loadOnMount && isModulesLoaded) { + loadKey.current = chain.id + daoAddress; setGuardContracts(); } - }, [setGuardContracts, isModulesLoaded, daoAddress, loadOnMount, chainId]); + }, [setGuardContracts, isModulesLoaded, daoAddress, loadOnMount, chain.id]); return loadFractalGuardContracts; }; diff --git a/src/hooks/DAO/loaders/useGovernanceContracts.ts b/src/hooks/DAO/loaders/useGovernanceContracts.ts index 3e1e8be23e..509f86a96a 100644 --- a/src/hooks/DAO/loaders/useGovernanceContracts.ts +++ b/src/hooks/DAO/loaders/useGovernanceContracts.ts @@ -13,7 +13,6 @@ import { GovernanceContractAction } from '../../../providers/App/governanceContr import { useEthersProvider } from '../../../providers/Ethers/hooks/useEthersProvider'; import { ContractConnection } from '../../../types'; import { getAzoriusModuleFromModules } from '../../../utils'; -import { useEthersProvider } from '../../utils/useEthersProvider'; import { useMasterCopy } from '../../utils/useMasterCopy'; import useSignerOrProvider from '../../utils/useSignerOrProvider'; @@ -109,7 +108,7 @@ export const useGovernanceContracts = () => { return undefined; }); - if (lockedToken && provider) { + if (lockedToken && provider && signerOrProvider) { lockReleaseContract = { asSigner: LockRelease__factory.connect(govTokenAddress, signerOrProvider), asProvider: LockRelease__factory.connect(govTokenAddress, provider), diff --git a/src/hooks/DAO/proposal/useSubmitProposal.ts b/src/hooks/DAO/proposal/useSubmitProposal.ts index 452f5c251b..8e705965a9 100644 --- a/src/hooks/DAO/proposal/useSubmitProposal.ts +++ b/src/hooks/DAO/proposal/useSubmitProposal.ts @@ -97,7 +97,7 @@ export default function useSubmitProposal() { */ const getCanUserCreateProposal = useCallback( async (safeAddress?: string): Promise => { - if (!user.address) { + if (!user.address || !safeAPI || !signerOrProvider) { return false; } @@ -282,7 +282,7 @@ export default function useSubmitProposal() { failedToastMessage, safeAddress, }: ISubmitAzoriusProposal) => { - if (!proposalData) { + if (!proposalData || !provider) { return; } const toastId = toast(pendingToastMessage, { @@ -352,7 +352,7 @@ export default function useSubmitProposal() { successCallback, safeAddress, }: ISubmitProposal) => { - if (!proposalData) { + if (!proposalData || !safeAPI) { return; } diff --git a/src/hooks/DAO/useClawBack.ts b/src/hooks/DAO/useClawBack.ts index 46177d04af..41d504f56c 100644 --- a/src/hooks/DAO/useClawBack.ts +++ b/src/hooks/DAO/useClawBack.ts @@ -19,7 +19,7 @@ export default function useClawBack({ childSafeInfo, parentAddress }: IUseClawBa const { submitProposal, canUserCreateProposal } = useSubmitProposal(); const handleClawBack = useCallback(async () => { - if (childSafeInfo && childSafeInfo.daoAddress && parentAddress) { + if (childSafeInfo && childSafeInfo.daoAddress && parentAddress && safeAPI && provider) { const childSafeBalance = await safeAPI.getBalances( utils.getAddress(childSafeInfo.daoAddress) ); diff --git a/src/hooks/DAO/useDAOName.ts b/src/hooks/DAO/useDAOName.ts index 816821dfae..b0a7921f93 100644 --- a/src/hooks/DAO/useDAOName.ts +++ b/src/hooks/DAO/useDAOName.ts @@ -1,6 +1,6 @@ import { FractalRegistry } from '@fractal-framework/fractal-contracts'; import { useCallback, useEffect, useState } from 'react'; -import { Address, useEnsName } from 'wagmi'; +import { Address, useEnsName, usePublicClient } from 'wagmi'; import { getEventRPC } from '../../helpers'; import { useFractal } from '../../providers/App/AppProvider'; import { useEthersProvider } from '../../providers/Ethers/hooks/useEthersProvider'; @@ -26,12 +26,11 @@ export default function useDAOName({ baseContracts: { fractalRegistryContract }, } = useFractal(); const [daoRegistryName, setDAORegistryName] = useState(''); - const provider = useEthersProvider(); - const networkId = provider.network.chainId; + const { chain } = usePublicClient(); const { data: ensName } = useEnsName({ address: address as Address, - chainId: networkId, + chainId: chain.id, cacheTime: 1000 * 60 * 30, // 30 min }); const { setValue, getValue } = useLocalStorage(); @@ -102,11 +101,13 @@ export function useLazyDAOName() { if (cachedName) { return cachedName; } - // check if ens name resolves - const ensName = await provider.lookupAddress(_address).catch(() => null); - if (ensName) { - setValue(CacheKeys.DAO_NAME_PREFIX + _address, ensName, 5); - return ensName; + if (provider) { + // check if ens name resolves + const ensName = await provider.lookupAddress(_address).catch(() => null); + if (ensName) { + setValue(CacheKeys.DAO_NAME_PREFIX + _address, ensName, 5); + return ensName; + } } if (_registryName) { diff --git a/src/hooks/safe/useSafeContracts.ts b/src/hooks/safe/useSafeContracts.ts index b280c0e5dd..6ef749ad1a 100644 --- a/src/hooks/safe/useSafeContracts.ts +++ b/src/hooks/safe/useSafeContracts.ts @@ -51,6 +51,9 @@ export default function useSafeContracts() { } = useNetworkConfig(); const daoContracts = useMemo(() => { + if (!signerOrProvider || !provider) { + return; + } const multiSendContract = { asSigner: MultiSend__factory.connect(multisend, signerOrProvider), asProvider: MultiSend__factory.connect(multisend, provider), diff --git a/src/hooks/utils/useAvatar.ts b/src/hooks/utils/useAvatar.ts index 54d3245f58..d8fbd9a5f8 100644 --- a/src/hooks/utils/useAvatar.ts +++ b/src/hooks/utils/useAvatar.ts @@ -1,12 +1,10 @@ -import { useEnsAvatar } from 'wagmi'; -import { useEthersProvider } from '../../providers/Ethers/hooks/useEthersProvider'; +import { useEnsAvatar, usePublicClient } from 'wagmi'; const useAvatar = (account: string | null) => { - const provider = useEthersProvider(); - const networkId = provider.network.chainId; + const { chain } = usePublicClient(); const { data: avatarURL } = useEnsAvatar({ name: account, - chainId: networkId, + chainId: chain.id, }); return avatarURL; diff --git a/src/hooks/utils/useDisplayName.ts b/src/hooks/utils/useDisplayName.ts index 68e40c1385..628530c654 100644 --- a/src/hooks/utils/useDisplayName.ts +++ b/src/hooks/utils/useDisplayName.ts @@ -1,6 +1,5 @@ import { useEffect, useState } from 'react'; -import { Address, useEnsName } from 'wagmi'; -import { useEthersProvider } from '../../providers/Ethers/hooks/useEthersProvider'; +import { Address, useEnsName, usePublicClient } from 'wagmi'; export const createAccountSubstring = (account: string) => { return `${account.substring(0, 6)}...${account.slice(-4)}`; @@ -16,11 +15,10 @@ export const createAccountSubstring = (account: string) => { */ const useDisplayName = (account?: string | null, truncate?: boolean) => { if (truncate === undefined) truncate = true; - const provider = useEthersProvider(); - const networkId = provider.network.chainId; + const { chain } = usePublicClient(); const { data: ensName } = useEnsName({ address: account as Address, - chainId: networkId, + chainId: chain.id, cacheTime: 1000 * 60 * 30, // 30 min }); diff --git a/src/hooks/utils/useSafeTransactions.ts b/src/hooks/utils/useSafeTransactions.ts index b7dea61012..cf21818b41 100644 --- a/src/hooks/utils/useSafeTransactions.ts +++ b/src/hooks/utils/useSafeTransactions.ts @@ -41,7 +41,7 @@ export const useSafeTransactions = () => { freezeGuard?: MultisigFreezeGuard, freezeGuardData?: FreezeGuardData ) => { - if (freezeGuard && freezeGuardData) { + if (freezeGuard && freezeGuardData && provider) { return Promise.all( activities.map(async (activity, _, activityArr) => { if (activity.eventType !== ActivityEventType.Governance || !activity.transaction) { @@ -189,7 +189,7 @@ export const useSafeTransactions = () => { const parseTransactions = useCallback( async (transactions: AllTransactionsListResponse, daoAddress: string) => { - if (!transactions.results.length) { + if (!transactions.results.length || !provider) { return []; }