Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added fallback for ipfs and make first request from cache #92

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/proposal/page.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { IGovernanceCore_ABI } from '@bgd-labs/aave-address-book';
import {
CachedDetails,
getGovCoreConfigs,
getProposalMetadata,
getProposalState,
getVotingMachineProposalState,
ProposalMetadata,
Expand All @@ -23,6 +22,7 @@ import {
cachedVotesPath,
githubStartUrl,
} from '../../src/utils/cacheGithubLinks';
import { getProposalMetadata } from '../../src/utils/getProposalMetadata';
import { initialClients } from '../../src/utils/initialClients';

export const revalidate = 0;
Expand All @@ -44,7 +44,7 @@ export async function generateMetadata({

if (ipfsHash && proposalId) {
try {
const ipfsData = await getProposalMetadata(ipfsHash);
const ipfsData = await getProposalMetadata({ hash: ipfsHash });

return {
title: `${metaTexts.main}${metaTexts.proposalId(proposalId)}`,
Expand Down Expand Up @@ -197,7 +197,7 @@ export default async function ProposalPage({
try {
ipfsDataSSR =
cachedDetailsData?.ipfs ??
(ipfsHash ? await getProposalMetadata(ipfsHash) : undefined);
(ipfsHash ? await getProposalMetadata({ hash: ipfsHash }) : undefined);
} catch (e) {
ipfsDataSSR = cachedDetailsData?.ipfs;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"dependencies": {
"@bgd-labs/aave-address-book": "^2.26.0",
"@bgd-labs/aave-governance-ui-helpers": "^3.1.3",
"@bgd-labs/aave-governance-ui-helpers": "^3.1.5",
"@bgd-labs/frontend-web3-utils": "^1.2.1",
"@emotion/cache": "^11.11.0",
"@emotion/react": "^11.11.4",
Expand Down
16 changes: 3 additions & 13 deletions src/proposals/components/proposalList/ActiveProposalListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { WalletType } from '@bgd-labs/frontend-web3-utils';
import { Box, useTheme } from '@mui/system';
import React, { useEffect, useState } from 'react';
import { Hex, zeroAddress } from 'viem';
import { zeroAddress } from 'viem';

import { useStore } from '../../../store/ZustandStoreProvider';
import { Link } from '../../../ui';
Expand Down Expand Up @@ -44,7 +44,6 @@ export function ActiveProposalListItem({
const appClients = useStore((state) => state.appClients);
const ipfsDataErrors = useStore((state) => state.ipfsDataErrors);
const ipfsData = useStore((state) => state.ipfsData);
const getIpfsData = useStore((state) => state.getIpfsData);
let activeWallet = useStore((state) => state.activeWallet);

const proposal = proposalData.proposal;
Expand All @@ -55,22 +54,13 @@ export function ActiveProposalListItem({
const [isIPFSError, setIsIpfsError] = useState(
ipfsDataErrors[proposal.data.ipfsHash] && !ipfsData[proposal.data.ipfsHash],
);
const [ipfsErrorCount, setIpfsErrorCount] = useState(0);

const MAX_COUNT = 5;

useEffect(() => {
setIsIpfsError(
ipfsDataErrors[proposal.data.ipfsHash] &&
!ipfsData[proposal.data.ipfsHash],
);
if (isIPFSError && ipfsErrorCount <= MAX_COUNT) {
setTimeout(async () => {
getIpfsData([proposal.data.id], proposal.data.ipfsHash as Hex);
setIpfsErrorCount(ipfsErrorCount + 1);
}, 1000);
}
}, [ipfsErrorCount, isIPFSError, Object.keys(ipfsDataErrors).length]);
}, [isIPFSError, Object.keys(ipfsDataErrors).length]);

if (isForHelpModal) {
activeWallet = {
Expand Down Expand Up @@ -214,7 +204,7 @@ export function ActiveProposalListItem({
? `${theme.palette.$text} !important`
: theme.palette.$text,
}}>
{isIPFSError && ipfsErrorCount > MAX_COUNT ? (
{isIPFSError ? (
'Ipfs getting error'
) : isIPFSError ? (
<CustomSkeleton width={250} height={24} />
Expand Down
11 changes: 4 additions & 7 deletions src/proposals/store/proposalsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
checkHash,
ContractsConstants,
getProofOfRepresentative,
getProposalMetadata,
getProposalStepsAndAmounts,
getVotingMachineProposalState,
getVotingProofs,
Expand Down Expand Up @@ -38,7 +37,7 @@ import {
import { IUISlice } from '../../ui/store/uiSlice';
import { texts } from '../../ui/utils/texts';
import { appConfig } from '../../utils/appConfig';
import { ipfsGateway } from '../../utils/configs';
import { getProposalMetadata } from '../../utils/getProposalMetadata';
import { PAGE_SIZE } from '../../web3/services/govDataService';
import { ICreationFeesSlice } from '../../web3/store/creationFeesSlice';
import { ENSDataExists } from '../../web3/store/ensSelectors';
Expand Down Expand Up @@ -513,12 +512,10 @@ export const createProposalsSlice: StoreSlice<

filteredNewIpfsHashes.map(async (hash) => {
try {
const ipfsData = await getProposalMetadata(
const ipfsData = await getProposalMetadata({
hash,
ipfsGateway,
get().setIpfsDataErrors,
texts.other.fetchFromIpfsIncorrectHash,
);
setIpfsError: get().setIpfsDataErrors,
});

if (ipfsData) {
get().setIpfsDataErrors(hash, '', true);
Expand Down
7 changes: 6 additions & 1 deletion src/utils/cacheGithubLinks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { coreName } from './appConfig';

export const githubStartUrl = `https://raw.githubusercontent.com/bgd-labs/aave-governance-ui-helpers/main/cache/ui/${coreName}`;
export const githubInitialUrl =
'https://raw.githubusercontent.com/bgd-labs/aave-governance-ui-helpers/main/cache';
export const githubStartUrl = `${githubInitialUrl}/ui/${coreName}`;

export const listViewPath = '/list_view_proposals.json';
export const cachedProposalsIdsPath = '/cached_proposals_ids.json';
Expand All @@ -15,3 +17,6 @@ export const cachedVotesPath = (id: number) =>

export const cachedEventsPath = (id: number) =>
`/events/proposal_${id}_events.json`;

export const cachedIPFSDataPath = (ipfsHash: string) =>
`/ipfs/${ipfsHash}.json`;
6 changes: 6 additions & 0 deletions src/utils/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ import { CHAINS } from './chains';

// ipfs gateway to get proposals metadata
export const ipfsGateway = 'https://cloudflare-ipfs.com/ipfs';
export const fallbackGateways = [
'https://ipfs.io',
'https://ipfs.eth.aragon.network',
'https://dweb.link',
'https://ipfs.runfission.com',
];

export const chainInfoHelper = initChainInformationConfig(CHAINS);
53 changes: 53 additions & 0 deletions src/utils/getProposalMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
getProposalMetadata as baseGetProposalMetadata,
ProposalMetadata,
} from '@bgd-labs/aave-governance-ui-helpers';
import matter from 'gray-matter';

import { texts } from '../ui/utils/texts';
import { cachedIPFSDataPath, githubStartUrl } from './cacheGithubLinks';
import { fallbackGateways, ipfsGateway } from './configs';

export async function getProposalMetadata({
hash,
setIpfsError,
}: {
hash: string;
setIpfsError?: (ipfsHash: string, text?: string, remove?: boolean) => void;
}): Promise<ProposalMetadata | undefined> {
try {
const request = await fetch(`${githubStartUrl}${cachedIPFSDataPath(hash)}`);
if (request.ok) {
const response = await request.json();
const { content, data } = matter(response.description);
return {
...response,
ipfsHash: hash,
description: content,
...data,
};
} else {
console.error(
"Can't fetch cached ipfs data. Try to fetch from IPFS gateway",
);
return await baseGetProposalMetadata({
hash,
gateway: ipfsGateway,
setIpfsError,
errorText: texts.other.fetchFromIpfsError,
fallbackGateways,
});
}
} catch (e) {
console.error(
'An error occurred while fetching proposal metadata from IPFS, trying to request one more time.',
);
return await baseGetProposalMetadata({
hash,
gateway: ipfsGateway,
setIpfsError,
errorText: texts.other.fetchFromIpfsError,
fallbackGateways,
});
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1383,10 +1383,10 @@
resolved "https://registry.yarnpkg.com/@bgd-labs/aave-address-book/-/aave-address-book-2.26.0.tgz#d861386eb1fbc977a82c7f1c1ce4c1e6c30e77d0"
integrity sha512-1VjBruXMHEpPr7X6/m9XZH0fIxfNr2FgEvzhzCLb7Fb5opYU+4yTr7C+ATmQGGoy2I4PYDMr0kH6TMg3EpfvTw==

"@bgd-labs/aave-governance-ui-helpers@^3.1.3":
version "3.1.3"
resolved "https://registry.yarnpkg.com/@bgd-labs/aave-governance-ui-helpers/-/aave-governance-ui-helpers-3.1.3.tgz#aada6c1e043ca3e12097bcdd7d59ec1e396f6519"
integrity sha512-RiO46hwoz13309RqqQXKrxy/U+Ybb8DBYckKbZwiV7gXhbmBf2Hz6TRU78JsGPOMz0UUA7CRrb6V1c1OS48wqw==
"@bgd-labs/aave-governance-ui-helpers@^3.1.5":
version "3.1.5"
resolved "https://registry.yarnpkg.com/@bgd-labs/aave-governance-ui-helpers/-/aave-governance-ui-helpers-3.1.5.tgz#028bd728c379d567201a97c57ade28f197ef90c1"
integrity sha512-OX7kbA6+V6ot/XA3ue1HFnxyhRT8v2Yllah4njXr7VH+PxeAIs1i5cbK2cN6qJ6tPHAC/JGFkGaSHIxdkjB3TQ==
dependencies:
bs58 "^5.0.0"
dayjs "^1.11.10"
Expand Down
Loading