Skip to content

Commit

Permalink
Support changes to contract state refactor
Browse files Browse the repository at this point in the history
- uses useSafeContracts directly instead of global state to get base contracts
  • Loading branch information
Da-Colon committed Mar 15, 2024
1 parent d2b8b5d commit b2feac2
Show file tree
Hide file tree
Showing 22 changed files with 470 additions and 296 deletions.
4 changes: 2 additions & 2 deletions src/components/DaoCreator/formComponents/GuardDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function GuardDetails(props: ICreationStepProps) {
const {
node: { safe },
governance,
governanceContracts: { azoriusContract },
governanceContracts: { azoriusContractAddress },
readOnly: { dao },
} = useFractal();
const { type } = governance;
Expand All @@ -55,7 +55,7 @@ function GuardDetails(props: ICreationStepProps) {
setFieldValue('multisig.customNonce', safe.nonce);
setShowCustomNonce(true);
}
}, [isSubDAO, azoriusContract, type, setFieldValue, safe, dao, showCustomNonce]);
}, [isSubDAO, azoriusContractAddress, type, setFieldValue, safe, dao, showCustomNonce]);

useEffect(() => {
// set the initial value for freezeGuard.freezeVotesThreshold
Expand Down
11 changes: 6 additions & 5 deletions src/components/Proposals/ProposalSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BigNumber } from 'ethers';
import { useMemo, useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { BACKGROUND_SEMI_TRANSPARENT } from '../../constants/common';
import useSafeContracts from '../../hooks/safe/useSafeContracts';
import useBlockTimestamp from '../../hooks/utils/useBlockTimestamp';
import { useFractal } from '../../providers/App/AppProvider';
import { AzoriusGovernance, AzoriusProposal, GovernanceType } from '../../types';
Expand All @@ -32,12 +33,11 @@ export default function ProposalSummary({
}) {
const {
governance,
governanceContracts: { tokenContract },
readOnly: {
user: { votingWeight, address },
},
} = useFractal();

const baseContracts = useSafeContracts();
const azoriusGovernance = governance as AzoriusGovernance;
const { votesToken, type, erc721Tokens, votingStrategy } = azoriusGovernance;
const { t } = useTranslation(['proposal', 'common', 'navigation']);
Expand All @@ -62,16 +62,17 @@ export default function ProposalSummary({

useEffect(() => {
async function loadProposalVotingWeight() {
if (tokenContract && address) {
const pastVotingWeight = await tokenContract.asProvider.getPastVotes(address, startBlock);
if (address && baseContracts && votesToken) {
const tokenContract = baseContracts?.votesTokenMasterCopyContract.asProvider.attach(votesToken.address);
const pastVotingWeight = await tokenContract.getPastVotes(address, startBlock);
setProposalsERC20VotingWeight(
pastVotingWeight.div(votesTokenDecimalsDenominator).toString(),
);
}
}

loadProposalVotingWeight();
}, [address, startBlock, tokenContract, votesTokenDecimalsDenominator]);
}, [address, startBlock, votesTokenDecimalsDenominator, baseContracts, votesToken]);

const isERC20 = type === GovernanceType.AZORIUS_ERC20;
const isERC721 = type === GovernanceType.AZORIUS_ERC721;
Expand Down
27 changes: 18 additions & 9 deletions src/components/Proposals/ProposalVotes/context/VoteContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useContext, useCallback, useEffect, useState, createContext, ReactNode } from 'react';
import useSnapshotProposal from '../../../../hooks/DAO/loaders/snapshot/useSnapshotProposal';
import useUserERC721VotingTokens from '../../../../hooks/DAO/proposal/useUserERC721VotingTokens';
import useSafeContracts from '../../../../hooks/safe/useSafeContracts';
import { useFractal } from '../../../../providers/App/AppProvider';
import {
FractalProposal,
Expand Down Expand Up @@ -52,8 +53,10 @@ export function VoteContextProvider({
readOnly: { user, dao },
node: { safe },
governance: { type },
governanceContracts: { ozLinearVotingContract },
governanceContracts: { ozLinearVotingContractAddress },
} = useFractal();
const baseContracts = useSafeContracts();

const { loadVotingWeight } = useSnapshotProposal(proposal as SnapshotProposal);
const { remainingTokenIds, getUserERC721VotingTokens } = useUserERC721VotingTokens(
proposal.proposalId,
Expand Down Expand Up @@ -91,14 +94,19 @@ export function VoteContextProvider({
if (isSnapshotProposal) {
const votingWeightData = await loadVotingWeight();
newCanVote = votingWeightData.votingWeight >= 1;
} else if (type === GovernanceType.AZORIUS_ERC20) {
} else if (
type === GovernanceType.AZORIUS_ERC20 &&
ozLinearVotingContractAddress &&
baseContracts
) {
const ozLinearVotingContract =
baseContracts.linearVotingMasterCopyContract.asProvider.attach(
ozLinearVotingContractAddress,
);
newCanVote =
(
await ozLinearVotingContract!.asProvider.getVotingWeight(
user.address,
proposal.proposalId,
)
)?.gt(0) && !hasVoted;
(await ozLinearVotingContract.getVotingWeight(user.address, proposal.proposalId))?.gt(
0,
) && !hasVoted;
} else if (type === GovernanceType.AZORIUS_ERC721) {
if (refetchUserTokens) {
await getUserERC721VotingTokens();
Expand Down Expand Up @@ -126,8 +134,9 @@ export function VoteContextProvider({
getUserERC721VotingTokens,
isSnapshotProposal,
loadVotingWeight,
ozLinearVotingContract,
proposal?.proposalId,
baseContracts,
ozLinearVotingContractAddress,
],
);
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { useNetworkConfig } from '../../../../providers/NetworkConfig/NetworkCon
import { TransferType, TokenInfo, AssetTransfer } from '../../../../types';
import { formatCoin } from '../../../../utils/numberFormats';


export enum TokenEventType {
DEPOSIT,
WITHDRAW,
}

export interface TransferDisplayData {
eventType: TokenEventType;
transferType: TransferType;
Expand Down
18 changes: 13 additions & 5 deletions src/components/ui/modals/DelegateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { BigNumber, constants } from 'ethers';
import { Field, FieldAttributes, Formik } from 'formik';
import { useTranslation } from 'react-i18next';
import * as Yup from 'yup';
import { LockRelease__factory } from '../../../assets/typechain-types/dcnt';
import useDelegateVote from '../../../hooks/DAO/useDelegateVote';
import useSafeContracts from '../../../hooks/safe/useSafeContracts';
import { useValidationAddress } from '../../../hooks/schemas/common/useValidationAddress';
import useDisplayName from '../../../hooks/utils/useDisplayName';
import { useFractal } from '../../../providers/App/AppProvider';
Expand All @@ -20,11 +22,13 @@ export function DelegateModal({ close }: { close: Function }) {

const {
governance,
governanceContracts: { tokenContract, lockReleaseContract },
governanceContracts: { votesTokenContractAddress, lockReleaseContractAddress },
readOnly: { user },
action: { loadReadOnlyValues },
} = useFractal();

const baseContracts = useSafeContracts()

const signer = useEthersSigner();
const azoriusGovernance = governance as AzoriusGovernance;
const decentGovernance = azoriusGovernance as DecentGovernance;
Expand All @@ -34,28 +38,32 @@ export function DelegateModal({ close }: { close: Function }) {
const { addressValidationTest } = useValidationAddress();

const submitDelegation = async (values: { address: string }) => {
if (!tokenContract) return;
if (!votesTokenContractAddress || !baseContracts) return;
let validAddress = values.address;
if (couldBeENS(validAddress)) {
validAddress = await signer!.resolveName(values.address);
}
const votingTokenContract = baseContracts.votesERC20WrapperMasterCopyContract.asProvider.attach(
votesTokenContractAddress,
);
delegateVote({
delegatee: validAddress,
votingTokenContract: tokenContract?.asSigner,
votingTokenContract,
successCallback: () => {
close();
},
});
};
const submitLockedDelegation = async (values: { address: string }) => {
if (!lockReleaseContract) return;
if (!lockReleaseContractAddress || !baseContracts || !signer) return;
let validAddress = values.address;
if (couldBeENS(validAddress)) {
validAddress = await signer!.resolveName(values.address);
}
const lockReleaseContract = LockRelease__factory.connect(lockReleaseContractAddress, signer)
delegateVote({
delegatee: validAddress,
votingTokenContract: lockReleaseContract.asSigner,
votingTokenContract: lockReleaseContract,
successCallback: async () => {
await loadReadOnlyValues();
close();
Expand Down
15 changes: 9 additions & 6 deletions src/components/ui/modals/UnwrapToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useTranslation } from 'react-i18next';
import { useAccount } from 'wagmi';
import * as Yup from 'yup';
import { useERC20LinearToken } from '../../../hooks/DAO/loaders/governance/useERC20LinearToken';
import useSafeContracts from '../../../hooks/safe/useSafeContracts';
import useApproval from '../../../hooks/utils/useApproval';
import { useFormHelpers } from '../../../hooks/utils/useFormHelpers';
import { useTransaction } from '../../../hooks/utils/useTransaction';
Expand All @@ -22,7 +23,7 @@ export function UnwrapToken({ close }: { close: () => void }) {
const azoriusGovernance = governance as AzoriusGovernance;
const signer = useEthersSigner();
const { address: account } = useAccount();

const baseContracts = useSafeContracts();
const { loadERC20TokenAccountData } = useERC20LinearToken({ onMount: false });

const [contractCall, pending] = useTransaction();
Expand All @@ -31,18 +32,20 @@ export function UnwrapToken({ close }: { close: () => void }) {
approveTransaction,
pending: approvalPending,
} = useApproval(
governanceContracts.tokenContract?.asSigner.attach(governanceContracts.underlyingTokenAddress!),
baseContracts?.votesTokenMasterCopyContract?.asSigner.attach(governanceContracts.underlyingTokenAddress!),
azoriusGovernance.votesToken?.address,
);


const { t } = useTranslation(['modals', 'treasury']);
const { restrictChars } = useFormHelpers();

const handleFormSubmit = useCallback(
(amount: BigNumberValuePair) => {
const { tokenContract } = governanceContracts;
if (!tokenContract || !signer || !account) return;
const wrapperTokenContract = tokenContract.asSigner as VotesERC20Wrapper;
const { votesTokenContractAddress } = governanceContracts;
if (!votesTokenContractAddress || !signer || !account) return;
const votesTokenContract = baseContracts?.votesERC20WrapperMasterCopyContract?.asSigner.attach(votesTokenContractAddress);
const wrapperTokenContract = votesTokenContract as VotesERC20Wrapper;
contractCall({
contractFn: () => wrapperTokenContract.withdrawTo(account, amount.bigNumberValue!),
pendingMessage: t('unwrapTokenPendingMessage'),
Expand All @@ -56,7 +59,7 @@ export function UnwrapToken({ close }: { close: () => void }) {
},
});
},
[account, contractCall, governanceContracts, signer, close, t, loadERC20TokenAccountData],
[account, contractCall, governanceContracts, signer, close, t, loadERC20TokenAccountData, baseContracts],
);

if (
Expand Down
27 changes: 21 additions & 6 deletions src/components/ui/modals/WrapToken.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Button, Flex, Input } from '@chakra-ui/react';
import { LabelWrapper } from '@decent-org/fractal-ui';
import { VotesERC20Wrapper } from '@fractal-framework/fractal-contracts';
import { BigNumber, Contract } from 'ethers';
import { Formik, FormikProps } from 'formik';
import { useCallback, useEffect, useState } from 'react';
Expand All @@ -9,6 +8,7 @@ import { erc20ABI, useAccount } from 'wagmi';
import * as Yup from 'yup';
import { logError } from '../../../helpers/errorLogging';
import { useERC20LinearToken } from '../../../hooks/DAO/loaders/governance/useERC20LinearToken';
import useSafeContracts from '../../../hooks/safe/useSafeContracts';
import useApproval from '../../../hooks/utils/useApproval';
import { useFormHelpers } from '../../../hooks/utils/useFormHelpers';
import { useTransaction } from '../../../hooks/utils/useTransaction';
Expand All @@ -27,6 +27,7 @@ export function WrapToken({ close }: { close: () => void }) {
value: '',
bigNumberValue: BigNumber.from(0),
});
const baseContracts = useSafeContracts();

const { loadERC20TokenAccountData } = useERC20LinearToken({ onMount: false });
const [contractCall, pending] = useTransaction();
Expand All @@ -35,7 +36,9 @@ export function WrapToken({ close }: { close: () => void }) {
approveTransaction,
pending: approvalPending,
} = useApproval(
governanceContracts.tokenContract?.asSigner.attach(governanceContracts.underlyingTokenAddress!),
baseContracts?.votesTokenMasterCopyContract?.asSigner.attach(
governanceContracts.underlyingTokenAddress!,
),
azoriusGovernance.votesToken?.address,
userBalance.bigNumberValue,
);
Expand Down Expand Up @@ -82,9 +85,12 @@ export function WrapToken({ close }: { close: () => void }) {

const handleFormSubmit = useCallback(
(amount: BigNumberValuePair) => {
const { tokenContract } = governanceContracts;
if (!tokenContract || !signer || !account) return;
const wrapperTokenContract = tokenContract.asSigner as VotesERC20Wrapper;
const { votesTokenContractAddress } = governanceContracts;
if (!votesTokenContractAddress || !signer || !account || !baseContracts) return;
const wrapperTokenContract =
baseContracts.votesERC20WrapperMasterCopyContract.asSigner.attach(
votesTokenContractAddress,
);
contractCall({
contractFn: () => wrapperTokenContract.depositFor(account, amount.bigNumberValue!),
pendingMessage: t('wrapTokenPendingMessage'),
Expand All @@ -98,7 +104,16 @@ export function WrapToken({ close }: { close: () => void }) {
},
});
},
[account, contractCall, governanceContracts, signer, close, t, loadERC20TokenAccountData],
[
account,
contractCall,
governanceContracts,
signer,
close,
t,
loadERC20TokenAccountData,
baseContracts,
],
);

if (
Expand Down
Loading

0 comments on commit b2feac2

Please sign in to comment.