Skip to content

Commit

Permalink
Get all Voted and ProposalExecuted events one time, not per proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgall committed Mar 30, 2024
1 parent ab359c0 commit a176e28
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
29 changes: 26 additions & 3 deletions src/hooks/DAO/loaders/governance/useAzoriusProposals.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { LinearERC20Voting, LinearERC721Voting } from '@fractal-framework/fractal-contracts';
import { TypedListener } from '@fractal-framework/fractal-contracts/dist/typechain-types/common';

Check warning on line 2 in src/hooks/DAO/loaders/governance/useAzoriusProposals.ts

View workflow job for this annotation

GitHub Actions / lint

'TypedListener' is defined but never used
import { ProposalExecutedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/Azorius';

Check warning on line 3 in src/hooks/DAO/loaders/governance/useAzoriusProposals.ts

View workflow job for this annotation

GitHub Actions / lint

'/home/runner/work/fractal-interface/fractal-interface/node_modules/@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/Azorius.d.ts' imported multiple times
import { ProposalCreatedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/Azorius';

Check warning on line 4 in src/hooks/DAO/loaders/governance/useAzoriusProposals.ts

View workflow job for this annotation

GitHub Actions / lint

'/home/runner/work/fractal-interface/fractal-interface/node_modules/@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/Azorius.d.ts' imported multiple times
import { VotedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC20Voting';

Check warning on line 5 in src/hooks/DAO/loaders/governance/useAzoriusProposals.ts

View workflow job for this annotation

GitHub Actions / lint

'/home/runner/work/fractal-interface/fractal-interface/node_modules/@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC20Voting.d.ts' imported multiple times
import { VotedEvent as ERC20VotedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC20Voting';

Check warning on line 6 in src/hooks/DAO/loaders/governance/useAzoriusProposals.ts

View workflow job for this annotation

GitHub Actions / lint

'ERC20VotedEvent' is defined but never used

Check warning on line 6 in src/hooks/DAO/loaders/governance/useAzoriusProposals.ts

View workflow job for this annotation

GitHub Actions / lint

'/home/runner/work/fractal-interface/fractal-interface/node_modules/@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC20Voting.d.ts' imported multiple times
import { VotedEvent as ERC721VotedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC721Voting';

Check warning on line 7 in src/hooks/DAO/loaders/governance/useAzoriusProposals.ts

View workflow job for this annotation

GitHub Actions / lint

'ERC721VotedEvent' is defined but never used
import { BigNumber } from 'ethers';

Check warning on line 8 in src/hooks/DAO/loaders/governance/useAzoriusProposals.ts

View workflow job for this annotation

GitHub Actions / lint

'BigNumber' is defined but never used
Expand Down Expand Up @@ -78,7 +80,11 @@ export const useAzoriusProposals = () => {
return [];
}

const loadProposalFromEvent = async ({ args }: ProposalCreatedEvent) => {
const loadProposalFromEvent = async (
{ args }: ProposalCreatedEvent,
votedEvents: VotedEvent[],
executedEvents: ProposalExecutedEvent[],
) => {
let proposalData;
if (args.metadata) {
const metadataEvent: ProposalMetadata = JSON.parse(args.metadata);
Expand All @@ -93,23 +99,40 @@ export const useAzoriusProposals = () => {
decodedTransactions,
};
}

return mapProposalCreatedEventToProposal(
strategyContract,
strategyType,
args.proposalId,
args.proposer,
azoriusContract,
provider,
votedEvents,
executedEvents,
proposalData,
);
};

const loadProposalSynchronously = async (
_proposalCreatedEvents: ProposalCreatedEvent[],
_loadProposalFromEvent: ({ args }: ProposalCreatedEvent) => Promise<AzoriusProposal>,
_loadProposalFromEvent: (
{ args }: ProposalCreatedEvent,
votedEvents: VotedEvent[],
executedEvents: ProposalExecutedEvent[],
) => Promise<AzoriusProposal>,
) => {
const votedEventFilter = strategyContract.filters.Voted();
const votedEvents = await strategyContract.queryFilter(votedEventFilter);

const executedEventFilter = azoriusContract.filters.ProposalExecuted();
const executedEvents = await azoriusContract.queryFilter(executedEventFilter);

for (const proposalCreatedEvent of _proposalCreatedEvents) {
const proposal = await _loadProposalFromEvent(proposalCreatedEvent);
const proposal = await _loadProposalFromEvent(
proposalCreatedEvent,
votedEvents,
executedEvents,
);
action.dispatch({
type: FractalGovernanceAction.SET_AZORIUS_PROPOSAL,
payload: proposal,
Expand Down
25 changes: 15 additions & 10 deletions src/utils/azorius.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
LinearERC20Voting,
LinearERC721Voting,
} from '@fractal-framework/fractal-contracts';
import { ProposalExecutedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/Azorius';
import { VotedEvent } from '@fractal-framework/fractal-contracts/dist/typechain-types/contracts/azorius/LinearERC20Voting';
import { SafeMultisigTransactionWithTransfersResponse } from '@safe-global/safe-service-client';
import { BigNumber } from 'ethers';
import { strategyFractalProposalStates } from '../constants/strategy';
Expand Down Expand Up @@ -90,12 +92,13 @@ export const getProposalVotesSummary = async (
}
};

export const getProposalVotes = async (
strategyContract: LinearERC20Voting | LinearERC721Voting,
const getProposalVotes = (
// strategyContract: LinearERC20Voting | LinearERC721Voting,
votes: VotedEvent[],
proposalId: BigNumber,
): Promise<ProposalVote[] | ERC721ProposalVote[]> => {
const voteEventFilter = strategyContract.filters.Voted();
const votes = await strategyContract.queryFilter(voteEventFilter);
): ProposalVote[] | ERC721ProposalVote[] => {
// const voteEventFilter = strategyContract.filters.Voted();
// const votes = await strategyContract.queryFilter(voteEventFilter);
const proposalVotesEvent = votes.filter(voteEvent => proposalId.eq(voteEvent.args.proposalId));

return proposalVotesEvent.map(({ args: { voter, voteType, ...rest } }) => {
Expand All @@ -114,16 +117,20 @@ export const mapProposalCreatedEventToProposal = async (
proposer: string,
azoriusContract: Azorius,
provider: Providers,
votedEvents: VotedEvent[],
executedEvents: ProposalExecutedEvent[],
data?: ProposalData,
) => {
const { endBlock, startBlock, abstainVotes, yesVotes, noVotes } =
await strategyContract.getProposalVotes(proposalId);
const quorum = await getQuorum(strategyContract, strategyType, proposalId);

const deadlineSeconds = await getTimeStamp(endBlock, provider);
const state = await getAzoriusProposalState(azoriusContract, proposalId);
const votes = await getProposalVotes(strategyContract, proposalId);
const block = await provider.getBlock(startBlock);

const state = await getAzoriusProposalState(azoriusContract, proposalId);
const votes = getProposalVotes(votedEvents, proposalId);

const votesSummary = {
yes: yesVotes,
no: noVotes,
Expand All @@ -135,9 +142,7 @@ export const mapProposalCreatedEventToProposal = async (

let transactionHash: string | undefined;
if (state === FractalProposalState.EXECUTED) {
const proposalExecutedFilter = azoriusContract.filters.ProposalExecuted();
const proposalExecutedEvents = await azoriusContract.queryFilter(proposalExecutedFilter);
const executedEvent = proposalExecutedEvents.find(event =>
const executedEvent = executedEvents.find(event =>
BigNumber.from(event.args[0]).eq(proposalId),
);
transactionHash = executedEvent?.transactionHash;
Expand Down

0 comments on commit a176e28

Please sign in to comment.