Skip to content

Commit

Permalink
Reduce usages of useEthersProvider, fix lint errors, add more conditi…
Browse files Browse the repository at this point in the history
…onal logic whenever applicable to check that provider is not undefined
  • Loading branch information
mudrila committed Mar 7, 2024
1 parent 4cdd6f1 commit 5213594
Show file tree
Hide file tree
Showing 22 changed files with 178 additions and 169 deletions.
215 changes: 114 additions & 101 deletions src/components/DaoCreator/hooks/usePrepareFormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,43 @@ export function usePrepareFormData() {
const prepareFreezeGuardData = useCallback(
async (
freezeGuard: DAOFreezeGuardConfig<BigNumberValuePair>
): Promise<DAOFreezeGuardConfig> => {
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<DAOFreezeGuardConfig | undefined> => {
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]
Expand All @@ -77,15 +81,17 @@ export function usePrepareFormData() {
return inputValue;
})
);
let freezeGuardData: Partial<DAOFreezeGuardConfig> = {};
let freezeGuardData;
if (freezeGuard) {
freezeGuardData = await prepareFreezeGuardData(freezeGuard);
}
return {
trustedAddresses: resolvedAddresses,
...freezeGuardData,
...rest,
};
if (freezeGuardData) {
return {
trustedAddresses: resolvedAddresses,
...freezeGuardData,
...rest,
};
}
},
[signer, prepareFreezeGuardData]
);
Expand All @@ -103,44 +109,48 @@ export function usePrepareFormData() {
tokenImportAddress,
tokenCreationType,
...rest
}: AzoriusERC20DAO<BigNumberValuePair> & FreezeGuardConfigParam): Promise<AzoriusERC20DAO> => {
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<DAOFreezeGuardConfig> = {};
if (freezeGuard) {
freezeGuardData = await prepareFreezeGuardData(freezeGuard);
}
const isTokenImported =
tokenCreationType === TokenCreationType.IMPORTED && !!tokenImportAddress;
let isVotesToken = false;
if (isTokenImported) {
isVotesToken = await checkVotesToken(tokenImportAddress);
}: AzoriusERC20DAO<BigNumberValuePair> & 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]
);
Expand All @@ -155,39 +165,42 @@ export function usePrepareFormData() {
nfts,
quorumThreshold,
...rest
}: AzoriusERC721DAO<BigNumberValuePair> &
FreezeGuardConfigParam): Promise<AzoriusERC721DAO> => {
let freezeGuardData: Partial<DAOFreezeGuardConfig> = {};
if (freezeGuard) {
freezeGuardData = await prepareFreezeGuardData(freezeGuard);
}
}: AzoriusERC721DAO<BigNumberValuePair> & 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]
);
Expand Down
6 changes: 4 additions & 2 deletions src/components/pages/DaoDashboard/Info/InfoGovernance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -25,7 +25,9 @@ export function InfoGovernance() {
useEffect(() => {
const setTimelockInfo = async () => {
const formatBlocks = async (blocks: number): Promise<string | undefined> => {
return getTimeDuration(await blocksToSeconds(blocks, provider));
if (provider) {
return getTimeDuration(await blocksToSeconds(blocks, provider));
}
};
if (freezeGuardType == FreezeGuardType.MULTISIG) {
if (freezeGuardContract) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -37,11 +36,10 @@ function RemoveSignerModal({
const [prevSigner, setPrevSigner] = useState<string>('');
const [threshold, setThreshold] = useState<number>(currentThreshold);
const [nonce, setNonce] = useState<number | undefined>(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']);
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/modals/DelegateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
7 changes: 2 additions & 5 deletions src/components/ui/modals/ForkProposalTemplateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/modals/UnwrapToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/modals/WrapToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
11 changes: 8 additions & 3 deletions src/components/ui/proposal/useProposalCountdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 5213594

Please sign in to comment.