Improve Governance Functions

This commit is contained in:
Tornado Contrib 2024-10-18 14:17:00 +00:00
parent 6ab73c7d59
commit 75f57937e3
Signed by: tornadocontrib
GPG Key ID: 60B4DF1A076C64B1
6 changed files with 254 additions and 154 deletions

13
dist/events/base.d.ts vendored

@ -143,6 +143,7 @@ export declare const proposalState: {
};
export interface GovernanceProposals extends GovernanceProposalCreatedEvents {
title: string;
proposerName?: string;
forVotes: bigint;
againstVotes: bigint;
executed: boolean;
@ -153,6 +154,8 @@ export interface GovernanceProposals extends GovernanceProposalCreatedEvents {
export interface GovernanceVotes extends GovernanceVotedEvents {
contact: string;
message: string;
fromName?: string;
voterName?: string;
}
export interface BaseGovernanceServiceConstructor extends Omit<BaseEventsServiceConstructor, 'contract' | 'type'> {
Governance: Governance;
@ -173,16 +176,14 @@ export declare class BaseGovernanceService extends BaseEventsService<AllGovernan
fromBlock: number;
}): Promise<BaseEvents<AllGovernanceEvents>>;
getAllProposals(): Promise<GovernanceProposals[]>;
getVotes(proposalId: number): Promise<{
votes: GovernanceVotes[];
ensNames: {
[key: string]: string;
};
}>;
getVotes(proposalId: number): Promise<GovernanceVotes[]>;
getDelegatedBalance(ethAccount: string): Promise<{
delegatedAccs: string[];
undelegatedAccs: string[];
uniq: string[];
uniqNames: {
[key: string]: string;
};
balances: bigint[];
balance: bigint;
}>;

92
dist/index.js vendored

@ -3351,44 +3351,47 @@ class BaseGovernanceService extends BaseEventsService {
}
async getAllProposals() {
const { events } = await this.updateEvents();
const [QUORUM_VOTES, proposalStatus] = await Promise.all([
const proposalEvents = events.filter((e) => e.event === "ProposalCreated");
const allProposers = [...new Set(proposalEvents.map((e) => [e.proposer]).flat())];
const [QUORUM_VOTES, proposalStatus, proposerNameRecords] = await Promise.all([
this.Governance.QUORUM_VOTES(),
this.Aggregator.getAllProposals(this.Governance.target)
this.Aggregator.getAllProposals(this.Governance.target),
this.ReverseRecords.getNames(allProposers)
]);
return events.filter((e) => e.event === "ProposalCreated").map(
(event, index) => {
const { id, description: text } = event;
const status = proposalStatus[index];
const { forVotes, againstVotes, executed, extended, state } = status;
const { title, description } = parseDescription(id, text);
const quorum = (Number(forVotes + againstVotes) / Number(QUORUM_VOTES) * 100).toFixed(0) + "%";
return {
...event,
title,
description,
forVotes,
againstVotes,
executed,
extended,
quorum,
state: proposalState[String(state)]
};
}
const proposerNames = allProposers.reduce(
(acc, address, index) => {
if (proposerNameRecords[index]) {
acc[address] = proposerNameRecords[index];
}
return acc;
},
{}
);
return proposalEvents.map((event, index) => {
const { id, proposer, description: text } = event;
const status = proposalStatus[index];
const { forVotes, againstVotes, executed, extended, state } = status;
const { title, description } = parseDescription(id, text);
const quorum = (Number(forVotes + againstVotes) / Number(QUORUM_VOTES) * 100).toFixed(0) + "%";
return {
...event,
title,
proposerName: proposerNames[proposer] || void 0,
description,
forVotes,
againstVotes,
executed,
extended,
quorum,
state: proposalState[String(state)]
};
});
}
async getVotes(proposalId) {
const { events } = await this.getSavedEvents();
const votedEvents = events.filter(
(e) => e.event === "Voted" && e.proposalId === proposalId
);
const votes = votedEvents.map((event) => {
const { contact, message } = parseComment(this.Governance, event.input);
return {
...event,
contact,
message
};
});
const allVoters = [...new Set(votedEvents.map((e) => [e.from, e.voter]).flat())];
const names = await this.ReverseRecords.getNames(allVoters);
const ensNames = allVoters.reduce(
@ -3400,10 +3403,18 @@ class BaseGovernanceService extends BaseEventsService {
},
{}
);
return {
votes,
ensNames
};
const votes = votedEvents.map((event) => {
const { from, voter } = event;
const { contact, message } = parseComment(this.Governance, event.input);
return {
...event,
contact,
message,
fromName: ensNames[from] || void 0,
voterName: ensNames[voter] || void 0
};
});
return votes;
}
async getDelegatedBalance(ethAccount) {
const { events } = await this.getSavedEvents();
@ -3418,11 +3429,24 @@ class BaseGovernanceService extends BaseEventsService {
}
return true;
});
const balances = await this.Aggregator.getGovernanceBalances(this.Governance.target, uniq);
const [balances, uniqNameRecords] = await Promise.all([
this.Aggregator.getGovernanceBalances(this.Governance.target, uniq),
this.ReverseRecords.getNames(uniq)
]);
const uniqNames = uniq.reduce(
(acc, address, index) => {
if (uniqNameRecords[index]) {
acc[address] = uniqNameRecords[index];
}
return acc;
},
{}
);
return {
delegatedAccs,
undelegatedAccs,
uniq,
uniqNames,
balances,
balance: balances.reduce((acc, curr) => acc + curr, BigInt(0))
};

92
dist/index.mjs vendored

@ -3330,44 +3330,47 @@ class BaseGovernanceService extends BaseEventsService {
}
async getAllProposals() {
const { events } = await this.updateEvents();
const [QUORUM_VOTES, proposalStatus] = await Promise.all([
const proposalEvents = events.filter((e) => e.event === "ProposalCreated");
const allProposers = [...new Set(proposalEvents.map((e) => [e.proposer]).flat())];
const [QUORUM_VOTES, proposalStatus, proposerNameRecords] = await Promise.all([
this.Governance.QUORUM_VOTES(),
this.Aggregator.getAllProposals(this.Governance.target)
this.Aggregator.getAllProposals(this.Governance.target),
this.ReverseRecords.getNames(allProposers)
]);
return events.filter((e) => e.event === "ProposalCreated").map(
(event, index) => {
const { id, description: text } = event;
const status = proposalStatus[index];
const { forVotes, againstVotes, executed, extended, state } = status;
const { title, description } = parseDescription(id, text);
const quorum = (Number(forVotes + againstVotes) / Number(QUORUM_VOTES) * 100).toFixed(0) + "%";
return {
...event,
title,
description,
forVotes,
againstVotes,
executed,
extended,
quorum,
state: proposalState[String(state)]
};
}
const proposerNames = allProposers.reduce(
(acc, address, index) => {
if (proposerNameRecords[index]) {
acc[address] = proposerNameRecords[index];
}
return acc;
},
{}
);
return proposalEvents.map((event, index) => {
const { id, proposer, description: text } = event;
const status = proposalStatus[index];
const { forVotes, againstVotes, executed, extended, state } = status;
const { title, description } = parseDescription(id, text);
const quorum = (Number(forVotes + againstVotes) / Number(QUORUM_VOTES) * 100).toFixed(0) + "%";
return {
...event,
title,
proposerName: proposerNames[proposer] || void 0,
description,
forVotes,
againstVotes,
executed,
extended,
quorum,
state: proposalState[String(state)]
};
});
}
async getVotes(proposalId) {
const { events } = await this.getSavedEvents();
const votedEvents = events.filter(
(e) => e.event === "Voted" && e.proposalId === proposalId
);
const votes = votedEvents.map((event) => {
const { contact, message } = parseComment(this.Governance, event.input);
return {
...event,
contact,
message
};
});
const allVoters = [...new Set(votedEvents.map((e) => [e.from, e.voter]).flat())];
const names = await this.ReverseRecords.getNames(allVoters);
const ensNames = allVoters.reduce(
@ -3379,10 +3382,18 @@ class BaseGovernanceService extends BaseEventsService {
},
{}
);
return {
votes,
ensNames
};
const votes = votedEvents.map((event) => {
const { from, voter } = event;
const { contact, message } = parseComment(this.Governance, event.input);
return {
...event,
contact,
message,
fromName: ensNames[from] || void 0,
voterName: ensNames[voter] || void 0
};
});
return votes;
}
async getDelegatedBalance(ethAccount) {
const { events } = await this.getSavedEvents();
@ -3397,11 +3408,24 @@ class BaseGovernanceService extends BaseEventsService {
}
return true;
});
const balances = await this.Aggregator.getGovernanceBalances(this.Governance.target, uniq);
const [balances, uniqNameRecords] = await Promise.all([
this.Aggregator.getGovernanceBalances(this.Governance.target, uniq),
this.ReverseRecords.getNames(uniq)
]);
const uniqNames = uniq.reduce(
(acc, address, index) => {
if (uniqNameRecords[index]) {
acc[address] = uniqNameRecords[index];
}
return acc;
},
{}
);
return {
delegatedAccs,
undelegatedAccs,
uniq,
uniqNames,
balances,
balance: balances.reduce((acc, curr) => acc + curr, BigInt(0))
};

92
dist/tornado.umd.js vendored

@ -59653,44 +59653,47 @@ class BaseGovernanceService extends BaseEventsService {
}
async getAllProposals() {
const { events } = await this.updateEvents();
const [QUORUM_VOTES, proposalStatus] = await Promise.all([
const proposalEvents = events.filter((e) => e.event === "ProposalCreated");
const allProposers = [...new Set(proposalEvents.map((e) => [e.proposer]).flat())];
const [QUORUM_VOTES, proposalStatus, proposerNameRecords] = await Promise.all([
this.Governance.QUORUM_VOTES(),
this.Aggregator.getAllProposals(this.Governance.target)
this.Aggregator.getAllProposals(this.Governance.target),
this.ReverseRecords.getNames(allProposers)
]);
return events.filter((e) => e.event === "ProposalCreated").map(
(event, index) => {
const { id, description: text } = event;
const status = proposalStatus[index];
const { forVotes, againstVotes, executed, extended, state } = status;
const { title, description } = parseDescription(id, text);
const quorum = (Number(forVotes + againstVotes) / Number(QUORUM_VOTES) * 100).toFixed(0) + "%";
return {
...event,
title,
description,
forVotes,
againstVotes,
executed,
extended,
quorum,
state: proposalState[String(state)]
};
}
const proposerNames = allProposers.reduce(
(acc, address, index) => {
if (proposerNameRecords[index]) {
acc[address] = proposerNameRecords[index];
}
return acc;
},
{}
);
return proposalEvents.map((event, index) => {
const { id, proposer, description: text } = event;
const status = proposalStatus[index];
const { forVotes, againstVotes, executed, extended, state } = status;
const { title, description } = parseDescription(id, text);
const quorum = (Number(forVotes + againstVotes) / Number(QUORUM_VOTES) * 100).toFixed(0) + "%";
return {
...event,
title,
proposerName: proposerNames[proposer] || void 0,
description,
forVotes,
againstVotes,
executed,
extended,
quorum,
state: proposalState[String(state)]
};
});
}
async getVotes(proposalId) {
const { events } = await this.getSavedEvents();
const votedEvents = events.filter(
(e) => e.event === "Voted" && e.proposalId === proposalId
);
const votes = votedEvents.map((event) => {
const { contact, message } = parseComment(this.Governance, event.input);
return {
...event,
contact,
message
};
});
const allVoters = [...new Set(votedEvents.map((e) => [e.from, e.voter]).flat())];
const names = await this.ReverseRecords.getNames(allVoters);
const ensNames = allVoters.reduce(
@ -59702,10 +59705,18 @@ class BaseGovernanceService extends BaseEventsService {
},
{}
);
return {
votes,
ensNames
};
const votes = votedEvents.map((event) => {
const { from, voter } = event;
const { contact, message } = parseComment(this.Governance, event.input);
return {
...event,
contact,
message,
fromName: ensNames[from] || void 0,
voterName: ensNames[voter] || void 0
};
});
return votes;
}
async getDelegatedBalance(ethAccount) {
const { events } = await this.getSavedEvents();
@ -59720,11 +59731,24 @@ class BaseGovernanceService extends BaseEventsService {
}
return true;
});
const balances = await this.Aggregator.getGovernanceBalances(this.Governance.target, uniq);
const [balances, uniqNameRecords] = await Promise.all([
this.Aggregator.getGovernanceBalances(this.Governance.target, uniq),
this.ReverseRecords.getNames(uniq)
]);
const uniqNames = uniq.reduce(
(acc, address, index) => {
if (uniqNameRecords[index]) {
acc[address] = uniqNameRecords[index];
}
return acc;
},
{}
);
return {
delegatedAccs,
undelegatedAccs,
uniq,
uniqNames,
balances,
balance: balances.reduce((acc, curr) => acc + curr, BigInt(0))
};

File diff suppressed because one or more lines are too long

@ -686,6 +686,7 @@ function parseComment(Governance: Governance, calldata: string): { contact: stri
export interface GovernanceProposals extends GovernanceProposalCreatedEvents {
title: string;
proposerName?: string;
forVotes: bigint;
againstVotes: bigint;
executed: boolean;
@ -697,6 +698,8 @@ export interface GovernanceProposals extends GovernanceProposalCreatedEvents {
export interface GovernanceVotes extends GovernanceVotedEvents {
contact: string;
message: string;
fromName?: string;
voterName?: string;
}
export interface BaseGovernanceServiceConstructor extends Omit<BaseEventsServiceConstructor, 'contract' | 'type'> {
@ -845,60 +848,59 @@ export class BaseGovernanceService extends BaseEventsService<AllGovernanceEvents
async getAllProposals(): Promise<GovernanceProposals[]> {
const { events } = await this.updateEvents();
const [QUORUM_VOTES, proposalStatus] = await Promise.all([
const proposalEvents = events.filter((e) => e.event === 'ProposalCreated') as GovernanceProposalCreatedEvents[];
const allProposers = [...new Set(proposalEvents.map((e) => [e.proposer]).flat())];
const [QUORUM_VOTES, proposalStatus, proposerNameRecords] = await Promise.all([
this.Governance.QUORUM_VOTES(),
this.Aggregator.getAllProposals(this.Governance.target),
this.ReverseRecords.getNames(allProposers),
]);
return (events.filter((e) => e.event === 'ProposalCreated') as GovernanceProposalCreatedEvents[]).map(
(event, index) => {
const { id, description: text } = event;
const status = proposalStatus[index];
const { forVotes, againstVotes, executed, extended, state } = status;
const { title, description } = parseDescription(id, text);
const quorum = ((Number(forVotes + againstVotes) / Number(QUORUM_VOTES)) * 100).toFixed(0) + '%';
return {
...event,
title,
description,
forVotes,
againstVotes,
executed,
extended,
quorum,
state: proposalState[String(state)],
};
const proposerNames = allProposers.reduce(
(acc, address, index) => {
if (proposerNameRecords[index]) {
acc[address] = proposerNameRecords[index];
}
return acc;
},
{} as { [key: string]: string },
);
return proposalEvents.map((event, index) => {
const { id, proposer, description: text } = event;
const status = proposalStatus[index];
const { forVotes, againstVotes, executed, extended, state } = status;
const { title, description } = parseDescription(id, text);
const quorum = ((Number(forVotes + againstVotes) / Number(QUORUM_VOTES)) * 100).toFixed(0) + '%';
return {
...event,
title,
proposerName: proposerNames[proposer] || undefined,
description,
forVotes,
againstVotes,
executed,
extended,
quorum,
state: proposalState[String(state)],
};
});
}
async getVotes(proposalId: number): Promise<{
votes: GovernanceVotes[];
ensNames: {
[key: string]: string;
};
}> {
async getVotes(proposalId: number): Promise<GovernanceVotes[]> {
const { events } = await this.getSavedEvents();
const votedEvents = events.filter(
(e) => e.event === 'Voted' && (e as GovernanceVotedEvents).proposalId === proposalId,
) as GovernanceVotedEvents[];
const votes = votedEvents.map((event) => {
const { contact, message } = parseComment(this.Governance, event.input);
return {
...event,
contact,
message,
};
});
const allVoters = [...new Set(votedEvents.map((e) => [e.from, e.voter]).flat())];
const names = await this.ReverseRecords.getNames(allVoters);
@ -913,10 +915,21 @@ export class BaseGovernanceService extends BaseEventsService<AllGovernanceEvents
{} as { [key: string]: string },
);
return {
votes,
ensNames,
};
const votes = votedEvents.map((event) => {
const { from, voter } = event;
const { contact, message } = parseComment(this.Governance, event.input);
return {
...event,
contact,
message,
fromName: ensNames[from] || undefined,
voterName: ensNames[voter] || undefined,
};
});
return votes;
}
async getDelegatedBalance(ethAccount: string) {
@ -941,12 +954,26 @@ export class BaseGovernanceService extends BaseEventsService<AllGovernanceEvents
return true;
});
const balances = await this.Aggregator.getGovernanceBalances(this.Governance.target, uniq);
const [balances, uniqNameRecords] = await Promise.all([
this.Aggregator.getGovernanceBalances(this.Governance.target, uniq),
this.ReverseRecords.getNames(uniq),
]);
const uniqNames = uniq.reduce(
(acc, address, index) => {
if (uniqNameRecords[index]) {
acc[address] = uniqNameRecords[index];
}
return acc;
},
{} as { [key: string]: string },
);
return {
delegatedAccs,
undelegatedAccs,
uniq,
uniqNames,
balances,
balance: balances.reduce((acc, curr) => acc + curr, BigInt(0)),
};