// https://api.pinata.cloud/pinning/pinJSONToIPFS import config from '../config/config'; import { Env } from '../config/types'; import { ipfsConvertToGateway } from '../utils/ipfs'; import { getLocalStorageItem, setLocalStorageItem, } from '../utils/localStorage'; import { request } from './apiClient'; export const postTokenMetadata = async (requestBody: { photoCid: string; name: string; description: string; collectionId: string; tokenId: string; }): Promise => { const { photoCid, name, description } = requestBody; const metadata = { name, description, image: `ipfs://${photoCid}`, }; const response = await request( 'POST', '/pinning/pinJSONToIPFS', metadata, 'https://api.pinata.cloud/', { Authorization: 'Bearer ' + config.pinataJWTToken, } ); const body = response as { IpfsHash: string; //This is the IPFS multi-hash provided back for your content, PinSize: string; //This is how large (in bytes) the content you just pinned is, Timestamp: string; //This is the timestamp for your content pinning (represented in ISO 8601 format) }; return body.IpfsHash; }; // based on https://docs.opensea.io/docs/contract-level-metadata export const postCollectionMetadata = async ({ name, description, image, }: { name: string; description: string; image: string; external_link?: string; seller_fee_basis_points?: number; // Indicates a 1% seller fee. fee_recipient?: string; }): Promise => { let parsedImageUri = image; if (image && !image.startsWith('ipfs://')) { parsedImageUri = `ipfs://${image}`; } const response = await request( 'POST', '/pinning/pinJSONToIPFS', { name, description, image: parsedImageUri, }, 'https://api.pinata.cloud/', { Authorization: 'Bearer ' + config.pinataJWTToken, } ); const body = response as { IpfsHash: string; //This is the IPFS multi-hash provided back for your content, PinSize: string; //This is how large (in bytes) the content you just pinned is, Timestamp: string; //This is the timestamp for your content pinning (represented in ISO 8601 format) }; return body.IpfsHash; }; export const uploadPhoto = async (file: File): Promise => { let data = new FormData(); data.append('file', file); const response = await request( 'POST', '/pinning/pinFileToIPFS', data, 'https://api.pinata.cloud/', { Authorization: 'Bearer ' + config.pinataJWTToken, } ); const body = response as { IpfsHash: string; //This is the IPFS multi-hash provided back for your content, PinSize: string; //This is how large (in bytes) the content you just pinned is, Timestamp: string; //This is the timestamp for your content pinning (represented in ISO 8601 format) }; return body.IpfsHash; }; export const ipfsFetchTokenImageUrl = async ( tokenUri: string | null ): Promise => { try { const url = ipfsConvertToGateway(tokenUri); if (!tokenUri || !url) { return; } const cachedItems = getLocalStorageItem('cacheTokenImage') || {}; if (cachedItems[url] && config.env !== Env.development) { return cachedItems[url]; } const json = await ipfsFetchMetadataContent(url); if (!json?.image) { return; } const imageUrl = ipfsConvertToGateway(json?.image); if (imageUrl) { cachedItems[url] = imageUrl; setLocalStorageItem('cacheTokenImage', cachedItems); return imageUrl; } return; } catch (err) { return; } }; export const ipfsFetchMetadataContent = async (tokenIpfsUri: string | null) => { try { const url = ipfsConvertToGateway(tokenIpfsUri); if (!url) { return; } const response = await fetch(url, { method: 'GET', cache: 'force-cache', }); const json: { image: string; description: string; name: string } = await response.json(); return json; } catch (err) { return; } };