Skip to content

Commit

Permalink
fix: added caching layer for getting proposals and payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Argeare5 committed Nov 18, 2024
1 parent d05fad4 commit cfd5a26
Show file tree
Hide file tree
Showing 17 changed files with 1,142 additions and 196 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
"test": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
},
"dependencies": {
"@bgd-labs/aave-address-book": "3.1.1",
"@bgd-labs/aave-governance-ui-helpers": "3.1.6",
"@bgd-labs/aave-address-book": "^4.3.1",
"@bgd-labs/aave-cli": "^1.1.10",
"@bgd-labs/aave-governance-ui-helpers": "^3.1.8",
"@bgd-labs/aave-v3-governance-cache": "^1.0.8",
"@bgd-labs/frontend-web3-utils": "1.3.1",
"@bgd-labs/react-web3-icons": "1.4.6",
"@emotion/cache": "^11.13.1",
Expand Down
970 changes: 956 additions & 14 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/app/page.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Metadata } from 'next';

import { ActiveItem } from '../components/ProposalsList/ActiveItem';
import { FinishedItem } from '../components/ProposalsList/FinishedItem';
import { PAGE_SIZE } from '../configs/configs';
import { metaTexts } from '../helpers/texts/metaTexts';
import { api } from '../trpc/server';

Expand All @@ -26,7 +25,6 @@ export default async function Page() {
...contractsConstants,
votingConfigs: configs,
proposalsCount: totalProposalsCount,
pageSize: PAGE_SIZE,
});

return (
Expand Down
2 changes: 1 addition & 1 deletion src/old/delegate/store/delegationSlice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IERC20_ABI } from '@bgd-labs/aave-address-book';
import { IERC20_ABI } from '@bgd-labs/aave-address-book/abis';
import { Asset, normalizeBN } from '@bgd-labs/aave-governance-ui-helpers';
import {
safeSdkOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/old/rpcSwitcher/store/rpcSwitcherSlice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IPayloadsControllerCore_ABI } from '@bgd-labs/aave-address-book';
import { IPayloadsControllerCore_ABI } from '@bgd-labs/aave-address-book/abis';
import {
blockLimit,
getPayloadsCreated,
Expand Down
2 changes: 1 addition & 1 deletion src/old/web3/services/delegationService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { IMetaDelegateHelper_ABI } from '@bgd-labs/aave-address-book';
import { IMetaDelegateHelper_ABI } from '@bgd-labs/aave-address-book/abis';
import { normalizeBN } from '@bgd-labs/aave-governance-ui-helpers';
import { IAaveTokenV3_ABI } from '@bgd-labs/aave-governance-ui-helpers/dist/abis/IAaveTokenV3';
import { IATokenWithDelegation_ABI } from '@bgd-labs/aave-governance-ui-helpers/dist/abis/IATokenWithDelegation';
Expand Down
2 changes: 1 addition & 1 deletion src/old/web3/services/govDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
IPayloadsControllerDataHelper_ABI,
IVotingMachineDataHelper_ABI,
IVotingMachineWithProofs_ABI,
} from '@bgd-labs/aave-address-book';
} from '@bgd-labs/aave-address-book/abis';
import {
BasicProposal,
blockLimit,
Expand Down
2 changes: 1 addition & 1 deletion src/requests/fetchInitialData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IGovernanceCore_ABI } from '@bgd-labs/aave-address-book';
import { IGovernanceCore_ABI } from '@bgd-labs/aave-address-book/abis';
import { Client } from 'viem';
import { readContract } from 'viem/actions';

Expand Down
91 changes: 55 additions & 36 deletions src/requests/fetchProposalsDataForList.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { IVotingPortal_ABI } from '@bgd-labs/aave-address-book/abis';
import { Client } from 'viem';
import { readContract } from 'viem/actions';

import { appConfig } from '../configs/appConfig';
import {
ContractsConstants,
InitialPayloadState,
Expand All @@ -20,6 +25,7 @@ export type FetchProposalsDataForListParams = Pick<
votingConfigs: VotingConfig[];
userAddress?: string;
representativeAddress?: string;
clients: Record<number, Client>;
};

export async function fetchProposalsDataForList({
Expand All @@ -36,20 +42,22 @@ export async function fetchProposalsDataForList({
'Error getting proposals data from API, using RPC fallback',
e,
);
const proposalsData = await getProposalsData(input);
const proposalsData = (await getProposalsData(input)).sort(
(a, b) => b.proposal.id - a.proposal.id,
);

const payloadsChainsWithIds: Record<number, number[]> = {};
const initialPayloads = proposalsData
.map((proposal) => {
return proposal.proposalData.payloads;
return proposal.proposal.payloads;
})
.flat();
const payloadsChains = initialPayloads
.map((payload) => payload.chain)
.filter((value, index, self) => self.indexOf(value) === index);
payloadsChains.forEach((chainId) => {
payloadsChainsWithIds[Number(chainId)] = initialPayloads
.filter((payload) => payload.chain === BigInt(chainId))
.filter((payload) => Number(payload.chain) === Number(chainId))
.map((payload) => payload.payloadId)
.filter((value, index, self) => self.indexOf(value) === index);
});
Expand All @@ -60,54 +68,64 @@ export async function fetchProposalsDataForList({
await getPayloadsData({
chainId: Number(chainId),
payloadsIds,
clients,
}),
),
)
).flat();

const proposalsWithPayloads = proposalsData.map((proposal) => {
const proposalPayloads = proposal.proposalData.payloads.map((payload) => {
const proposalPayloads = proposal.proposal.payloads.map((payload) => {
return payloadsData.filter(
(p) =>
Number(p.id) === payload.payloadId &&
p.chain === payload.chain &&
p.payloadsController === payload.payloadsController,
Number(p.payload.id) === Number(payload.payloadId) &&
Number(p.payload.data.chain) === Number(payload.chain) &&
p.payload.data.payloadsController === payload.payloadsController,
)[0];
});
const isProposalPayloadsFinished = proposalPayloads.every(
(payload) =>
payload && payload?.data.state > InitialPayloadState.Queued,
payload && payload?.payload.data.state > InitialPayloadState.Queued,
);
return {
proposal: {
...proposal,
isFinished:
proposal.proposalData.state === InitialProposalState.Executed
proposal.proposal.state === InitialProposalState.Executed
? isProposalPayloadsFinished
: proposal.proposalData.state > InitialProposalState.Executed,
: proposal.proposal.state > InitialProposalState.Executed,
},
payloads: proposalPayloads,
};
});

const activeIds = proposalsWithPayloads
.filter((item) => !item.proposal.isFinished)
.map((item) => item.proposal.id);
.map((item) => item.proposal.proposal.id);
const finishedIds = proposalsWithPayloads
.filter((item) => item.proposal.isFinished)
.map((item) => item.proposal.id);
.map((item) => item.proposal.proposal.id);

const proposalsForGetVotingData = activeIds.map((id) => {
const proposal = proposalsWithPayloads.filter(
(item) => item.proposal.id === id,
)[0].proposal;
return {
id: proposal.id,
votingChainId: Number(proposal.votingChainId),
snapshotBlockHash: proposal.proposalData.snapshotBlockHash,
};
});
const proposalsForGetVotingData = await Promise.all(
activeIds.map(async (id) => {
const proposal = proposalsWithPayloads.filter(
(item) => item.proposal.proposal.id === id,
)[0].proposal;
const votingChainId = await readContract(
clients[appConfig.govCoreChainId],
{
abi: IVotingPortal_ABI,
address: proposal.proposal.votingPortal,
functionName: 'VOTING_MACHINE_CHAIN_ID',
args: [],
},
);
return {
id: BigInt(proposal.proposal.id),
votingChainId: Number(votingChainId),
snapshotBlockHash: proposal.proposal.snapshotBlockHash,
};
}),
);

const votingProposalsData =
proposalsForGetVotingData.length > 0
Expand All @@ -124,45 +142,46 @@ export async function fetchProposalsDataForList({
? await Promise.all(
activeIds.map(async (id) => {
const data = proposalsWithPayloads.filter(
(item) => item.proposal.id === id,
(item) => item.proposal.proposal.id === id,
)[0];
const votingData = votingProposalsData.filter(
(data) => data.proposalData.id === id,
(data) => data.proposalData.id === BigInt(id),
)[0];
const votingConfig = votingConfigs.filter(
(config) =>
config.accessLevel === data.proposal.proposalData.accessLevel,
config.accessLevel === data.proposal.proposal.accessLevel,
)[0];
return formatActiveProposalData({
...input,
...votingConfig,
core: data.proposal,
payloads: data.payloads,
core: data.proposal.proposal,
payloads: data.payloads.map((payload) => payload.payload),
voting: votingData,
title: data.proposal.ipfs?.title,
});
}),
)
: [];

const finishedProposalsData = finishedIds.map((id) => {
const data = proposalsWithPayloads.filter(
(item) => item.proposal.id === id,
(item) => item.proposal.proposal.id === id,
)[0];
const votingConfig = votingConfigs.filter(
(config) =>
config.accessLevel === data.proposal.proposalData.accessLevel,
(config) => config.accessLevel === data.proposal.proposal.accessLevel,
)[0];
const { proposalState, finishedTimestamp } =
getStateAndTimestampForFinishedProposal({
...input,
...votingConfig,
core: data.proposal,
payloads: data.payloads,
core: data.proposal.proposal,
payloads: data.payloads.map((payload) => payload.payload),
});
return {
proposalId: Number(data.proposal.id),
title: `Proposal ${data.proposal.id}`, // TODO: should be proposal title
ipfsHash: data.proposal.proposalData.ipfsHash,
proposalId: Number(data.proposal.proposal.id),
title:
data.proposal.ipfs?.title ?? `Proposal #${data.proposal.proposal.id}`,
ipfsHash: data.proposal.proposal.ipfsHash,
state: {
state: proposalState,
timestamp: finishedTimestamp,
Expand Down
5 changes: 5 additions & 0 deletions src/requests/utils/cachingLayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { fallbackProvider } from '@bgd-labs/aave-v3-governance-cache/fallbackProvider';
import { githubPagesProvider } from '@bgd-labs/aave-v3-governance-cache/githubPagesProvider';
import { rpcProvider } from '@bgd-labs/aave-v3-governance-cache/rpcProvider';

export const cachingLayer = fallbackProvider(githubPagesProvider, rpcProvider);
Loading

1 comment on commit cfd5a26

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit was deployed on ipfs

Please sign in to comment.