Skip to content

Commit

Permalink
Remove LinearERC20Voting typechain from project
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgall committed May 7, 2024
1 parent f9f7bc0 commit c52eedd
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 220 deletions.
86 changes: 45 additions & 41 deletions src/hooks/DAO/loaders/governance/useAzoriusListeners.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { LinearERC20Voting, LinearERC721Voting } from '@fractal-framework/fractal-contracts';
import { LinearERC721Voting } from '@fractal-framework/fractal-contracts';
import { TypedListener } from '@fractal-framework/fractal-contracts/dist/typechain-types/common';
import { TimelockPeriodUpdatedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/MultisigFreezeGuard';
import {
Azorius,
ProposalCreatedEvent,
} from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/Azorius';
import { VotedEvent as ERC20VotedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC20Voting';
import { VotedEvent as ERC721VotedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC721Voting';
import { Dispatch, useEffect, useMemo } from 'react';
import { getAddress, Hex } from 'viem';
import { getAddress, getContract, GetContractReturnType, Hex, PublicClient } from 'viem';
import { usePublicClient } from 'wagmi';
import LinearERC20VotingAbi from '../../../../assets/abi/LinearERC20Voting';
import { useFractal } from '../../../../providers/App/AppProvider';
import { FractalGovernanceAction } from '../../../../providers/App/governance/action';
import { useEthersProvider } from '../../../../providers/Ethers/hooks/useEthersProvider';
Expand All @@ -30,7 +31,9 @@ import { useSafeDecoder } from '../../../utils/useSafeDecoder';

const proposalCreatedEventListener = (
azoriusContract: Azorius,
erc20StrategyContract: LinearERC20Voting | undefined,
erc20StrategyContract:
| GetContractReturnType<typeof LinearERC20VotingAbi, PublicClient>
| undefined,
erc721StrategyContract: LinearERC721Voting | undefined,
provider: Providers,
strategyType: VotingStrategyType,
Expand Down Expand Up @@ -88,32 +91,6 @@ const proposalCreatedEventListener = (
};
};

const erc20VotedEventListener = (
erc20StrategyContract: LinearERC20Voting,
strategyType: VotingStrategyType,
dispatch: Dispatch<FractalActions>,
): TypedListener<ERC20VotedEvent> => {
return async (voter, proposalId, voteType, weight) => {
const votesSummary = await getProposalVotesSummary(
erc20StrategyContract,
undefined,
strategyType,
BigInt(proposalId),
);

dispatch({
type: FractalGovernanceAction.UPDATE_NEW_AZORIUS_ERC20_VOTE,
payload: {
proposalId: proposalId.toString(),
voter,
support: voteType,
weight: weight.toBigInt(),
votesSummary,
},
});
};
};

const erc721VotedEventListener = (
erc721StrategyContract: LinearERC721Voting,
strategyType: VotingStrategyType,
Expand Down Expand Up @@ -155,6 +132,8 @@ export const useAzoriusListeners = () => {
const provider = useEthersProvider();
const decode = useSafeDecoder();

const publicClient = usePublicClient();

const azoriusContract = useMemo(() => {
if (!baseContracts || !azoriusContractAddress) {
return;
Expand All @@ -174,14 +153,16 @@ export const useAzoriusListeners = () => {
}, [ozLinearVotingContractAddress, erc721LinearVotingContractAddress]);

const erc20StrategyContract = useMemo(() => {
if (!baseContracts || !ozLinearVotingContractAddress) {
if (!ozLinearVotingContractAddress || !publicClient) {
return undefined;
}

return baseContracts.linearVotingMasterCopyContract.asProvider.attach(
ozLinearVotingContractAddress,
);
}, [baseContracts, ozLinearVotingContractAddress]);
return getContract({
abi: LinearERC20VotingAbi,
address: getAddress(ozLinearVotingContractAddress),
client: publicClient,
});
}, [ozLinearVotingContractAddress, publicClient]);

const erc721StrategyContract = useMemo(() => {
if (!baseContracts || !erc721LinearVotingContractAddress) {
Expand Down Expand Up @@ -229,15 +210,38 @@ export const useAzoriusListeners = () => {
return;
}

const votedEvent = erc20StrategyContract.filters.Voted();
const listener = erc20VotedEventListener(erc20StrategyContract, strategyType, action.dispatch);

erc20StrategyContract.on(votedEvent, listener);
const unwatch = erc20StrategyContract.watchEvent.Voted({
onLogs: async logs => {
for (const log of logs) {
if (!log.args.proposalId || !log.args.voter || !log.args.voteType || !log.args.weight) {
continue;
}

const votesSummary = await getProposalVotesSummary(
erc20StrategyContract,
undefined,
strategyType,
BigInt(log.args.proposalId),
);

action.dispatch({
type: FractalGovernanceAction.UPDATE_NEW_AZORIUS_ERC20_VOTE,
payload: {
proposalId: log.args.proposalId.toString(),
voter: log.args.voter,
support: log.args.voteType,
weight: log.args.weight,
votesSummary,
},
});
}
},
});

return () => {
erc20StrategyContract.off(votedEvent, listener);
unwatch();
};
}, [action.dispatch, erc20StrategyContract, strategyType]);
}, [action, erc20StrategyContract, strategyType]);

useEffect(() => {
if (strategyType !== VotingStrategyType.LINEAR_ERC721 || !erc721StrategyContract) {
Expand Down
41 changes: 27 additions & 14 deletions src/hooks/DAO/loaders/governance/useAzoriusProposals.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { LinearERC20Voting, LinearERC721Voting } from '@fractal-framework/fractal-contracts';
import { LinearERC721Voting } from '@fractal-framework/fractal-contracts';
import {
Azorius,
ProposalExecutedEvent,
} from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/Azorius';
import { VotedEvent as ERC20VotedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC20Voting';
import { VotedEvent as ERC721VotedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC721Voting';
import { useCallback, useEffect, useMemo, useRef } from 'react';
import { Hex, getAddress } from 'viem';
import {
GetContractEventsReturnType,
GetContractReturnType,
Hex,
PublicClient,
getAddress,
getContract,
} from 'viem';
import { usePublicClient } from 'wagmi';
import LinearERC20VotingAbi from '../../../../assets/abi/LinearERC20Voting';
import { logError } from '../../../../helpers/errorLogging';
import { useFractal } from '../../../../providers/App/AppProvider';
import { useEthersProvider } from '../../../../providers/Ethers/hooks/useEthersProvider';
Expand All @@ -32,6 +40,8 @@ export const useAzoriusProposals = () => {
const provider = useEthersProvider();
const decode = useSafeDecoder();

const publicClient = usePublicClient();

const azoriusContract = useMemo(() => {
if (!baseContracts || !azoriusContractAddress) {
return;
Expand All @@ -51,14 +61,16 @@ export const useAzoriusProposals = () => {
}, [ozLinearVotingContractAddress, erc721LinearVotingContractAddress]);

const erc20StrategyContract = useMemo(() => {
if (!baseContracts || !ozLinearVotingContractAddress) {
if (!ozLinearVotingContractAddress || !publicClient) {
return undefined;
}

return baseContracts.linearVotingMasterCopyContract.asProvider.attach(
ozLinearVotingContractAddress,
);
}, [baseContracts, ozLinearVotingContractAddress]);
return getContract({
abi: LinearERC20VotingAbi,
address: getAddress(ozLinearVotingContractAddress),
client: publicClient,
});
}, [ozLinearVotingContractAddress, publicClient]);

const erc721StrategyContract = useMemo(() => {
if (!baseContracts || !erc721LinearVotingContractAddress) {
Expand All @@ -75,10 +87,7 @@ export const useAzoriusProposals = () => {
return;
}

const filter = erc20StrategyContract.filters.Voted();
const events = await erc20StrategyContract.queryFilter(filter);

return events;
return erc20StrategyContract.getEvents.Voted();
}, [erc20StrategyContract]);

const erc721VotedEvents = useMemo(async () => {
Expand Down Expand Up @@ -116,10 +125,14 @@ export const useAzoriusProposals = () => {
const loadAzoriusProposals = useCallback(
async (
_azoriusContract: Azorius | undefined,
_erc20StrategyContract: LinearERC20Voting | undefined,
_erc20StrategyContract:
| GetContractReturnType<typeof LinearERC20VotingAbi, PublicClient>
| undefined,
_erc721StrategyContract: LinearERC721Voting | undefined,
_strategyType: VotingStrategyType | undefined,
_erc20VotedEvents: Promise<ERC20VotedEvent[] | undefined>,
_erc20VotedEvents: Promise<
GetContractEventsReturnType<typeof LinearERC20VotingAbi, 'Voted'> | undefined
>,
_erc721VotedEvents: Promise<ERC721VotedEvent[] | undefined>,
_executedEvents: Promise<ProposalExecutedEvent[] | undefined>,
_provider: Providers | undefined,
Expand Down
100 changes: 56 additions & 44 deletions src/hooks/DAO/loaders/governance/useERC20LinearStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TypedListener } from '@fractal-framework/fractal-contracts/dist/typechain-types/common';
import { QuorumNumeratorUpdatedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/BaseQuorumPercent';
import { VotingPeriodUpdatedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC20Voting';
import { useCallback, useEffect } from 'react';
import { useCallback, useEffect, useMemo } from 'react';
import { getAddress, getContract } from 'viem';
import { usePublicClient } from 'wagmi';
import LinearERC20VotingAbi from '../../../../assets/abi/LinearERC20Voting';
import { useFractal } from '../../../../providers/App/AppProvider';
import { FractalGovernanceAction } from '../../../../providers/App/governance/action';
import { useEthersProvider } from '../../../../providers/Ethers/hooks/useEthersProvider';
Expand All @@ -18,21 +18,32 @@ export const useERC20LinearStrategy = () => {
const baseContracts = useSafeContracts();
const provider = useEthersProvider();
const { getTimeDuration } = useTimeHelpers();
const publicClient = usePublicClient();

const ozLinearVotingContract = useMemo(() => {
if (!ozLinearVotingContractAddress || !publicClient) {
return;
}

return getContract({
abi: LinearERC20VotingAbi,
address: getAddress(ozLinearVotingContractAddress),
client: publicClient,
});
}, [ozLinearVotingContractAddress, publicClient]);

const loadERC20Strategy = useCallback(async () => {
if (!ozLinearVotingContractAddress || !azoriusContractAddress || !provider || !baseContracts) {
if (!ozLinearVotingContract || !azoriusContractAddress || !provider || !baseContracts) {
return {};
}
const ozLinearVotingContract = baseContracts.linearVotingMasterCopyContract.asProvider.attach(
ozLinearVotingContractAddress,
);

const azoriusContract =
baseContracts.fractalAzoriusMasterCopyContract.asProvider.attach(azoriusContractAddress);
const [votingPeriodBlocks, quorumNumerator, quorumDenominator, timeLockPeriod] =
await Promise.all([
ozLinearVotingContract.votingPeriod(),
(await ozLinearVotingContract.quorumNumerator()).toBigInt(),
(await ozLinearVotingContract.QUORUM_DENOMINATOR()).toBigInt(),
ozLinearVotingContract.read.votingPeriod(),
ozLinearVotingContract.read.quorumNumerator(),
ozLinearVotingContract.read.QUORUM_DENOMINATOR(),
azoriusContract.timelockPeriod(),
]);

Expand All @@ -56,56 +67,57 @@ export const useERC20LinearStrategy = () => {
};
action.dispatch({ type: FractalGovernanceAction.SET_STRATEGY, payload: votingData });
}, [
ozLinearVotingContractAddress,
action,
azoriusContractAddress,
baseContracts,
getTimeDuration,
action,
ozLinearVotingContract,
provider,
baseContracts,
]);

useEffect(() => {
if (!ozLinearVotingContractAddress || !baseContracts) {
if (!ozLinearVotingContract || !publicClient) {
return;
}
const ozLinearVotingContract = baseContracts.linearVotingMasterCopyContract.asProvider.attach(
ozLinearVotingContractAddress,
);

const votingPeriodfilter = ozLinearVotingContract.filters.VotingPeriodUpdated();
const listener: TypedListener<VotingPeriodUpdatedEvent> = votingPeriod => {
action.dispatch({
type: FractalGovernanceAction.UPDATE_VOTING_PERIOD,
payload: BigInt(votingPeriod),
});
};
ozLinearVotingContract.on(votingPeriodfilter, listener);
const unwatch = ozLinearVotingContract.watchEvent.VotingPeriodUpdated({
onLogs: logs => {
const lastLog = logs.pop();
if (lastLog && lastLog.args.votingPeriod) {
action.dispatch({
type: FractalGovernanceAction.UPDATE_VOTING_PERIOD,
payload: BigInt(lastLog.args.votingPeriod),
});
}
},
});

return () => {
ozLinearVotingContract.off(votingPeriodfilter, listener);
unwatch();
};
}, [ozLinearVotingContractAddress, action, baseContracts]);
}, [action, ozLinearVotingContract, publicClient]);

useEffect(() => {
if (!ozLinearVotingContractAddress || !baseContracts) {
if (!ozLinearVotingContract) {
return;
}
const ozLinearVotingContract = baseContracts.linearVotingMasterCopyContract.asProvider.attach(
ozLinearVotingContractAddress,
);
const quorumNumeratorUpdatedFilter = ozLinearVotingContract.filters.QuorumNumeratorUpdated();
const quorumNumeratorUpdatedListener: TypedListener<
QuorumNumeratorUpdatedEvent
> = quorumPercentage => {
action.dispatch({
type: FractalGovernanceAction.UPDATE_VOTING_QUORUM,
payload: quorumPercentage.toBigInt(),
});
};
ozLinearVotingContract.on(quorumNumeratorUpdatedFilter, quorumNumeratorUpdatedListener);

const unwatch = ozLinearVotingContract.watchEvent.QuorumNumeratorUpdated({
onLogs: logs => {
const lastLog = logs.pop();
if (lastLog && lastLog.args.quorumNumerator) {
action.dispatch({
type: FractalGovernanceAction.UPDATE_VOTING_QUORUM,
payload: lastLog.args.quorumNumerator,
});
}
},
});

return () => {
ozLinearVotingContract.off(quorumNumeratorUpdatedFilter, quorumNumeratorUpdatedListener);
unwatch();
};
}, [ozLinearVotingContractAddress, action, baseContracts]);
}, [action, ozLinearVotingContract]);

return loadERC20Strategy;
};
Loading

0 comments on commit c52eedd

Please sign in to comment.