From d76ec5bd21c53e6ee61b23341ab1030213e2954d Mon Sep 17 00:00:00 2001 From: David Colon <38386583+Da-Colon@users.noreply.github.com> Date: Thu, 12 Dec 2024 01:05:53 -0500 Subject: [PATCH] Refactor loadGovernanceContracts to accept daoModules parameter --- .../DAO/loaders/useGovernanceContracts.ts | 205 +++++++++--------- 1 file changed, 103 insertions(+), 102 deletions(-) diff --git a/src/hooks/DAO/loaders/useGovernanceContracts.ts b/src/hooks/DAO/loaders/useGovernanceContracts.ts index 0463eaf1b..4c2ae912d 100644 --- a/src/hooks/DAO/loaders/useGovernanceContracts.ts +++ b/src/hooks/DAO/loaders/useGovernanceContracts.ts @@ -6,6 +6,7 @@ import LockReleaseAbi from '../../../assets/abi/LockRelease'; import { useFractal } from '../../../providers/App/AppProvider'; import { GovernanceContractAction } from '../../../providers/App/governanceContracts/action'; import { useDaoInfoStore } from '../../../store/daoInfo/useDaoInfoStore'; +import { DecentModule } from '../../../types'; import { getAzoriusModuleFromModules } from '../../../utils'; import { useAddressContractType } from '../../utils/useAddressContractType'; import useVotingStrategyAddress from '../../utils/useVotingStrategiesAddresses'; @@ -23,115 +24,115 @@ export const useGovernanceContracts = () => { const safeAddress = safe?.address; - const loadGovernanceContracts = useCallback(async () => { - if (!modules) { - throw new Error('DAO modules not ready'); - } - const azoriusModule = getAzoriusModuleFromModules(modules); - - const votingStrategies = await getVotingStrategies(); + const loadGovernanceContracts = useCallback( + async (daoModules: DecentModule[]) => { + const azoriusModule = getAzoriusModuleFromModules(daoModules); - if (!azoriusModule || !votingStrategies) { - action.dispatch({ - type: GovernanceContractAction.SET_GOVERNANCE_CONTRACT_ADDRESSES, - payload: {}, - }); - return; - } - - if (!publicClient) { - throw new Error('Public Client is not set!'); - } + const votingStrategies = await getVotingStrategies(); - let linearVotingErc20Address: Address | undefined; - let linearVotingErc721Address: Address | undefined; - let linearVotingErc20WithHatsWhitelistingAddress: Address | undefined; - let linearVotingErc721WithHatsWhitelistingAddress: Address | undefined; - let votesTokenAddress: Address | undefined; - let underlyingTokenAddress: Address | undefined; - let lockReleaseAddress: Address | undefined; - - const setGovTokenAddress = async (erc20VotingStrategyAddress: Address) => { - if (votesTokenAddress) { + if (!azoriusModule || !votingStrategies) { + action.dispatch({ + type: GovernanceContractAction.SET_GOVERNANCE_CONTRACT_ADDRESSES, + payload: {}, + }); return; } - const ozLinearVotingContract = getContract({ - abi: abis.LinearERC20Voting, - address: erc20VotingStrategyAddress, - client: publicClient, - }); - - const govTokenAddress = await ozLinearVotingContract.read.governanceToken(); - // govTokenAddress might be either - // - a valid VotesERC20 contract - // - a valid LockRelease contract - // - or none of these which is against business logic - - const { isVotesErc20 } = await getAddressContractType(govTokenAddress); - - if (isVotesErc20) { - votesTokenAddress = govTokenAddress; - } else { - const possibleLockRelease = getContract({ - address: govTokenAddress, - abi: LockReleaseAbi, - client: { public: publicClient }, - }); - try { - const lockedTokenAddress = await possibleLockRelease.read.token(); - lockReleaseAddress = govTokenAddress; - votesTokenAddress = lockedTokenAddress; - } catch { - throw new Error('Unknown governance token type'); - } + if (!publicClient) { + throw new Error('Public Client is not set!'); } - }; - - await Promise.all( - votingStrategies.map(async votingStrategy => { - const { - strategyAddress, - isLinearVotingErc20, - isLinearVotingErc721, - isLinearVotingErc20WithHatsProposalCreation, - isLinearVotingErc721WithHatsProposalCreation, - } = votingStrategy; - if (isLinearVotingErc20) { - linearVotingErc20Address = strategyAddress; - await setGovTokenAddress(strategyAddress); - } else if (isLinearVotingErc721) { - linearVotingErc721Address = strategyAddress; - } else if (isLinearVotingErc20WithHatsProposalCreation) { - linearVotingErc20WithHatsWhitelistingAddress = strategyAddress; - await setGovTokenAddress(strategyAddress); - } else if (isLinearVotingErc721WithHatsProposalCreation) { - linearVotingErc721WithHatsWhitelistingAddress = strategyAddress; + + let linearVotingErc20Address: Address | undefined; + let linearVotingErc721Address: Address | undefined; + let linearVotingErc20WithHatsWhitelistingAddress: Address | undefined; + let linearVotingErc721WithHatsWhitelistingAddress: Address | undefined; + let votesTokenAddress: Address | undefined; + let underlyingTokenAddress: Address | undefined; + let lockReleaseAddress: Address | undefined; + + const setGovTokenAddress = async (erc20VotingStrategyAddress: Address) => { + if (votesTokenAddress) { + return; } - }), - ); + const ozLinearVotingContract = getContract({ + abi: abis.LinearERC20Voting, + address: erc20VotingStrategyAddress, + client: publicClient, + }); - if ( - linearVotingErc20Address || - linearVotingErc20WithHatsWhitelistingAddress || - linearVotingErc721Address || - linearVotingErc721WithHatsWhitelistingAddress - ) { - action.dispatch({ - type: GovernanceContractAction.SET_GOVERNANCE_CONTRACT_ADDRESSES, - payload: { - linearVotingErc20Address, - linearVotingErc20WithHatsWhitelistingAddress, - linearVotingErc721Address, - linearVotingErc721WithHatsWhitelistingAddress, - votesTokenAddress, - underlyingTokenAddress, - lockReleaseAddress, - moduleAzoriusAddress: azoriusModule.moduleAddress, - }, - }); - } - }, [action, modules, getVotingStrategies, publicClient, getAddressContractType]); + const govTokenAddress = await ozLinearVotingContract.read.governanceToken(); + // govTokenAddress might be either + // - a valid VotesERC20 contract + // - a valid LockRelease contract + // - or none of these which is against business logic + + const { isVotesErc20 } = await getAddressContractType(govTokenAddress); + + if (isVotesErc20) { + votesTokenAddress = govTokenAddress; + } else { + const possibleLockRelease = getContract({ + address: govTokenAddress, + abi: LockReleaseAbi, + client: { public: publicClient }, + }); + + try { + const lockedTokenAddress = await possibleLockRelease.read.token(); + lockReleaseAddress = govTokenAddress; + votesTokenAddress = lockedTokenAddress; + } catch { + throw new Error('Unknown governance token type'); + } + } + }; + + await Promise.all( + votingStrategies.map(async votingStrategy => { + const { + strategyAddress, + isLinearVotingErc20, + isLinearVotingErc721, + isLinearVotingErc20WithHatsProposalCreation, + isLinearVotingErc721WithHatsProposalCreation, + } = votingStrategy; + if (isLinearVotingErc20) { + linearVotingErc20Address = strategyAddress; + await setGovTokenAddress(strategyAddress); + } else if (isLinearVotingErc721) { + linearVotingErc721Address = strategyAddress; + } else if (isLinearVotingErc20WithHatsProposalCreation) { + linearVotingErc20WithHatsWhitelistingAddress = strategyAddress; + await setGovTokenAddress(strategyAddress); + } else if (isLinearVotingErc721WithHatsProposalCreation) { + linearVotingErc721WithHatsWhitelistingAddress = strategyAddress; + } + }), + ); + + if ( + linearVotingErc20Address || + linearVotingErc20WithHatsWhitelistingAddress || + linearVotingErc721Address || + linearVotingErc721WithHatsWhitelistingAddress + ) { + action.dispatch({ + type: GovernanceContractAction.SET_GOVERNANCE_CONTRACT_ADDRESSES, + payload: { + linearVotingErc20Address, + linearVotingErc20WithHatsWhitelistingAddress, + linearVotingErc721Address, + linearVotingErc721WithHatsWhitelistingAddress, + votesTokenAddress, + underlyingTokenAddress, + lockReleaseAddress, + moduleAzoriusAddress: azoriusModule.moduleAddress, + }, + }); + } + }, + [action, getVotingStrategies, publicClient, getAddressContractType], + ); useEffect(() => { if ( @@ -139,7 +140,7 @@ export const useGovernanceContracts = () => { currentValidAddress.current !== safeAddress && modules !== null ) { - loadGovernanceContracts(); + loadGovernanceContracts(modules); currentValidAddress.current = safeAddress; } if (!safeAddress) {