Skip to content

Commit

Permalink
Remove ERC20Claim typechain instances, also change state so that we o…
Browse files Browse the repository at this point in the history
…nly store an address, not a whole contract object
  • Loading branch information
adamgall committed May 7, 2024
1 parent d35a115 commit ea82775
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 65 deletions.
31 changes: 23 additions & 8 deletions src/components/pages/DaoDashboard/ERC20Claim.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Alert, AlertTitle, Button, Flex, Text } from '@chakra-ui/react';
import { Alert as AlertIcon } from '@decent-org/fractal-ui';
import { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { getAddress, getContract } from 'viem';
import { usePublicClient, useWalletClient } from 'wagmi';
import ERC20ClaimAbi from '../../../assets/abi/ERC20Claim';
import { useTransaction } from '../../../hooks/utils/useTransaction';
import { useFractal } from '../../../providers/App/AppProvider';
import { AzoriusGovernance } from '../../../types';
Expand All @@ -14,25 +17,32 @@ export function ERCO20Claim() {
readOnly: { user },
} = useFractal();
const account = user.address;
const { tokenClaimContract, type } = governance;
const { tokenClaimContractAddress, type } = governance;
const { t } = useTranslation(['dashboard', 'transaction']);
const [contractCall, pending] = useTransaction();
const [, pending, contractCallViem] = useTransaction();
const azoriusGovernance = governance as AzoriusGovernance;
const publicClient = usePublicClient();
const { data: walletClient } = useWalletClient();

const loadClaim = useCallback(async () => {
if (!tokenClaimContract || !type || !account) {
if (!tokenClaimContractAddress || !type || !account || !publicClient) {
return;
}
const claimableAmount = (await tokenClaimContract.getClaimAmount(account)).toBigInt();
const tokenClaimContract = getContract({
abi: ERC20ClaimAbi,
address: tokenClaimContractAddress,
client: publicClient,
});
const claimableAmount = await tokenClaimContract.read.getClaimAmount([getAddress(account)]);
setUserClaimable(claimableAmount);
}, [tokenClaimContract, type, account]);
}, [account, publicClient, tokenClaimContractAddress, type]);

useEffect(() => {
loadClaim();
}, [loadClaim]);

const claimToken = async () => {
if (!tokenClaimContract || !azoriusGovernance.votesToken || !account) {
if (!tokenClaimContractAddress || !azoriusGovernance.votesToken || !account || !walletClient) {
return;
}
const claimableString = formatCoin(
Expand All @@ -41,8 +51,13 @@ export function ERCO20Claim() {
azoriusGovernance.votesToken.decimals,
azoriusGovernance.votesToken.symbol,
);
contractCall({
contractFn: () => tokenClaimContract.claimTokens(account),
const tokenClaimContract = getContract({
abi: ERC20ClaimAbi,
address: tokenClaimContractAddress,
client: walletClient,
});
contractCallViem({
contractFn: () => tokenClaimContract.write.claimTokens([getAddress(account)]),
pendingMessage: t('pendingTokenClaim', {
symbol: azoriusGovernance.votesToken.symbol,
ns: 'transaction',
Expand Down
28 changes: 15 additions & 13 deletions src/hooks/DAO/loaders/governance/useERC20Claim.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useEffect, useCallback, useRef } from 'react';
import { getContract, getAddress } from 'viem';
import { usePublicClient } from 'wagmi';
import ERC20ClaimAbi from '../../../../assets/abi/ERC20Claim';
import VotesERC20Abi from '../../../../assets/abi/VotesERC20';
import { useFractal } from '../../../../providers/App/AppProvider';
import { FractalGovernanceAction } from '../../../../providers/App/governance/action';
import useSafeContracts from '../../../safe/useSafeContracts';
// get list of approvals; approval [0] should be token claim
// query using attach = masterTokenClaim.attach(approval[0]).queryFilter()
// check if module is tokenClaim;
Expand All @@ -16,14 +16,12 @@ export function useERC20Claim() {
governanceContracts: { votesTokenContractAddress },
action,
} = useFractal();
const baseContracts = useSafeContracts();
const publicClient = usePublicClient();

const loadTokenClaimContract = useCallback(async () => {
if (!baseContracts || !votesTokenContractAddress || !publicClient) {
if (!votesTokenContractAddress || !publicClient) {
return;
}
const { claimingMasterCopyContract } = baseContracts;

const votesTokenContract = getContract({
abi: VotesERC20Abi,
Expand All @@ -39,23 +37,27 @@ export function useERC20Claim() {
return;
}

const possibleTokenClaimContract = claimingMasterCopyContract.asProvider.attach(
getAddress(approvals[0].args.spender),
);
const tokenClaimFilter = possibleTokenClaimContract.filters.ERC20ClaimCreated();
const tokenClaimArray = await possibleTokenClaimContract
.queryFilter(tokenClaimFilter)
const possibleTokenClaimContract = getContract({
abi: ERC20ClaimAbi,
address: getAddress(approvals[0].args.spender),
client: publicClient,
});

const tokenClaimArray = await possibleTokenClaimContract.getEvents
.ERC20ClaimCreated()
.catch(() => []);

if (!tokenClaimArray.length || tokenClaimArray[0].args[1] === votesTokenContractAddress) {
const childToken = tokenClaimArray[0].args.childToken;

if (!tokenClaimArray.length || !childToken || childToken === votesTokenContractAddress) {
return;
}
// action to governance
action.dispatch({
type: FractalGovernanceAction.SET_CLAIMING_CONTRACT,
payload: possibleTokenClaimContract,
payload: getAddress(approvals[0].args.spender),
});
}, [action, baseContracts, publicClient, votesTokenContractAddress]);
}, [action, publicClient, votesTokenContractAddress]);

const loadKey = useRef<string>();

Expand Down
8 changes: 4 additions & 4 deletions src/hooks/DAO/useBuildDAOTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const useBuildDAOTx = () => {
safe: safeSingleton,
zodiacModuleProxyFactory,
multisend: multiSendCallOnly,
claimingMasterCopy: erc20ClaimMasterCopy,
},
} = useNetworkConfig();

Expand Down Expand Up @@ -61,7 +62,6 @@ const useBuildDAOTx = () => {
freezeMultisigVotingMasterCopyContract,
freezeERC20VotingMasterCopyContract,
freezeERC721VotingMasterCopyContract,
claimingMasterCopyContract,
} = baseContracts;

if (
Expand All @@ -77,8 +77,7 @@ const useBuildDAOTx = () => {
if (
!fractalAzoriusMasterCopyContract ||
!linearVotingMasterCopyContract ||
!azoriusFreezeGuardMasterCopyContract ||
!claimingMasterCopyContract
!azoriusFreezeGuardMasterCopyContract
) {
return;
}
Expand All @@ -88,7 +87,6 @@ const useBuildDAOTx = () => {
linearVotingMasterCopyContract: linearVotingMasterCopyContract.asSigner,
linearVotingERC721MasterCopyContract: linearVotingERC721MasterCopyContract.asSigner,
azoriusFreezeGuardMasterCopyContract: azoriusFreezeGuardMasterCopyContract.asSigner,
claimingMasterCopyContract: claimingMasterCopyContract.asSigner,
};
}

Expand All @@ -115,6 +113,7 @@ const useBuildDAOTx = () => {
safeSingleton,
zodiacModuleProxyFactory,
multiSendCallOnly,
erc20ClaimMasterCopy,
parentAddress,
parentTokenAddress,
);
Expand Down Expand Up @@ -170,6 +169,7 @@ const useBuildDAOTx = () => {
safeSingleton,
zodiacModuleProxyFactory,
multiSendCallOnly,
erc20ClaimMasterCopy,
],
);

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 @@ -34,6 +34,7 @@ const useDeployAzorius = () => {
safe: safeSingleton,
zodiacModuleProxyFactory,
multisend: multiSendCallOnly,
claimingMasterCopy: erc20ClaimMasterCopy,
},
addressPrefix,
} = useNetworkConfig();
Expand Down Expand Up @@ -66,7 +67,6 @@ const useDeployAzorius = () => {
freezeMultisigVotingMasterCopyContract,
freezeERC20VotingMasterCopyContract,
freezeERC721VotingMasterCopyContract,
claimingMasterCopyContract,
} = baseContracts;
let azoriusContracts: AzoriusContracts;

Expand All @@ -75,7 +75,6 @@ const useDeployAzorius = () => {
linearVotingMasterCopyContract: linearVotingMasterCopyContract.asProvider,
linearVotingERC721MasterCopyContract: linearVotingERC721MasterCopyContract.asProvider,
azoriusFreezeGuardMasterCopyContract: azoriusFreezeGuardMasterCopyContract.asProvider,
claimingMasterCopyContract: claimingMasterCopyContract.asProvider,
};

const builderBaseContracts: BaseContracts = {
Expand All @@ -101,6 +100,7 @@ const useDeployAzorius = () => {
safeSingleton,
zodiacModuleProxyFactory,
multiSendCallOnly,
erc20ClaimMasterCopy,
undefined,
undefined,
);
Expand Down Expand Up @@ -168,6 +168,7 @@ const useDeployAzorius = () => {
safeSingleton,
zodiacModuleProxyFactory,
multiSendCallOnly,
erc20ClaimMasterCopy,
],
);

Expand Down
9 changes: 0 additions & 9 deletions src/hooks/safe/useSafeContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
MultisigFreezeVoting__factory,
LinearERC20Voting__factory,
Azorius__factory,
ERC20Claim__factory,
LinearERC721Voting__factory,
ERC721FreezeVoting__factory,
} from '@fractal-framework/fractal-contracts';
Expand All @@ -29,7 +28,6 @@ export default function useSafeContracts() {
multisigFreezeVotingMasterCopy,
erc20FreezeVotingMasterCopy,
erc721FreezeVotingMasterCopy,
claimingMasterCopy,
linearVotingERC721MasterCopy,
},
} = useNetworkConfig();
Expand Down Expand Up @@ -89,11 +87,6 @@ export default function useSafeContracts() {
asProvider: ERC721FreezeVoting__factory.connect(erc721FreezeVotingMasterCopy, provider),
};

const claimingMasterCopyContract = {
asSigner: ERC20Claim__factory.connect(claimingMasterCopy, signerOrProvider),
asProvider: ERC20Claim__factory.connect(claimingMasterCopy, provider),
};

return {
fractalAzoriusMasterCopyContract,
linearVotingMasterCopyContract,
Expand All @@ -103,7 +96,6 @@ export default function useSafeContracts() {
freezeMultisigVotingMasterCopyContract,
freezeERC20VotingMasterCopyContract,
freezeERC721VotingMasterCopyContract,
claimingMasterCopyContract,
linearVotingERC721MasterCopyContract,
};
}, [
Expand All @@ -114,7 +106,6 @@ export default function useSafeContracts() {
azoriusFreezeGuardMasterCopy,
multisigFreezeVotingMasterCopy,
erc20FreezeVotingMasterCopy,
claimingMasterCopy,
linearVotingERC721MasterCopy,
erc721FreezeVotingMasterCopy,
signerOrProvider,
Expand Down
27 changes: 12 additions & 15 deletions src/models/AzoriusTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
encodeFunctionData,
PublicClient,
} from 'viem';
import ERC20ClaimAbi from '../assets/abi/ERC20Claim';
import GnosisSafeL2Abi from '../assets/abi/GnosisSafeL2';
import ModuleProxyFactoryAbi from '../assets/abi/ModuleProxyFactory';
import VotesERC20Abi from '../assets/abi/VotesERC20';
Expand Down Expand Up @@ -61,6 +62,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
private votesERC20MasterCopyAddress: string;
private moduleProxyFactoryAddress: Address;
private multiSendCallOnlyAddress: Address;
private erc20ClaimMasterCopyAddress: Address;

private tokenNonce: bigint;
private strategyNonce: bigint;
Expand All @@ -78,6 +80,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
votesERC20MasterCopyAddress: string,
moduleProxyFactoryAddress: Address,
multiSendCallOnlyAddress: Address,
erc20ClaimMasterCopyAddress: Address,
parentAddress?: Address,
parentTokenAddress?: Address,
) {
Expand All @@ -102,6 +105,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
this.votesERC20MasterCopyAddress = votesERC20MasterCopyAddress;
this.moduleProxyFactoryAddress = moduleProxyFactoryAddress;
this.multiSendCallOnlyAddress = multiSendCallOnlyAddress;
this.erc20ClaimMasterCopyAddress = erc20ClaimMasterCopyAddress;

if (daoData.votingStrategyType === VotingStrategyType.LINEAR_ERC20) {
daoData = daoData as AzoriusERC20DAO;
Expand Down Expand Up @@ -250,11 +254,7 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
ModuleProxyFactoryAbi,
this.moduleProxyFactoryAddress,
'deployModule',
[
this.azoriusContracts!.claimingMasterCopyContract.address,
this.encodedSetupTokenClaimData,
this.claimNonce,
],
[this.erc20ClaimMasterCopyAddress, this.encodedSetupTokenClaimData, this.claimNonce],
0,
false,
);
Expand Down Expand Up @@ -404,20 +404,17 @@ export class AzoriusTxBuilder extends BaseTxBuilder {
azoriusGovernanceDaoData.parentAllocationAmount,
],
);
const encodedSetupTokenClaimData =
this.azoriusContracts!.claimingMasterCopyContract.interface.encodeFunctionData('setUp', [
encodedInitTokenData,
]);
if (!isHex(encodedSetupTokenClaimData)) {
throw new Error('Error ecnoding setup token claim data');
}
const encodedSetupTokenClaimData = encodeFunctionData({
abi: ERC20ClaimAbi,
functionName: 'setUp',
args: [encodedInitTokenData],
});

this.encodedSetupTokenClaimData = encodedSetupTokenClaimData;
}

private setPredictedTokenClaimAddress() {
const tokenByteCodeLinear = generateContractByteCodeLinear(
getAddress(this.azoriusContracts!.claimingMasterCopyContract.address),
);
const tokenByteCodeLinear = generateContractByteCodeLinear(this.erc20ClaimMasterCopyAddress);

const tokenSalt = generateSalt(this.encodedSetupTokenClaimData!, this.claimNonce);

Expand Down
4 changes: 4 additions & 0 deletions src/models/TxBuilderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class TxBuilderFactory extends BaseTxBuilder {
private gnosisSafeSingletonAddress: string;
private moduleProxyFactoryAddress: string;
private multiSendCallOnlyAddress: string;
private erc20ClaimMasterCopyAddress: string;

constructor(
signerOrProvider: ethers.Signer | any,
Expand All @@ -53,6 +54,7 @@ export class TxBuilderFactory extends BaseTxBuilder {
gnosisSafeSingletonAddress: string,
moduleProxyFactoryAddress: string,
multiSendCallOnlyAddress: string,
erc20ClaimMasterCopyAddress: string,
parentAddress?: string,
parentTokenAddress?: string,
) {
Expand All @@ -76,6 +78,7 @@ export class TxBuilderFactory extends BaseTxBuilder {
this.gnosisSafeSingletonAddress = gnosisSafeSingletonAddress;
this.moduleProxyFactoryAddress = moduleProxyFactoryAddress;
this.multiSendCallOnlyAddress = multiSendCallOnlyAddress;
this.erc20ClaimMasterCopyAddress = erc20ClaimMasterCopyAddress;
}

public setSafeContract(safeAddress: Address) {
Expand Down Expand Up @@ -179,6 +182,7 @@ export class TxBuilderFactory extends BaseTxBuilder {
this.votesERC20MasterCopyAddress,
getAddress(this.moduleProxyFactoryAddress),
getAddress(this.multiSendCallOnlyAddress),
getAddress(this.erc20ClaimMasterCopyAddress),
this.parentAddress ? getAddress(this.parentAddress) : undefined,
this.parentTokenAddress ? getAddress(this.parentTokenAddress) : undefined,
);
Expand Down
7 changes: 5 additions & 2 deletions src/providers/App/governance/action.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ERC20Claim } from '@fractal-framework/fractal-contracts';
import { Address } from 'viem';
import {
FractalProposal,
ProposalVotesSummary,
Expand Down Expand Up @@ -111,7 +111,10 @@ export type FractalGovernanceActions =
type: FractalGovernanceAction.SET_TOKEN_ACCOUNT_DATA;
payload: VotesData;
}
| { type: FractalGovernanceAction.SET_CLAIMING_CONTRACT; payload: ERC20Claim }
| {
type: FractalGovernanceAction.SET_CLAIMING_CONTRACT;
payload: Address;
}
| {
type: FractalGovernanceAction.RESET_TOKEN_ACCOUNT_DATA;
}
Expand Down
Loading

0 comments on commit ea82775

Please sign in to comment.