@@ -3,12 +3,13 @@ import { ethers } from 'ethers';
33
44import { INFURA_ENDPOINT } from '../utils/config' ;
55import LRUCache from '../utils/cache' ;
6+ import { FALLBACK_IPFS_URL , IPFS_URL } from '../utils/nft' ;
67
78interface NFTMetadata {
89 [ index : string ] : unknown ;
910}
1011
11- const IPFS_URL = 'https://loopring.mypinata.cloud /ipfs/' ;
12+ // const IPFS_URL = 'https://ipfs. loopring.io /ipfs/';
1213
1314// Two caches need to maintained
1415// NFT URI cache {key-> token_address:nft_id}
@@ -89,6 +90,7 @@ const getNFTURI = async (nft) => {
8990} ;
9091
9192const getNFTMetadata = async ( uri , nft , isErrorFallback = false ) => {
93+
9294 const cacheKey = nft . id ;
9395 let cacheResult = metadataCache . get ( cacheKey ) ;
9496 if ( cacheResult ) {
@@ -102,10 +104,17 @@ const getNFTMetadata = async (uri, nft, isErrorFallback = false) => {
102104 } ;
103105 }
104106 try {
105- const metadata = await fetch ( uri . replace ( 'ipfs://' , IPFS_URL ) ) . then ( ( res ) => res . json ( ) ) ;
107+
108+ const metadata = await fetch ( uri . replace ( 'ipfs://' , IPFS_URL ) )
109+ . catch ( ( ) => {
110+
111+ return fetch ( uri . replace ( 'ipfs://' , FALLBACK_IPFS_URL ) )
112+ } )
113+ . then ( ( res ) => res . json ( ) ) ;
106114 metadataCache . set ( cacheKey , metadata ) ;
107115 return metadata ;
108116 } catch ( error ) {
117+
109118 if ( ! isErrorFallback ) {
110119 return getNFTMetadata ( `${ uri } /metadata.json` , nft , true ) ;
111120 }
@@ -132,7 +141,9 @@ const useCachedNFT = (nft) => {
132141 ...metadata ,
133142 uri : uri ?. replace ( 'ipfs://' , IPFS_URL ) ,
134143 image : metadata ?. image ?. replace ( 'ipfs://' , IPFS_URL ) ,
144+ fallBackImage : metadata ?. image ?. replace ( 'ipfs://' , FALLBACK_IPFS_URL ) ,
135145 animation_url : metadata ?. animation_url ?. replace ( 'ipfs://' , IPFS_URL ) ,
146+ fallBackAnimation_url : metadata ?. animation_url ?. replace ( 'ipfs://' , FALLBACK_IPFS_URL ) ,
136147 } ) ;
137148 } ) ( ) ;
138149 }
0 commit comments