Skip to content

Commit

Permalink
Remove fractalRegistryContract from FractalContracts interface and re…
Browse files Browse the repository at this point in the history
…solve all downstream issues
  • Loading branch information
adamgall committed May 7, 2024
1 parent 02ed6ce commit 5d38bec
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 70 deletions.
25 changes: 10 additions & 15 deletions src/components/pages/DaoSettings/components/Metadata/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { Flex, Text, Button, Divider } from '@chakra-ui/react';
import { useState, useEffect, ChangeEventHandler } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { isHex, getAddress, encodeFunctionData } from 'viem';
import { encodeFunctionData } from 'viem';
import { SettingsSection } from '..';
import FractalRegistryAbi from '../../../../../assets/abi/FractalRegistry';
import KeyValuePairsAbi from '../../../../../assets/abi/KeyValuePairs';
import { DAO_ROUTES } from '../../../../../constants/routes';
import useSubmitProposal from '../../../../../hooks/DAO/proposal/useSubmitProposal';
Expand All @@ -25,15 +26,14 @@ export default function MetadataContainer() {
const { submitProposal } = useSubmitProposal();
const { canUserCreateProposal } = useCanUserCreateProposal();
const {
baseContracts,
node: { daoName, daoSnapshotENS, daoAddress, safe },
readOnly: {
user: { votingWeight },
},
} = useFractal();
const {
addressPrefix,
contracts: { keyValuePairs },
contracts: { keyValuePairs, fractalRegistry },
} = useNetworkConfig();

useEffect(() => {
Expand Down Expand Up @@ -65,24 +65,19 @@ export default function MetadataContainer() {
};

const handleEditDAOName = () => {
if (!baseContracts) {
return;
}
const { fractalRegistryContract } = baseContracts;
const encodedUpdateDAOName = fractalRegistryContract.asProvider.interface.encodeFunctionData(
'updateDAOName',
[name],
);
if (!isHex(encodedUpdateDAOName)) {
return;
}
const encodedUpdateDAOName = encodeFunctionData({
abi: FractalRegistryAbi,
functionName: 'updateDAOName',
args: [name],
});

const proposalData: ProposalExecuteData = {
metaData: {
title: t('Update Safe Name', { ns: 'proposalMetadata' }),
description: '',
documentationUrl: '',
},
targets: [getAddress(fractalRegistryContract.asProvider.address)],
targets: [fractalRegistry],
values: [0n],
calldatas: [encodedUpdateDAOName],
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/menus/FavoritesMenu/Favorite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface IFavorite {
address: string;
}
export function Favorite({ network, address }: IFavorite) {
const { daoRegistryName } = useDAOName({ address });
const { daoRegistryName } = useDAOName(address);
const { action } = useFractal();
const navigate = useNavigate();

Expand Down
40 changes: 27 additions & 13 deletions src/hooks/DAO/useCreateSubDAOProposal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { isHex, getAddress } from 'viem';
import { isHex, getAddress, encodeFunctionData } from 'viem';
import FractalRegistryAbi from '../../assets/abi/FractalRegistry';
import { useFractal } from '../../providers/App/AppProvider';
import { useNetworkConfig } from '../../providers/NetworkConfig/NetworkConfigProvider';
import { SafeMultisigDAO, AzoriusGovernance, AzoriusERC20DAO, AzoriusERC721DAO } from '../../types';
import { ProposalExecuteData } from '../../types/daoProposal';
import { useCanUserCreateProposal } from '../utils/useCanUserSubmitProposal';
Expand All @@ -19,6 +21,9 @@ export const useCreateSubDAOProposal = () => {
node: { daoAddress },
governance,
} = useFractal();
const {
contracts: { fractalRegistry },
} = useNetworkConfig();
const azoriusGovernance = governance as AzoriusGovernance;
const proposeDao = useCallback(
(
Expand All @@ -30,7 +35,7 @@ export const useCreateSubDAOProposal = () => {
if (!baseContracts || !daoAddress) {
return;
}
const { multiSendContract, fractalRegistryContract } = baseContracts;
const { multiSendContract } = baseContracts;

const builtSafeTx = await build(daoData, daoAddress, azoriusGovernance.votesToken?.address);
if (!builtSafeTx) {
Expand All @@ -43,18 +48,19 @@ export const useCreateSubDAOProposal = () => {
'multiSend',
[safeTx],
);
const encodedDeclareSubDAO =
fractalRegistryContract.asProvider.interface.encodeFunctionData('declareSubDAO', [
predictedSafeAddress,
]);
if (!isHex(encodedMultisend) || !isHex(encodedDeclareSubDAO)) {
return;

if (!isHex(encodedMultisend)) {
throw new Error('encodedMultisend data is not hex??');
}

const encodedDeclareSubDAO = encodeFunctionData({
abi: FractalRegistryAbi,
functionName: 'declareSubDAO',
args: [getAddress(predictedSafeAddress)],
});

const proposalData: ProposalExecuteData = {
targets: [
getAddress(multiSendContract.asProvider.address),
getAddress(fractalRegistryContract.asProvider.address),
],
targets: [getAddress(multiSendContract.asProvider.address), fractalRegistry],
values: [0n, 0n],
calldatas: [encodedMultisend, encodedDeclareSubDAO],
metaData: {
Expand All @@ -74,7 +80,15 @@ export const useCreateSubDAOProposal = () => {
};
propose();
},
[baseContracts, build, daoAddress, submitProposal, azoriusGovernance, t],
[
azoriusGovernance.votesToken?.address,
baseContracts,
build,
daoAddress,
fractalRegistry,
submitProposal,
t,
],
);

return { proposeDao, pendingCreateTx, canUserCreateProposal } as const;
Expand Down
69 changes: 30 additions & 39 deletions src/hooks/DAO/useDAOName.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
import { FractalRegistry } from '@fractal-framework/fractal-contracts';
import { useCallback, useEffect, useState } from 'react';
import { Address } from 'viem';
import { useEnsName } from 'wagmi';
import { getEventRPC } from '../../helpers';
import { useFractal } from '../../providers/App/AppProvider';
import { getAddress, getContract } from 'viem';
import { useEnsName, usePublicClient } from 'wagmi';
import FractalRegistryAbi from '../../assets/abi/FractalRegistry';
import { useEthersProvider } from '../../providers/Ethers/hooks/useEthersProvider';
import { useNetworkConfig } from '../../providers/NetworkConfig/NetworkConfigProvider';
import { createAccountSubstring } from '../utils/useDisplayName';

/**
* Gets the 'display name' for a Fractal DAO, in the following order of preference:
*
* 1. Primary ENS Name (reverse record)
* 2. Fractal name registry name
* 3. Truncated Eth address in the form 0xbFC4...7551
*/
export default function useDAOName({
address,
registryName,
}: {
address?: string;
registryName?: string | null;
}) {
const { baseContracts } = useFractal();
export default function useDAOName(address: string) {
const [daoRegistryName, setDAORegistryName] = useState<string>('');
const { chain } = useNetworkConfig();
const publicClient = usePublicClient();

const { data: ensName } = useEnsName({
address: address as Address,
address: getAddress(address),
chainId: chain.id,
});

const {
contracts: { fractalRegistry },
} = useNetworkConfig();

const getDaoName = useCallback(async () => {
if (!address || !baseContracts) {
if (!address || !publicClient) {
setDAORegistryName('');
return;
}
Expand All @@ -42,28 +31,30 @@ export default function useDAOName({
return;
}

const { fractalRegistryContract } = baseContracts;
if (!fractalRegistryContract) {
const fractalRegistryContract = getContract({
abi: FractalRegistryAbi,
address: fractalRegistry,
client: publicClient,
});

const events = await fractalRegistryContract.getEvents.FractalNameUpdated({
daoAddress: getAddress(address),
});

const latestEvent = events.pop();
if (!latestEvent) {
setDAORegistryName(createAccountSubstring(address));
return;
}
if (registryName) {
// Aka supplied from Subgraph
setDAORegistryName(registryName);
} else {
const rpc = getEventRPC<FractalRegistry>(fractalRegistryContract);
const events = await rpc.queryFilter(rpc.filters.FractalNameUpdated(address));

const latestEvent = events.pop();
if (!latestEvent) {
setDAORegistryName(createAccountSubstring(address));
return;
}

const { daoName } = latestEvent.args;
setDAORegistryName(daoName);
const { daoName } = latestEvent.args;
if (!daoName) {
setDAORegistryName(createAccountSubstring(address));
return;
}
}, [address, ensName, baseContracts, registryName]);

setDAORegistryName(daoName);
}, [address, ensName, fractalRegistry, publicClient]);

useEffect(() => {
(async () => {
Expand Down
1 change: 1 addition & 0 deletions src/hooks/utils/useMasterCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export function useMasterCopy() {
let masterCopyAddress: Address = zeroAddress;
let error;
if (baseContracts) {
// TODO after removing this moduleproxyfactorycontract from basecontracts, kill getEventRPC
const contract = getEventRPC<ModuleProxyFactory>(
baseContracts?.zodiacModuleProxyFactoryContract,
);
Expand Down
2 changes: 0 additions & 2 deletions src/types/fractal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
FractalModule,
FractalRegistry,
GnosisSafeProxyFactory,
ModuleProxyFactory,
LinearERC20Voting,
Expand Down Expand Up @@ -339,7 +338,6 @@ export interface FractalContracts {
safeSingletonContract: ContractConnection<GnosisSafeL2>;
zodiacModuleProxyFactoryContract: ContractConnection<ModuleProxyFactory>;
fractalModuleMasterCopyContract: ContractConnection<FractalModule>;
fractalRegistryContract: ContractConnection<FractalRegistry>;
multisigFreezeGuardMasterCopyContract: ContractConnection<MultisigFreezeGuard>;
azoriusFreezeGuardMasterCopyContract: ContractConnection<AzoriusFreezeGuard>;
freezeMultisigVotingMasterCopyContract: ContractConnection<MultisigFreezeVoting>;
Expand Down

0 comments on commit 5d38bec

Please sign in to comment.