Skip to content

Commit

Permalink
Remove ModuleProxyFactory from FractalContracts and BaseContracts and…
Browse files Browse the repository at this point in the history
… then work through it all
  • Loading branch information
adamgall committed May 6, 2024
1 parent 0908010 commit 7ed9eda
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 127 deletions.
8 changes: 0 additions & 8 deletions src/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
encodeFunctionData,
Abi,
} from 'viem';
import { ContractConnection } from '../types';
import { MetaTransaction, SafePostTransaction, SafeTransaction } from '../types/transaction';

export interface SafeSignature {
Expand Down Expand Up @@ -236,10 +235,3 @@ const encodeMetaTransaction = (tx: MetaTransaction): string => {
export const encodeMultiSend = (txs: MetaTransaction[]): string => {
return '0x' + txs.map(tx => encodeMetaTransaction(tx)).join('');
};

/**
* TODO: Remove getEventRPC usage as whole
*/
export function getEventRPC<T>(connection: ContractConnection<T>): T {
return connection.asProvider;
}
5 changes: 3 additions & 2 deletions src/hooks/DAO/useBuildDAOTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const useBuildDAOTx = () => {
keyValuePairs,
fractalRegistry,
safeFactory,
zodiacModuleProxyFactory,
},
} = useNetworkConfig();

Expand Down Expand Up @@ -54,7 +55,6 @@ const useBuildDAOTx = () => {
linearVotingMasterCopyContract,
linearVotingERC721MasterCopyContract,
fractalAzoriusMasterCopyContract,
zodiacModuleProxyFactoryContract,
fractalModuleMasterCopyContract,
multisigFreezeGuardMasterCopyContract,
azoriusFreezeGuardMasterCopyContract,
Expand Down Expand Up @@ -100,7 +100,6 @@ const useBuildDAOTx = () => {
freezeERC20VotingMasterCopyContract: freezeERC20VotingMasterCopyContract.asSigner,
freezeERC721VotingMasterCopyContract: freezeERC721VotingMasterCopyContract.asSigner,
freezeMultisigVotingMasterCopyContract: freezeMultisigVotingMasterCopyContract.asSigner,
zodiacModuleProxyFactoryContract: zodiacModuleProxyFactoryContract.asSigner,
};

const txBuilderFactory = new TxBuilderFactory(
Expand All @@ -115,6 +114,7 @@ const useBuildDAOTx = () => {
keyValuePairs,
fractalRegistry,
safeFactory,
zodiacModuleProxyFactory,
parentAddress,
parentTokenAddress,
);
Expand Down Expand Up @@ -167,6 +167,7 @@ const useBuildDAOTx = () => {
keyValuePairs,
fractalRegistry,
safeFactory,
zodiacModuleProxyFactory,
],
);

Expand Down
5 changes: 3 additions & 2 deletions src/hooks/DAO/useDeployAzorius.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const useDeployAzorius = () => {
keyValuePairs,
fractalRegistry,
safeFactory,
zodiacModuleProxyFactory,
},
addressPrefix,
} = useNetworkConfig();
Expand Down Expand Up @@ -57,7 +58,6 @@ const useDeployAzorius = () => {
linearVotingMasterCopyContract,
linearVotingERC721MasterCopyContract,
fractalAzoriusMasterCopyContract,
zodiacModuleProxyFactoryContract,
fractalModuleMasterCopyContract,
multisigFreezeGuardMasterCopyContract,
azoriusFreezeGuardMasterCopyContract,
Expand All @@ -84,7 +84,6 @@ const useDeployAzorius = () => {
freezeERC20VotingMasterCopyContract: freezeERC20VotingMasterCopyContract.asProvider,
freezeERC721VotingMasterCopyContract: freezeERC721VotingMasterCopyContract.asProvider,
freezeMultisigVotingMasterCopyContract: freezeMultisigVotingMasterCopyContract.asProvider,
zodiacModuleProxyFactoryContract: zodiacModuleProxyFactoryContract.asProvider,
};

const txBuilderFactory = new TxBuilderFactory(
Expand All @@ -99,6 +98,7 @@ const useDeployAzorius = () => {
keyValuePairs,
fractalRegistry,
safeFactory,
zodiacModuleProxyFactory,
undefined,
undefined,
);
Expand Down Expand Up @@ -162,6 +162,7 @@ const useDeployAzorius = () => {
keyValuePairs,
fractalRegistry,
safeFactory,
zodiacModuleProxyFactory,
],
);

Expand Down
80 changes: 52 additions & 28 deletions src/hooks/utils/useMasterCopy.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { ModuleProxyFactory } from '@fractal-framework/fractal-contracts';
import { Contract } from 'ethers';
import { useCallback } from 'react';
import { Address, zeroAddress } from 'viem';
import { getEventRPC } from '../../helpers';
import { Address, getContract, zeroAddress } from 'viem';
import { usePublicClient } from 'wagmi';
import ModuleProxyFactoryAbi from '../../assets/abi/ModuleProxyFactory';
import { useFractal } from '../../providers/App/AppProvider';
import { useNetworkConfig } from '../../providers/NetworkConfig/NetworkConfigProvider';
import { CacheExpiry, CacheKeys } from './cache/cacheDefaults';
import { useLocalStorage } from './cache/useLocalStorage';

export function useMasterCopy() {
const { getValue, setValue } = useLocalStorage();
const { baseContracts } = useFractal();
const {
contracts: { zodiacModuleProxyFactory },
} = useNetworkConfig();
const publicClient = usePublicClient();

const isOzLinearVoting = useCallback(
(masterCopyAddress: Address) =>
Expand Down Expand Up @@ -49,39 +53,60 @@ export function useMasterCopy() {
);

const getMasterCopyAddress = useCallback(
async function (contract: Contract, proxyAddress: Address): Promise<[Address, string | null]> {
async function (proxyAddress: Address): Promise<readonly [Address, string | null]> {
if (!publicClient) {
return [zeroAddress, null] as const;
}

const cachedValue = getValue(CacheKeys.MASTER_COPY_PREFIX + proxyAddress);
if (cachedValue) return [cachedValue, null] as const;
if (cachedValue) {
return [cachedValue, null] as const;
}

const filter = contract.filters.ModuleProxyCreation(proxyAddress, null);
return contract.queryFilter(filter).then(proxiesCreated => {
// @dev to prevent redundant queries, cache the master copy address as AddressZero if no proxies were created
if (proxiesCreated.length === 0) {
setValue(CacheKeys.MASTER_COPY_PREFIX + proxyAddress, zeroAddress, CacheExpiry.ONE_WEEK);
return [zeroAddress, 'No proxies created'] as const;
}
const masterCopyAddress = proxiesCreated[0].args!.masterCopy;
setValue(CacheKeys.MASTER_COPY_PREFIX + proxyAddress, masterCopyAddress);
return [masterCopyAddress, null] as const;
const moduleProxyFactoryContract = getContract({
abi: ModuleProxyFactoryAbi,
address: zodiacModuleProxyFactory,
client: publicClient,
});

return moduleProxyFactoryContract.getEvents
.ModuleProxyCreation({ proxy: proxyAddress })
.then(proxiesCreated => {
// @dev to prevent redundant queries, cache the master copy address as AddressZero if no proxies were created
if (proxiesCreated.length === 0) {
setValue(
CacheKeys.MASTER_COPY_PREFIX + proxyAddress,
zeroAddress,
CacheExpiry.ONE_WEEK,
);
return [zeroAddress, 'No proxies created'] as const;
}

const masterCopyAddress = proxiesCreated[0].args.masterCopy;
if (!masterCopyAddress) {
return [zeroAddress, 'No master copy address'] as const;
}

setValue(CacheKeys.MASTER_COPY_PREFIX + proxyAddress, masterCopyAddress);
return [masterCopyAddress, null] as const;
})
.catch(() => {
return [zeroAddress, 'error'] as const;
});
},
[getValue, setValue],
[getValue, publicClient, setValue, zodiacModuleProxyFactory],
);

const getZodiacModuleProxyMasterCopyData = useCallback(
async function (proxyAddress: Address) {
let masterCopyAddress: Address = zeroAddress;
let error;
if (baseContracts) {
// TODO after removing this moduleproxyfactorycontract from basecontracts, kill getEventRPC
const contract = getEventRPC<ModuleProxyFactory>(
baseContracts?.zodiacModuleProxyFactoryContract,
);
[masterCopyAddress, error] = await getMasterCopyAddress(contract, proxyAddress);
if (error) {
console.error(error);
}

[masterCopyAddress, error] = await getMasterCopyAddress(proxyAddress);
if (error) {
console.error(error);
}

return {
address: masterCopyAddress,
isOzLinearVoting: isOzLinearVoting(masterCopyAddress),
Expand All @@ -96,13 +121,12 @@ export function useMasterCopy() {
[
getMasterCopyAddress,
isAzorius,
isFractalModule,
isERC721FreezeVoting,
isFractalModule,
isMultisigFreezeGuard,
isMultisigFreezeVoting,
isOzLinearVoting,
isOzLinearVotingERC721,
baseContracts,
],
);

Expand Down
41 changes: 25 additions & 16 deletions src/models/AzoriusTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
PublicClient,
getContract,
} from 'viem';
import ModuleProxyFactoryAbi from '../assets/abi/ModuleProxyFactory';
import VotesERC20Abi from '../assets/abi/VotesERC20';
import VotesERC20WrapperAbi from '../assets/abi/VotesERC20Wrapper';
import { GnosisSafeL2 } from '../assets/typechain-types/usul/@gnosis.pm/safe-contracts/contracts';
Expand Down Expand Up @@ -59,6 +60,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {

private votesERC20WrapperMasterCopyAddress: string;
private votesERC20MasterCopyAddress: string;
private moduleProxyFactoryAddress: Address;

private tokenNonce: bigint;
private strategyNonce: bigint;
Expand All @@ -74,6 +76,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
safeContract: GnosisSafeL2,
votesERC20WrapperMasterCopyAddress: string,
votesERC20MasterCopyAddress: string,
moduleProxyFactoryAddress: Address,
parentAddress?: Address,
parentTokenAddress?: Address,
) {
Expand All @@ -96,6 +99,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {

this.votesERC20WrapperMasterCopyAddress = votesERC20WrapperMasterCopyAddress;
this.votesERC20MasterCopyAddress = votesERC20MasterCopyAddress;
this.moduleProxyFactoryAddress = moduleProxyFactoryAddress;

if (daoData.votingStrategyType === VotingStrategyType.LINEAR_ERC20) {
daoData = daoData as AzoriusERC20DAO;
Expand Down Expand Up @@ -191,8 +195,9 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
}

public buildCreateTokenTx(): SafeTransaction {
return buildContractCall(
this.baseContracts.zodiacModuleProxyFactoryContract,
return buildContractCallViem(
ModuleProxyFactoryAbi,
this.moduleProxyFactoryAddress,
'deployModule',
[this.votesERC20MasterCopyAddress, this.encodedSetupTokenData, this.tokenNonce],
0,
Expand All @@ -203,8 +208,9 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
public buildDeployStrategyTx(): SafeTransaction {
const daoData = this.daoData as AzoriusGovernanceDAO;

return buildContractCall(
this.baseContracts.zodiacModuleProxyFactoryContract,
return buildContractCallViem(
ModuleProxyFactoryAbi,
this.moduleProxyFactoryAddress,
'deployModule',
[
daoData.votingStrategyType === VotingStrategyType.LINEAR_ERC20
Expand All @@ -219,8 +225,9 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
}

public buildDeployAzoriusTx(): SafeTransaction {
return buildContractCall(
this.baseContracts.zodiacModuleProxyFactoryContract,
return buildContractCallViem(
ModuleProxyFactoryAbi,
this.moduleProxyFactoryAddress,
'deployModule',
[
this.azoriusContracts!.fractalAzoriusMasterCopyContract.address,
Expand All @@ -233,8 +240,9 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
}

public buildDeployTokenClaim() {
return buildContractCall(
this.baseContracts.zodiacModuleProxyFactoryContract,
return buildContractCallViem(
ModuleProxyFactoryAbi,
this.moduleProxyFactoryAddress,
'deployModule',
[
this.azoriusContracts!.claimingMasterCopyContract.address,
Expand Down Expand Up @@ -263,8 +271,9 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
}

public buildCreateTokenWrapperTx(): SafeTransaction {
return buildContractCall(
this.baseContracts.zodiacModuleProxyFactoryContract,
return buildContractCallViem(
ModuleProxyFactoryAbi,
this.moduleProxyFactoryAddress,
'deployModule',
[this.votesERC20WrapperMasterCopyAddress, this.encodedSetupERC20WrapperData, this.tokenNonce],
0,
Expand Down Expand Up @@ -300,7 +309,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
const tokenSalt = generateSalt(this.encodedSetupERC20WrapperData!, this.tokenNonce);

this.predictedTokenAddress = getCreate2Address({
from: getAddress(this.baseContracts.zodiacModuleProxyFactoryContract.address),
from: this.moduleProxyFactoryAddress,
salt: tokenSalt,
bytecodeHash: keccak256(encodePacked(['bytes'], [tokenByteCodeLinear])),
});
Expand Down Expand Up @@ -368,7 +377,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
const tokenSalt = generateSalt(this.encodedSetupTokenData!, this.tokenNonce);

this.predictedTokenAddress = getCreate2Address({
from: getAddress(this.baseContracts.zodiacModuleProxyFactoryContract.address),
from: this.moduleProxyFactoryAddress,
salt: tokenSalt,
bytecodeHash: keccak256(encodePacked(['bytes'], [tokenByteCodeLinear])),
});
Expand Down Expand Up @@ -407,7 +416,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
const tokenSalt = generateSalt(this.encodedSetupTokenClaimData!, this.claimNonce);

this.predictedTokenClaimAddress = getCreate2Address({
from: getAddress(this.baseContracts.zodiacModuleProxyFactoryContract.address),
from: this.moduleProxyFactoryAddress,
salt: tokenSalt,
bytecodeHash: keccak256(encodePacked(['bytes'], [tokenByteCodeLinear])),
});
Expand Down Expand Up @@ -460,7 +469,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
this.encodedStrategySetupData = encodedStrategySetupData;

this.predictedStrategyAddress = getCreate2Address({
from: getAddress(this.baseContracts.zodiacModuleProxyFactoryContract.address),
from: this.moduleProxyFactoryAddress,
salt: strategySalt,
bytecodeHash: keccak256(encodePacked(['bytes'], [strategyByteCodeLinear])),
});
Expand Down Expand Up @@ -507,7 +516,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
this.encodedStrategySetupData = encodedStrategySetupData;

this.predictedStrategyAddress = getCreate2Address({
from: getAddress(this.baseContracts.zodiacModuleProxyFactoryContract.address),
from: this.moduleProxyFactoryAddress,
salt: strategySalt,
bytecodeHash: keccak256(encodePacked(['bytes'], [strategyByteCodeLinear])),
});
Expand Down Expand Up @@ -547,7 +556,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {

this.encodedSetupAzoriusData = encodedSetupAzoriusData;
this.predictedAzoriusAddress = getCreate2Address({
from: getAddress(this.baseContracts.zodiacModuleProxyFactoryContract.address),
from: this.moduleProxyFactoryAddress,
salt: azoriusSalt,
bytecodeHash: keccak256(encodePacked(['bytes'], [azoriusByteCodeLinear])),
});
Expand Down
5 changes: 4 additions & 1 deletion src/models/DaoTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class DaoTxBuilder extends BaseTxBuilder {

private readonly keyValuePairsAddress: string;
private readonly fractalRegistryAddress: string;
private readonly moduleProxyFactoryAddress: string;

constructor(
signerOrProvider: ethers.Signer | any,
Expand All @@ -49,6 +50,7 @@ export class DaoTxBuilder extends BaseTxBuilder {
txBuilderFactory: TxBuilderFactory,
keyValuePairsAddress: string,
fractalRegistryAddress: string,
moduleProxyFactoryAddress: string,
parentAddress?: string,
parentTokenAddress?: string,
parentStrategyType?: VotingStrategyType,
Expand All @@ -73,6 +75,7 @@ export class DaoTxBuilder extends BaseTxBuilder {

this.keyValuePairsAddress = keyValuePairsAddress;
this.fractalRegistryAddress = fractalRegistryAddress;
this.moduleProxyFactoryAddress = moduleProxyFactoryAddress;

// Prep fractal module txs for setting up subDAOs
this.setFractalModuleTxs();
Expand Down Expand Up @@ -216,7 +219,7 @@ export class DaoTxBuilder extends BaseTxBuilder {
private setFractalModuleTxs(): void {
const { enableFractalModuleTx, deployFractalModuleTx }: FractalModuleData = fractalModuleData(
this.baseContracts.fractalModuleMasterCopyContract,
this.baseContracts.zodiacModuleProxyFactoryContract,
getAddress(this.moduleProxyFactoryAddress),
this.safeContract!,
this.saltNum,
this.parentAddress,
Expand Down
Loading

0 comments on commit 7ed9eda

Please sign in to comment.