Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove linear erc721 voting #1669

Merged
merged 11 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
799 changes: 799 additions & 0 deletions src/assets/abi/LinearERC721Voting.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export function VoteContextProvider({

const { loadVotingWeight } = useSnapshotProposal(proposal as SnapshotProposal);
const { remainingTokenIds, getUserERC721VotingTokens } = useUserERC721VotingTokens(
null,
proposal.proposalId,
undefined,
true,
);
const { isSnapshotProposal } = useSnapshotProposal(proposal);
Expand Down
90 changes: 46 additions & 44 deletions src/hooks/DAO/loaders/governance/useAzoriusListeners.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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 ERC721VotedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC721Voting';
import { Dispatch, useEffect, useMemo } from 'react';
import { getAddress, getContract, GetContractReturnType, Hex, PublicClient } from 'viem';
import { usePublicClient } from 'wagmi';
import LinearERC20VotingAbi from '../../../../assets/abi/LinearERC20Voting';
import LinearERC721VotingAbi from '../../../../assets/abi/LinearERC721Voting';
import { useFractal } from '../../../../providers/App/AppProvider';
import { FractalGovernanceAction } from '../../../../providers/App/governance/action';
import { useEthersProvider } from '../../../../providers/Ethers/hooks/useEthersProvider';
Expand All @@ -34,7 +33,9 @@ const proposalCreatedEventListener = (
erc20StrategyContract:
| GetContractReturnType<typeof LinearERC20VotingAbi, PublicClient>
| undefined,
erc721StrategyContract: LinearERC721Voting | undefined,
erc721StrategyContract:
| GetContractReturnType<typeof LinearERC721VotingAbi, PublicClient>
| undefined,
provider: Providers,
strategyType: VotingStrategyType,
decode: (value: string, to: string, data?: string | undefined) => Promise<DecodedTransaction[]>,
Expand Down Expand Up @@ -91,33 +92,6 @@ const proposalCreatedEventListener = (
};
};

const erc721VotedEventListener = (
erc721StrategyContract: LinearERC721Voting,
strategyType: VotingStrategyType,
dispatch: Dispatch<FractalActions>,
): TypedListener<ERC721VotedEvent> => {
return async (voter, proposalId, voteType, tokenAddresses, tokenIds) => {
const votesSummary = await getProposalVotesSummary(
undefined,
erc721StrategyContract,
strategyType,
BigInt(proposalId),
);

dispatch({
type: FractalGovernanceAction.UPDATE_NEW_AZORIUS_ERC721_VOTE,
payload: {
proposalId: proposalId.toString(),
voter,
support: voteType,
tokenAddresses,
tokenIds: tokenIds.map(tokenId => tokenId.toString()),
votesSummary,
},
});
};
};

export const useAzoriusListeners = () => {
const {
action,
Expand Down Expand Up @@ -165,14 +139,16 @@ export const useAzoriusListeners = () => {
}, [ozLinearVotingContractAddress, publicClient]);

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

return baseContracts.linearVotingERC721MasterCopyContract.asProvider.attach(
erc721LinearVotingContractAddress,
);
}, [baseContracts, erc721LinearVotingContractAddress]);
return getContract({
abi: LinearERC721VotingAbi,
address: getAddress(erc721LinearVotingContractAddress),
client: publicClient,
});
}, [erc721LinearVotingContractAddress, publicClient]);

useEffect(() => {
if (!azoriusContract || !provider || !strategyType) {
Expand Down Expand Up @@ -248,19 +224,45 @@ export const useAzoriusListeners = () => {
return;
}

const votedEvent = erc721StrategyContract.filters.Voted();
const listener = erc721VotedEventListener(
erc721StrategyContract,
strategyType,
action.dispatch,
);
const unwatch = erc721StrategyContract.watchEvent.Voted({
onLogs: async logs => {
for (const log of logs) {
if (
!log.args.proposalId ||
!log.args.voter ||
!log.args.voteType ||
!log.args.tokenAddresses ||
!log.args.tokenIds
) {
continue;
}

erc721StrategyContract.on(votedEvent, listener);
const votesSummary = await getProposalVotesSummary(
undefined,
erc721StrategyContract,
strategyType,
BigInt(log.args.proposalId),
);

action.dispatch({
type: FractalGovernanceAction.UPDATE_NEW_AZORIUS_ERC721_VOTE,
payload: {
proposalId: log.args.proposalId.toString(),
voter: log.args.voter,
support: log.args.voteType,
tokenAddresses: log.args.tokenAddresses.map(tokenAddress => tokenAddress),
tokenIds: log.args.tokenIds.map(tokenId => tokenId.toString()),
votesSummary,
},
});
}
},
});

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

useEffect(() => {
if (!azoriusContract) {
Expand Down
28 changes: 15 additions & 13 deletions src/hooks/DAO/loaders/governance/useAzoriusProposals.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { LinearERC721Voting } from '@fractal-framework/fractal-contracts';
import {
Azorius,
ProposalExecutedEvent,
} from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/Azorius';
import { VotedEvent as ERC721VotedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC721Voting';
import { useCallback, useEffect, useMemo, useRef } from 'react';
import {
GetContractEventsReturnType,
Expand All @@ -15,6 +13,7 @@ import {
} from 'viem';
import { usePublicClient } from 'wagmi';
import LinearERC20VotingAbi from '../../../../assets/abi/LinearERC20Voting';
import LinearERC721VotingAbi from '../../../../assets/abi/LinearERC721Voting';
import { logError } from '../../../../helpers/errorLogging';
import { useFractal } from '../../../../providers/App/AppProvider';
import { useEthersProvider } from '../../../../providers/Ethers/hooks/useEthersProvider';
Expand Down Expand Up @@ -73,14 +72,16 @@ export const useAzoriusProposals = () => {
}, [ozLinearVotingContractAddress, publicClient]);

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

return baseContracts.linearVotingERC721MasterCopyContract.asProvider.attach(
erc721LinearVotingContractAddress,
);
}, [baseContracts, erc721LinearVotingContractAddress]);
return getContract({
abi: LinearERC721VotingAbi,
address: getAddress(erc721LinearVotingContractAddress),
client: publicClient,
});
}, [erc721LinearVotingContractAddress, publicClient]);

const erc20VotedEvents = useMemo(async () => {
if (!erc20StrategyContract) {
Expand All @@ -95,10 +96,7 @@ export const useAzoriusProposals = () => {
return;
}

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

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

const executedEvents = useMemo(async () => {
Expand Down Expand Up @@ -128,12 +126,16 @@ export const useAzoriusProposals = () => {
_erc20StrategyContract:
| GetContractReturnType<typeof LinearERC20VotingAbi, PublicClient>
| undefined,
_erc721StrategyContract: LinearERC721Voting | undefined,
_erc721StrategyContract:
| GetContractReturnType<typeof LinearERC721VotingAbi, PublicClient>
| undefined,
_strategyType: VotingStrategyType | undefined,
_erc20VotedEvents: Promise<
GetContractEventsReturnType<typeof LinearERC20VotingAbi, 'Voted'> | undefined
>,
_erc721VotedEvents: Promise<ERC721VotedEvent[] | undefined>,
_erc721VotedEvents: Promise<
GetContractEventsReturnType<typeof LinearERC721VotingAbi, 'Voted'> | undefined
>,
_executedEvents: Promise<ProposalExecutedEvent[] | undefined>,
_provider: Providers | undefined,
_decode: (
Expand Down
119 changes: 64 additions & 55 deletions src/hooks/DAO/loaders/governance/useERC721LinearStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { TypedListener } from '@fractal-framework/fractal-contracts/dist/typechain-types/common';
import {
VotingPeriodUpdatedEvent,
QuorumThresholdUpdatedEvent,
} from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC721Voting';
import { useCallback, useEffect } from 'react';
import { useCallback, useEffect, useMemo } from 'react';
import { getAddress, getContract } from 'viem';
import { usePublicClient } from 'wagmi';
import LinearERC721VotingAbi from '../../../../assets/abi/LinearERC721Voting';
import { useFractal } from '../../../../providers/App/AppProvider';
import { FractalGovernanceAction } from '../../../../providers/App/governance/action';
import { useEthersProvider } from '../../../../providers/Ethers/hooks/useEthersProvider';
Expand All @@ -20,25 +18,31 @@ export const useERC721LinearStrategy = () => {
const provider = useEthersProvider();
const { getTimeDuration } = useTimeHelpers();
const baseContracts = useSafeContracts();
const publicClient = usePublicClient();

const erc721LinearVotingContract = useMemo(() => {
if (!erc721LinearVotingContractAddress || !publicClient) {
return;
}

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

const loadERC721Strategy = useCallback(async () => {
if (
!erc721LinearVotingContractAddress ||
!azoriusContractAddress ||
!provider ||
!baseContracts
) {
if (!azoriusContractAddress || !provider || !baseContracts || !erc721LinearVotingContract) {
return {};
}
const erc721LinearVotingContract =
baseContracts.linearVotingERC721MasterCopyContract.asProvider.attach(
erc721LinearVotingContractAddress,
);

const azoriusContract =
baseContracts.fractalAzoriusMasterCopyContract.asProvider.attach(azoriusContractAddress);

const [votingPeriodBlocks, quorumThreshold, timeLockPeriod] = await Promise.all([
erc721LinearVotingContract.votingPeriod(),
erc721LinearVotingContract.quorumThreshold(),
erc721LinearVotingContract.read.votingPeriod(),
erc721LinearVotingContract.read.quorumThreshold(),
azoriusContract.timelockPeriod(),
]);

Expand All @@ -50,7 +54,7 @@ export const useERC721LinearStrategy = () => {
formatted: getTimeDuration(votingPeriodValue),
},
quorumThreshold: {
value: quorumThreshold.toBigInt(),
value: quorumThreshold,
formatted: quorumThreshold.toString(),
},
timeLockPeriod: {
Expand All @@ -61,58 +65,63 @@ export const useERC721LinearStrategy = () => {
};
action.dispatch({ type: FractalGovernanceAction.SET_STRATEGY, payload: votingData });
}, [
erc721LinearVotingContractAddress,
action,
azoriusContractAddress,
baseContracts,
erc721LinearVotingContract,
getTimeDuration,
action,
provider,
baseContracts,
]);

useEffect(() => {
if (!erc721LinearVotingContractAddress || !baseContracts) {
if (!erc721LinearVotingContract) {
return;
}
const erc721LinearVotingContract =
baseContracts.linearVotingERC721MasterCopyContract.asProvider.attach(
erc721LinearVotingContractAddress,
);
const votingPeriodfilter = erc721LinearVotingContract.filters.VotingPeriodUpdated();
const listener: TypedListener<VotingPeriodUpdatedEvent> = votingPeriod => {
action.dispatch({
type: FractalGovernanceAction.UPDATE_VOTING_PERIOD,
payload: BigInt(votingPeriod),
});
};
erc721LinearVotingContract.on(votingPeriodfilter, listener);

const unwatch = erc721LinearVotingContract.watchEvent.VotingPeriodUpdated({
onLogs: logs => {
for (const log of logs) {
if (!log.args.votingPeriod) {
continue;
}

action.dispatch({
type: FractalGovernanceAction.UPDATE_VOTING_PERIOD,
payload: BigInt(log.args.votingPeriod),
});
}
},
});

return () => {
erc721LinearVotingContract.off(votingPeriodfilter, listener);
unwatch();
};
}, [erc721LinearVotingContractAddress, action, baseContracts]);
}, [action, erc721LinearVotingContract]);

useEffect(() => {
if (!erc721LinearVotingContractAddress || !baseContracts) {
if (!erc721LinearVotingContract) {
return;
}
const erc721LinearVotingContract =
baseContracts.linearVotingERC721MasterCopyContract.asProvider.attach(
erc721LinearVotingContractAddress,
);
const quorumThresholdUpdatedFilter =
erc721LinearVotingContract.filters.QuorumThresholdUpdated();
const quorumThresholdUpdatedListener: TypedListener<
QuorumThresholdUpdatedEvent
> = quorumThreshold => {
action.dispatch({
type: FractalGovernanceAction.UPDATE_VOTING_QUORUM_THRESHOLD,
payload: quorumThreshold.toBigInt(),
});
};
erc721LinearVotingContract.on(quorumThresholdUpdatedFilter, quorumThresholdUpdatedListener);

const unwatch = erc721LinearVotingContract.watchEvent.QuorumThresholdUpdated({
onLogs: logs => {
for (const log of logs) {
if (!log.args.quorumThreshold) {
continue;
}

action.dispatch({
type: FractalGovernanceAction.UPDATE_VOTING_QUORUM_THRESHOLD,
payload: log.args.quorumThreshold,
});
}
},
});

return () => {
erc721LinearVotingContract.off(quorumThresholdUpdatedFilter, quorumThresholdUpdatedListener);
unwatch();
};
}, [erc721LinearVotingContractAddress, action, baseContracts]);
}, [erc721LinearVotingContract, action]);

return loadERC721Strategy;
};
Loading