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

Feature/decouple metaptr logic #573

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion app/src/store/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
GrantMetadataResolution,
GrantRoundMetadataResolution,
} from '@dgrants/types';
import { fetchMetaPtrs } from 'src/utils/data/ipfs';
import { fetchMetaPtrs } from 'src/utils/data/metaPtrs';
import { TokenInfo } from '@uniswap/token-lists';
import {
GRANT_REGISTRY_ADDRESS,
Expand Down
3 changes: 3 additions & 0 deletions app/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export const DefaultForageConfig: LocalForageConfig = {
version: 3,
};

// --- Constants ---
export const retrievalEndpoint = 'https://scopelift.b-cdn.net/ipfs';

// LocalForage keys
export const allGrantsKey = 'AllGrants';
export const allGrantRoundsKey = 'AllGrantRounds';
Expand Down
2 changes: 1 addition & 1 deletion app/src/utils/data/grantRounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { formatNumber, callMulticallContract, batchFilterCall, metadataId, recur
import { syncStorage } from 'src/utils/data/utils';
import { CLR, linear, InitArgs } from '@dgrants/dcurve';
import { filterContributionsByGrantId, filterContributionsByGrantRound } from './contributions';
import { getMetadata } from './ipfs';
import { getMetadata } from './metaPtrs';
// --- Constants ---
import {
START_BLOCK,
Expand Down
2 changes: 1 addition & 1 deletion app/src/utils/data/grants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { batchFilterCall, recursiveGraphFetch } from '../utils';
import { DGRANTS_CHAIN_ID } from '../chains';
import { Ref } from 'vue';
import { getAddress } from '../ethers';
import { getMetadata, fetchMetaPtrs } from './ipfs';
import { fetchMetaPtrs, getMetadata } from './metaPtrs';

// --- pull in the registry contract
const { grantRegistry } = useWalletStore();
Expand Down
111 changes: 2 additions & 109 deletions app/src/utils/data/ipfs.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
import { createIpfs } from '@dgrants/utils/src/ipfs';
import { GrantMetadata, GrantRoundMetadataResolution, MetaPtr } from '@dgrants/types';
import { getStorageKey, setStorageKey } from 'src/utils/data/utils';
import { decodeMetadataId, metadataId } from 'src/utils/utils';
import { BigNumber } from 'src/utils/ethers';
import { LocalForageData } from 'src/types';
import { Ref } from 'vue';

const retrievalEndpoint = 'https://scopelift.b-cdn.net/ipfs';

function assertIPFSPointer(logoPtr: MetaPtr | undefined) {
if (!logoPtr) throw new Error('assertIPFSPointer: logoPtr is undefined');
if (typeof logoPtr == 'string') {
logoPtr = decodeMetadataId(logoPtr);
}
const protocol = BigNumber.from(logoPtr.protocol).toString();
if (!['0', '1'].includes(protocol))
throw new Error(`assertIPFSPointer: Expected protocol ID of 0 or 1, found ${protocol}`);
}
import { GrantMetadata } from '@dgrants/types';
import { assertIPFSPointer } from 'src/utils/utils';

export const ipfs = createIpfs(import.meta.env.VITE_FLEEK_STORAGE_API_KEY);

Expand All @@ -37,94 +21,3 @@ export const uploadGrantMetadata = async ({ name, description, logoPtr, properti
const res = await ipfs.add(JSON.stringify({ name, description, logoPtr, properties }));
return res.cid;
};

/**
* Creates a url for a MetaPtr
* @param obj
* @param obj.cid CID of the grant
* @returns string
*/
export const getMetaPtr = ({ cid }: { cid: string | undefined }) => {
return `${retrievalEndpoint}/${cid}`;
};

/**
* Given a CID, formats the metadata pointer for compatibility with the contracts
* @param cid CID of the grant
* @returns string
*/
export const formatMetaPtr = (cid: string): MetaPtr => {
return {
protocol: 1, // IPFS has a protocol ID of 1
pointer: cid,
};
};

/**
* Resolves a metaPtr via fetch
* @param url URL of metaPtr
* @returns Object
*/
export const resolveMetaPtr = (url: string) => {
return fetch(url).then((res) => res.json());
};

/**
* @notice Helper method that fetches metadata for a Grant or GrantRound, and saves the data
* to the state as soon as it's received
* @param metaPtrs Array of IPFS MetaPtrs to resolve
* @param metadata Name of the store's ref to assign resolve metadata to
*/
export const fetchMetaPtrs = async (metaPtrs: MetaPtr[], metadata: Ref) => {
metaPtrs.forEach(assertIPFSPointer);

// check for any missing entries
const newMetadata = metaPtrs
.filter((_, i) => {
return !metadata.value[metadataId(metaPtrs[i])] || metadata.value[metadataId(metaPtrs[i])].status === 'error';
})
.reduce((prev, cur) => {
return {
...prev,
[metadataId(cur)]: { status: 'pending' },
};
}, {});
// when there is new metadata to load...
if (Object.keys(newMetadata).length) {
// save these pending metadata objects to state
metadata.value = { ...metadata.value, ...newMetadata };
// resolve metadata via metaPtr and update state (no need to wait for resolution)
Object.keys(newMetadata).forEach((id) => getMetadata(decodeMetadataId(id), metadata));
}

return metadata;
};

/**
* @notice Helper method that fetches and or returns metadata for a Grant or GrantRound, and saves the data
* to the state as soon as it's received
* @param metaPtr metadata pointer object
* @param metadata Ref store's ref to assign resolved metadata to
*/
export const getMetadata = async (metaPtr: MetaPtr, metadata: Ref) => {
assertIPFSPointer(metaPtr);
const id = metadataId(metaPtr);
const fillMetadata = {} as Record<string, GrantRoundMetadataResolution>;
try {
// save each individual ipfs result into storage
let data = await getStorageKey('ipfs-' + id);
if (!data) {
const url = getMetaPtr({ cid: metaPtr.pointer });
data = await resolveMetaPtr(url);
await setStorageKey('ipfs-' + id, data as LocalForageData);
}
fillMetadata[id] = { status: 'resolved', ...data };
} catch (e) {
fillMetadata[id] = { status: 'error' };
console.error(e);
}
// place the metadata
metadata.value = { ...metadata.value, ...fillMetadata };

return metadata.value[id];
};
86 changes: 86 additions & 0 deletions app/src/utils/data/metaPtrs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* 1. Functions in this file should allow uploading and fetching MetaPtr's.
* Inside these utility functions, we should detect the protocol, then curry the pointer off to the appropriate method
* to fetch/upload the data for the protocol in question. Currently, the only supported protocols should be 0
* (return null on fetch, no-op on upload) or 1 for IPFS, which should pull out the CID and pass it to the aforementioned IPFS utils.
* Any other protocol ID should throw.
*
* Questions:
* 1. What is purpose of the class?
* 2. What is the reason for change?
*/

import { GrantRoundMetadataResolution, MetaPtr } from '@dgrants/types';
import { assertIPFSPointer, decodeMetadataId, getMetaPtr, metadataId } from 'src/utils/utils';
import { getStorageKey, setStorageKey } from 'src/utils/data/utils';
import { LocalForageData } from 'src/types';
import { Ref } from 'vue';

/**
* Resolves a metaPtr via fetch
* @param url URL of metaPtr
* @returns Object
*/
export const resolveMetaPtr = (url: string) => {
return fetch(url).then((res) => res.json());
};

/**
* @notice Helper method that fetches and or returns metadata for a Grant or GrantRound, and saves the data
* to the state as soon as it's received
* @param metaPtr metadata pointer object
* @param metadata Ref store's ref to assign resolved metadata to
*/
export const getMetadata = async (metaPtr: MetaPtr, metadata: Ref) => {
assertIPFSPointer(metaPtr);
const id = metadataId(metaPtr);
const fillMetadata = {} as Record<string, GrantRoundMetadataResolution>;
try {
// save each individual ipfs result into storage
let data = await getStorageKey('ipfs-' + id);
if (!data) {
const url = getMetaPtr({ cid: metaPtr.pointer });
data = await resolveMetaPtr(url);
await setStorageKey('ipfs-' + id, data as LocalForageData);
}
fillMetadata[id] = { status: 'resolved', ...data };
} catch (e) {
fillMetadata[id] = { status: 'error' };
console.error(e);
}
// place the metadata
metadata.value = { ...metadata.value, ...fillMetadata };

return metadata.value[id];
};

/**
* @notice Helper method that fetches metadata for a Grant or GrantRound, and saves the data
* to the state as soon as it's received
* @param metaPtrs Array of IPFS MetaPtrs to resolve
* @param metadata Name of the store's ref to assign resolve metadata to
*/
export const fetchMetaPtrs = async (metaPtrs: MetaPtr[], metadata: Ref) => {
metaPtrs.forEach(assertIPFSPointer);

// check for any missing entries
const newMetadata = metaPtrs
.filter((_, i) => {
return !metadata.value[metadataId(metaPtrs[i])] || metadata.value[metadataId(metaPtrs[i])].status === 'error';
})
.reduce((prev, cur) => {
return {
...prev,
[metadataId(cur)]: { status: 'pending' },
};
}, {});
// when there is new metadata to load...
if (Object.keys(newMetadata).length) {
// save these pending metadata objects to state
metadata.value = { ...metadata.value, ...newMetadata };
// resolve metadata via metaPtr and update state (no need to wait for resolution)
Object.keys(newMetadata).forEach((id) => getMetadata(decodeMetadataId(id), metadata));
}

return metadata;
};
36 changes: 34 additions & 2 deletions app/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import { BatchFilterQuery, EtherscanGroup } from 'src/types';
import useWalletStore from 'src/store/wallet';
import { Grant, GrantRound, MetaPtr } from '@dgrants/types';
import { formatUnits } from 'src/utils/ethers';
import { ETH_ADDRESS } from 'src/utils/constants';
import { ETH_ADDRESS, retrievalEndpoint } from 'src/utils/constants';
import {
DEFAULT_PROVIDER,
ETHERSCAN_BASE_URL,
FILTER_BLOCK_LIMIT,
SUPPORTED_TOKENS_MAPPING,
WETH_ADDRESS,
} from 'src/utils/chains';
import { getMetaPtr } from 'src/utils/data/ipfs';
import { Ref } from 'vue';

// --- Formatters ---
Expand Down Expand Up @@ -478,3 +477,36 @@ Promise<any[]> => {
return await recursiveGraphFetch(url, key, query, fromBlock, [...before, ...json.data[key]]);
}
};

const supportedProtocols = ['1'];
const isSupportedProtocol = (protocol: string) => supportedProtocols.includes(protocol);

export function assertIPFSPointer(logoPtr: MetaPtr | undefined) {
if (!logoPtr) throw new Error('assertIPFSPointer: logoPtr is undefined');
const protocol = BigNumber.from(logoPtr.protocol).toString();

if (!isSupportedProtocol(protocol))
throw new Error(`assertIPFSPointer: Expected protocol ID of 0 or 1, found ${protocol}`);
}

/**
* Creates a url for a MetaPtr
* @param obj
* @param obj.cid CID of the grant
* @returns string
*/
export const getMetaPtr = ({ cid }: { cid: string | undefined }) => {
return `${retrievalEndpoint}/${cid}`;
};

/**
* Given a CID, formats the metadata pointer for compatibility with the contracts
* @param cid CID of the grant
* @returns string
*/
export const formatMetaPtr = (cid: string): MetaPtr => {
return {
protocol: 1, // IPFS has a protocol ID of 1
pointer: cid,
};
};
20 changes: 10 additions & 10 deletions app/src/views/GrantRegistryGrantDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
<SectionHeader title="Contributions" />
<div>
<BaseFilterNav :active="selectedRound" :items="contributionsNav" />
<div v-if="selectedRound == 0">
<div v-if="selectedRound === 0">
<div v-for="(contribution, index) in grantContributions" :key="Number(index)">
<ContributionRow :contribution="contribution" :donationToken="donationToken" />
</div>
Expand All @@ -119,7 +119,7 @@
<BaseHeader name="Edit Grant" :tagline="grantMetadata?.name" />
<BaseFilterNav :active="selectedEdit" :items="editNav" />

<div v-if="selectedEdit == 0" class="text-left">
<div v-if="selectedEdit === 0" class="text-left">
<form class="space-y-5 mb-20" @submit.prevent="saveEdits">
<!-- Grant name -->
<InputRow>
Expand Down Expand Up @@ -285,7 +285,9 @@ import { LOREM_IPSOM_TEXT, NO_LOGO_OBJECT } from 'src/utils/constants';
import { ContractTransaction } from 'src/utils/ethers';
import {
cleanTwitterUrl,
formatMetaPtr,
formatNumber,
getMetaPtr,
isDefined,
isValidAddress,
isValidGithub,
Expand Down Expand Up @@ -540,7 +542,7 @@ function useGrantDetail() {
website !== grantMetadata.value?.properties?.websiteURI ||
github !== grantMetadata.value?.properties?.githubURI ||
twitter !== cleanTwitterUrl(grantMetadata.value?.properties?.twitterURI) ||
logoURI !== ipfs.getMetaPtr({ cid: (grantMetadata.value?.logoPtr as MetaPtr).pointer });
logoURI !== getMetaPtr({ cid: (grantMetadata.value?.logoPtr as MetaPtr).pointer });

return areFieldsValid && areFieldsUpdated;
});
Expand All @@ -550,9 +552,7 @@ function useGrantDetail() {
if (isLogoValid.value) isUploadingLogo.value = true;
form.value.logo = logo && isLogoValid.value ? logo : undefined;
try {
form.value.logoURI = logo
? await ipfs.uploadFile(logo).then((cid) => ipfs.getMetaPtr({ cid: cid.toString() }))
: '';
form.value.logoURI = logo ? await ipfs.uploadFile(logo).then((cid) => getMetaPtr({ cid: cid.toString() })) : '';
isUploadingLogo.value = false;
} catch (err) {
isUploadingLogo.value = false;
Expand Down Expand Up @@ -593,7 +593,7 @@ function useGrantDetail() {
const isMetaPtrUpdated =
name !== gMetadata?.name ||
description !== gMetadata?.description ||
logoURI !== ipfs.getMetaPtr({ cid: (gMetadata?.logoPtr as MetaPtr).pointer }) ||
logoURI !== getMetaPtr({ cid: (gMetadata?.logoPtr as MetaPtr).pointer }) ||
website !== gMetadata?.properties?.websiteURI ||
github !== gMetadata?.properties?.githubURI ||
twitter !== cleanTwitterUrl(gMetadata?.properties?.twitterURI);
Expand All @@ -606,10 +606,10 @@ function useGrantDetail() {
const splitLogoURI = (logoURI as string).split('/');
cid = splitLogoURI[splitLogoURI.length - 1];
}
const logoPtr = cid ? ipfs.formatMetaPtr(cid) : NO_LOGO_OBJECT;
const logoPtr = cid ? formatMetaPtr(cid) : NO_LOGO_OBJECT;
metaPtr = await ipfs
.uploadGrantMetadata({ name, description, logoPtr, properties })
.then((cid) => ipfs.formatMetaPtr(cid.toString()));
.then((cid) => formatMetaPtr(cid.toString()));
}

if (owner !== g.owner && payee === g.payee && !isMetaPtrUpdated) {
Expand Down Expand Up @@ -649,7 +649,7 @@ function useGrantDetail() {
form.value.payee = grant.value?.payee || '';
form.value.name = grantMetadata.value?.name || '';
form.value.description = grantMetadata.value?.description || '';
form.value.logoURI = cid ? ipfs.getMetaPtr({ cid }) : 'placeholder_grant.svg';
form.value.logoURI = cid ? getMetaPtr({ cid }) : 'placeholder_grant.svg';
form.value.website = grantMetadata.value?.properties?.websiteURI || '';
form.value.github = grantMetadata.value?.properties?.githubURI || '';
form.value.twitter = cleanTwitterUrl(grantMetadata.value?.properties?.twitterURI) || '';
Expand Down
Loading