VotingMachineService.ts
1 import { 2 ChainId, 3 VotingMachineDataHelperService, 4 VotingMachineProposal, 5 } from '@aave/contract-helpers'; 6 import { ZERO_ADDRESS } from 'src/modules/governance/utils/formatProposal'; 7 import { governanceV3Config } from 'src/ui-config/governanceConfig'; 8 import { getProvider } from 'src/utils/marketsAndNetworksConfig'; 9 10 type VotingChainProposal = { 11 [chainId: number]: { 12 [votingMachineAddress: string]: Array<{ 13 id: number; 14 snapshotBlockHash: string; 15 }>; 16 }; 17 }; 18 19 export class VotingMachineService { 20 private getDataHelperService(chainId: ChainId) { 21 const provider = getProvider(chainId); 22 governanceV3Config.votingChainConfig; 23 return new VotingMachineDataHelperService( 24 governanceV3Config.votingChainConfig[chainId].votingPortalDataHelperAddress, 25 provider 26 ); 27 } 28 async getProposalsData( 29 proposals: Array<{ 30 id: number; 31 snapshotBlockHash: string; 32 chainId: number; 33 votingMachineAddress: string; 34 }>, 35 user?: string 36 ) { 37 const proposalsByVotingChainId: VotingChainProposal = proposals.reduce((acc, proposal) => { 38 const chainId = proposal.chainId; 39 const votingMachineAddress = proposal.votingMachineAddress; 40 41 if (!acc[chainId]) { 42 acc[chainId] = {}; 43 } 44 45 if (!acc[chainId][votingMachineAddress]) { 46 acc[chainId][votingMachineAddress] = []; 47 } 48 49 acc[chainId][votingMachineAddress].push({ 50 id: proposal.id, 51 snapshotBlockHash: proposal.snapshotBlockHash, 52 }); 53 return acc; 54 }, {} as VotingChainProposal); 55 56 const promises: Promise<VotingMachineProposal[]>[] = []; 57 Object.entries(proposalsByVotingChainId).forEach(([chainId, proposals]) => { 58 const chainIdKey = +chainId; 59 const dataHelperService = this.getDataHelperService(chainIdKey); 60 Object.entries(proposals).forEach(([votingMachineAddress, proposals]) => { 61 promises.push( 62 dataHelperService.getProposalsData(votingMachineAddress, proposals, user || ZERO_ADDRESS) 63 ); 64 }); 65 }); 66 67 const data = await Promise.all(promises); 68 69 const merged = data.reduce((acc, proposals) => { 70 return [...acc, ...proposals]; 71 }, [] as VotingMachineProposal[]); 72 73 return merged.sort((a, b) => +b.proposalData.id - +a.proposalData.id); 74 } 75 }