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

60
dist/index.js vendored

@ -3351,13 +3351,24 @@ class BaseGovernanceService extends BaseEventsService {
} }
async getAllProposals() { async getAllProposals() {
const { events } = await this.updateEvents(); 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.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( const proposerNames = allProposers.reduce(
(event, index) => { (acc, address, index) => {
const { id, description: text } = event; 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 status = proposalStatus[index];
const { forVotes, againstVotes, executed, extended, state } = status; const { forVotes, againstVotes, executed, extended, state } = status;
const { title, description } = parseDescription(id, text); const { title, description } = parseDescription(id, text);
@ -3365,6 +3376,7 @@ class BaseGovernanceService extends BaseEventsService {
return { return {
...event, ...event,
title, title,
proposerName: proposerNames[proposer] || void 0,
description, description,
forVotes, forVotes,
againstVotes, againstVotes,
@ -3373,22 +3385,13 @@ class BaseGovernanceService extends BaseEventsService {
quorum, quorum,
state: proposalState[String(state)] state: proposalState[String(state)]
}; };
} });
);
} }
async getVotes(proposalId) { async getVotes(proposalId) {
const { events } = await this.getSavedEvents(); const { events } = await this.getSavedEvents();
const votedEvents = events.filter( const votedEvents = events.filter(
(e) => e.event === "Voted" && e.proposalId === proposalId (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 allVoters = [...new Set(votedEvents.map((e) => [e.from, e.voter]).flat())];
const names = await this.ReverseRecords.getNames(allVoters); const names = await this.ReverseRecords.getNames(allVoters);
const ensNames = allVoters.reduce( const ensNames = allVoters.reduce(
@ -3400,10 +3403,18 @@ class BaseGovernanceService extends BaseEventsService {
}, },
{} {}
); );
const votes = votedEvents.map((event) => {
const { from, voter } = event;
const { contact, message } = parseComment(this.Governance, event.input);
return { return {
votes, ...event,
ensNames contact,
message,
fromName: ensNames[from] || void 0,
voterName: ensNames[voter] || void 0
}; };
});
return votes;
} }
async getDelegatedBalance(ethAccount) { async getDelegatedBalance(ethAccount) {
const { events } = await this.getSavedEvents(); const { events } = await this.getSavedEvents();
@ -3418,11 +3429,24 @@ class BaseGovernanceService extends BaseEventsService {
} }
return true; 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 { return {
delegatedAccs, delegatedAccs,
undelegatedAccs, undelegatedAccs,
uniq, uniq,
uniqNames,
balances, balances,
balance: balances.reduce((acc, curr) => acc + curr, BigInt(0)) balance: balances.reduce((acc, curr) => acc + curr, BigInt(0))
}; };

60
dist/index.mjs vendored

@ -3330,13 +3330,24 @@ class BaseGovernanceService extends BaseEventsService {
} }
async getAllProposals() { async getAllProposals() {
const { events } = await this.updateEvents(); 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.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( const proposerNames = allProposers.reduce(
(event, index) => { (acc, address, index) => {
const { id, description: text } = event; 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 status = proposalStatus[index];
const { forVotes, againstVotes, executed, extended, state } = status; const { forVotes, againstVotes, executed, extended, state } = status;
const { title, description } = parseDescription(id, text); const { title, description } = parseDescription(id, text);
@ -3344,6 +3355,7 @@ class BaseGovernanceService extends BaseEventsService {
return { return {
...event, ...event,
title, title,
proposerName: proposerNames[proposer] || void 0,
description, description,
forVotes, forVotes,
againstVotes, againstVotes,
@ -3352,22 +3364,13 @@ class BaseGovernanceService extends BaseEventsService {
quorum, quorum,
state: proposalState[String(state)] state: proposalState[String(state)]
}; };
} });
);
} }
async getVotes(proposalId) { async getVotes(proposalId) {
const { events } = await this.getSavedEvents(); const { events } = await this.getSavedEvents();
const votedEvents = events.filter( const votedEvents = events.filter(
(e) => e.event === "Voted" && e.proposalId === proposalId (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 allVoters = [...new Set(votedEvents.map((e) => [e.from, e.voter]).flat())];
const names = await this.ReverseRecords.getNames(allVoters); const names = await this.ReverseRecords.getNames(allVoters);
const ensNames = allVoters.reduce( const ensNames = allVoters.reduce(
@ -3379,10 +3382,18 @@ class BaseGovernanceService extends BaseEventsService {
}, },
{} {}
); );
const votes = votedEvents.map((event) => {
const { from, voter } = event;
const { contact, message } = parseComment(this.Governance, event.input);
return { return {
votes, ...event,
ensNames contact,
message,
fromName: ensNames[from] || void 0,
voterName: ensNames[voter] || void 0
}; };
});
return votes;
} }
async getDelegatedBalance(ethAccount) { async getDelegatedBalance(ethAccount) {
const { events } = await this.getSavedEvents(); const { events } = await this.getSavedEvents();
@ -3397,11 +3408,24 @@ class BaseGovernanceService extends BaseEventsService {
} }
return true; 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 { return {
delegatedAccs, delegatedAccs,
undelegatedAccs, undelegatedAccs,
uniq, uniq,
uniqNames,
balances, balances,
balance: balances.reduce((acc, curr) => acc + curr, BigInt(0)) balance: balances.reduce((acc, curr) => acc + curr, BigInt(0))
}; };

60
dist/tornado.umd.js vendored

@ -59653,13 +59653,24 @@ class BaseGovernanceService extends BaseEventsService {
} }
async getAllProposals() { async getAllProposals() {
const { events } = await this.updateEvents(); 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.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( const proposerNames = allProposers.reduce(
(event, index) => { (acc, address, index) => {
const { id, description: text } = event; 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 status = proposalStatus[index];
const { forVotes, againstVotes, executed, extended, state } = status; const { forVotes, againstVotes, executed, extended, state } = status;
const { title, description } = parseDescription(id, text); const { title, description } = parseDescription(id, text);
@ -59667,6 +59678,7 @@ class BaseGovernanceService extends BaseEventsService {
return { return {
...event, ...event,
title, title,
proposerName: proposerNames[proposer] || void 0,
description, description,
forVotes, forVotes,
againstVotes, againstVotes,
@ -59675,22 +59687,13 @@ class BaseGovernanceService extends BaseEventsService {
quorum, quorum,
state: proposalState[String(state)] state: proposalState[String(state)]
}; };
} });
);
} }
async getVotes(proposalId) { async getVotes(proposalId) {
const { events } = await this.getSavedEvents(); const { events } = await this.getSavedEvents();
const votedEvents = events.filter( const votedEvents = events.filter(
(e) => e.event === "Voted" && e.proposalId === proposalId (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 allVoters = [...new Set(votedEvents.map((e) => [e.from, e.voter]).flat())];
const names = await this.ReverseRecords.getNames(allVoters); const names = await this.ReverseRecords.getNames(allVoters);
const ensNames = allVoters.reduce( const ensNames = allVoters.reduce(
@ -59702,10 +59705,18 @@ class BaseGovernanceService extends BaseEventsService {
}, },
{} {}
); );
const votes = votedEvents.map((event) => {
const { from, voter } = event;
const { contact, message } = parseComment(this.Governance, event.input);
return { return {
votes, ...event,
ensNames contact,
message,
fromName: ensNames[from] || void 0,
voterName: ensNames[voter] || void 0
}; };
});
return votes;
} }
async getDelegatedBalance(ethAccount) { async getDelegatedBalance(ethAccount) {
const { events } = await this.getSavedEvents(); const { events } = await this.getSavedEvents();
@ -59720,11 +59731,24 @@ class BaseGovernanceService extends BaseEventsService {
} }
return true; 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 { return {
delegatedAccs, delegatedAccs,
undelegatedAccs, undelegatedAccs,
uniq, uniq,
uniqNames,
balances, balances,
balance: balances.reduce((acc, curr) => acc + curr, BigInt(0)) 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 { export interface GovernanceProposals extends GovernanceProposalCreatedEvents {
title: string; title: string;
proposerName?: string;
forVotes: bigint; forVotes: bigint;
againstVotes: bigint; againstVotes: bigint;
executed: boolean; executed: boolean;
@ -697,6 +698,8 @@ export interface GovernanceProposals extends GovernanceProposalCreatedEvents {
export interface GovernanceVotes extends GovernanceVotedEvents { export interface GovernanceVotes extends GovernanceVotedEvents {
contact: string; contact: string;
message: string; message: string;
fromName?: string;
voterName?: string;
} }
export interface BaseGovernanceServiceConstructor extends Omit<BaseEventsServiceConstructor, 'contract' | 'type'> { export interface BaseGovernanceServiceConstructor extends Omit<BaseEventsServiceConstructor, 'contract' | 'type'> {
@ -845,14 +848,28 @@ export class BaseGovernanceService extends BaseEventsService<AllGovernanceEvents
async getAllProposals(): Promise<GovernanceProposals[]> { async getAllProposals(): Promise<GovernanceProposals[]> {
const { events } = await this.updateEvents(); 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.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') as GovernanceProposalCreatedEvents[]).map( const proposerNames = allProposers.reduce(
(event, index) => { (acc, address, index) => {
const { id, description: text } = event; 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 status = proposalStatus[index];
@ -865,6 +882,7 @@ export class BaseGovernanceService extends BaseEventsService<AllGovernanceEvents
return { return {
...event, ...event,
title, title,
proposerName: proposerNames[proposer] || undefined,
description, description,
forVotes, forVotes,
againstVotes, againstVotes,
@ -873,32 +891,16 @@ export class BaseGovernanceService extends BaseEventsService<AllGovernanceEvents
quorum, quorum,
state: proposalState[String(state)], state: proposalState[String(state)],
}; };
}, });
);
} }
async getVotes(proposalId: number): Promise<{ async getVotes(proposalId: number): Promise<GovernanceVotes[]> {
votes: GovernanceVotes[];
ensNames: {
[key: string]: string;
};
}> {
const { events } = await this.getSavedEvents(); const { events } = await this.getSavedEvents();
const votedEvents = events.filter( const votedEvents = events.filter(
(e) => e.event === 'Voted' && (e as GovernanceVotedEvents).proposalId === proposalId, (e) => e.event === 'Voted' && (e as GovernanceVotedEvents).proposalId === proposalId,
) as GovernanceVotedEvents[]; ) 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 allVoters = [...new Set(votedEvents.map((e) => [e.from, e.voter]).flat())];
const names = await this.ReverseRecords.getNames(allVoters); const names = await this.ReverseRecords.getNames(allVoters);
@ -913,10 +915,21 @@ export class BaseGovernanceService extends BaseEventsService<AllGovernanceEvents
{} as { [key: string]: string }, {} as { [key: string]: string },
); );
const votes = votedEvents.map((event) => {
const { from, voter } = event;
const { contact, message } = parseComment(this.Governance, event.input);
return { return {
votes, ...event,
ensNames, contact,
message,
fromName: ensNames[from] || undefined,
voterName: ensNames[voter] || undefined,
}; };
});
return votes;
} }
async getDelegatedBalance(ethAccount: string) { async getDelegatedBalance(ethAccount: string) {
@ -941,12 +954,26 @@ export class BaseGovernanceService extends BaseEventsService<AllGovernanceEvents
return true; 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 { return {
delegatedAccs, delegatedAccs,
undelegatedAccs, undelegatedAccs,
uniq, uniq,
uniqNames,
balances, balances,
balance: balances.reduce((acc, curr) => acc + curr, BigInt(0)), balance: balances.reduce((acc, curr) => acc + curr, BigInt(0)),
}; };