@@ -6,6 +6,7 @@ import MintingContractJSON from '../artifacts/contracts/MitchMinterSupplyUpgrade
6
6
import { useContractRead } from 'wagmi' ;
7
7
import { constants } from './constants' ;
8
8
import { readContract } from '@wagmi/core' ;
9
+ import { nftData } from '../pages/store' ;
9
10
10
11
const ipfsGateways = constants . IPFS_GATEWAYS ! . split ( ',' ) ;
11
12
@@ -23,7 +24,7 @@ async function getDataFromGateways(path: any) {
23
24
throw new Error ( 'All gateways failed' ) ;
24
25
}
25
26
26
- export const getIpfsData = async ( tokenId : number , contractProps : MintingContractProps ) => {
27
+ export const getIpfsData = async ( tokenId : number , contractProps : MintingContractProps ) : Promise < nftData > => {
27
28
try {
28
29
const tokenInfo = await readContract ( {
29
30
address : `0x${ contractProps . address } ` ,
@@ -35,118 +36,134 @@ export const getIpfsData = async (tokenId: number, contractProps: MintingContrac
35
36
36
37
const [ , tokenURI ] = tokenInfo as [ string , string ] ;
37
38
const tokenCID = tokenURI . replace ( "ipfs://" , "" ) ;
38
-
39
39
const response = await getDataFromGateways ( tokenCID ) ;
40
40
41
41
const metaData = {
42
42
name : response . data . name ,
43
43
description : response . data . description ,
44
44
image : response . data . image ,
45
45
tokenId : tokenId ,
46
- }
47
- ;
46
+ } ;
47
+
48
+ return metaData ;
48
49
49
- return metaData
50
50
} catch ( error ) {
51
51
console . error ( error ) ;
52
52
throw error ; // You can choose to rethrow the error or handle it here
53
53
}
54
54
} ;
55
55
56
56
57
- export const useParseIpfsData = ( tokenId : number , contractProps : MintingContractProps ) => {
58
- const debouncedContractProps = useDebounce ( contractProps , 500 ) ;
59
- const [ ipfsData , setIpfsData ] = useState ( {
60
- name : 'Loading...' ,
61
- description : 'Loading...' ,
62
- } ) ;
63
- const { data : tokenInfo , isSuccess : isTokenInfoSuccess , isError : isTokenInfoError , error : tokenInfoError } = useContractRead ( {
64
- address : `0x${ debouncedContractProps . address } ` ,
65
- abi : MintingContractJSON . abi ,
66
- functionName : 'getTokenInfo' ,
67
- args : [ tokenId ] ,
68
- chainId : debouncedContractProps . chainId
69
- } ) ;
70
- const retrieveIpfsData = useCallback ( async ( ) => {
71
- if ( isTokenInfoSuccess && tokenInfo ) {
72
- const [ , tokenURI ] = tokenInfo as [ string , string ]
73
- const tokenCID = tokenURI . replace ( "ipfs://" , "" ) ;
74
- try {
75
- const response = await getDataFromGateways ( tokenCID ) ;
76
- const metaData = {
77
- name : response . data . name ,
78
- description : response . data . description ,
79
- image : response . data . image ,
80
- } ;
81
- setIpfsData ( metaData ) ;
82
- } catch ( error ) {
83
- console . error ( error ) ;
84
- }
85
- } else if ( isTokenInfoError ) {
86
- console . log ( tokenInfoError )
87
- }
88
- } , [ isTokenInfoSuccess , isTokenInfoError , tokenInfo ] ) ;
57
+
58
+ // export const useParseIpfsData = (tokenId: number, contractProps: MintingContractProps) => {
59
+ // const debouncedContractProps = useDebounce(contractProps, 500);
60
+ // const [ipfsData, setIpfsData] = useState({
61
+ // name: 'Loading...',
62
+ // description: 'Loading...',
63
+ // });
64
+ // const {data: tokenInfo, isSuccess: isTokenInfoSuccess, isError: isTokenInfoError, error: tokenInfoError } = useContractRead({
65
+ // address: `0x${debouncedContractProps.address}`,
66
+ // abi: MintingContractJSON.abi,
67
+ // functionName: 'getTokenInfo',
68
+ // args: [tokenId],
69
+ // chainId: debouncedContractProps.chainId
70
+ // });
71
+ // const retrieveIpfsData = useCallback(async () => {
72
+ // if (isTokenInfoSuccess && tokenInfo) {
73
+ // const [, tokenURI] = tokenInfo as [string, string]
74
+ // const tokenCID = tokenURI.replace("ipfs://", "");
75
+ // try {
76
+ // const response = await getDataFromGateways(tokenCID);
77
+ // const metaData = {
78
+ // name: response.data.name,
79
+ // description: response.data.description,
80
+ // image: response.data.image,
81
+ // };
82
+ // setIpfsData(metaData);
83
+ // } catch (error) {
84
+ // console.error(error);
85
+ // }
86
+ // } else if (isTokenInfoError) {
87
+ // console.log(tokenInfoError)
88
+ // }
89
+ // }, [isTokenInfoSuccess, isTokenInfoError, tokenInfo]);
89
90
90
- useEffect ( ( ) => {
91
- retrieveIpfsData ( ) ;
92
- } , [ retrieveIpfsData ] ) ;
91
+ // useEffect(() => {
92
+ // retrieveIpfsData();
93
+ // }, [retrieveIpfsData]);
93
94
94
- return ipfsData ;
95
- } ;
96
-
97
-
98
- export const useParseIpfsImage = ( tokenId : number , contractProps : MintingContractProps ) => {
99
- const debouncedContractProps = useDebounce ( contractProps , 500 ) ;
100
- const [ ipfsImage , setIpfsImage ] = useState ( '' ) ;
101
- const { data : tokenInfo , isSuccess : isTokenInfoSuccess , isError : isTokenInfoError , error : tokenInfoError } = useContractRead ( {
102
- address : `0x${ debouncedContractProps . address } ` ,
103
- abi : MintingContractJSON . abi ,
104
- functionName : 'getTokenInfo' ,
105
- args : [ tokenId ] ,
106
- chainId : debouncedContractProps . chainId
107
- } ) ;
108
-
109
-
110
- // const IPFS_GATEWAYS = process.env.IPFS_GATEWAYS!.split(',');
111
-
95
+ // return ipfsData;
96
+ // };
112
97
113
- const getImageFromGateways = async ( cid : string , gateways : string [ ] ) => {
114
- for ( const gateway of gateways ) {
115
- try {
116
- const response = await axios . get ( `${ gateway } /${ cid } ` , { responseType : 'arraybuffer' } , { timeout : 5000 } ) ;
117
- return response ;
118
- } catch ( error ) {
119
- console . error ( `Failed to fetch from ${ gateway } ` , error ) ;
120
- }
98
+ export const getIpfsImage = async ( ipfsHash : string ) => {
99
+ const tokenCID = ipfsHash . replace ( "ipfs://" , "" ) ;
100
+ let imageResponse : any
101
+ for ( const gateway of ipfsGateways ) {
102
+ try {
103
+ const response = await axios . get ( `${ gateway } /${ tokenCID } ` , { responseType : 'arraybuffer' } , { timeout : 5000 } ) ;
104
+ imageResponse = await response ;
105
+ const imageBuffer = Buffer . from ( imageResponse . data , 'binary' ) ;
106
+ const imageSrc = 'data:image/jpeg;base64,' + imageBuffer . toString ( 'base64' ) ;
107
+ return imageSrc
108
+ } catch ( error ) {
109
+ console . error ( `Failed to fetch from ${ gateway } ` , error ) ;
121
110
}
122
- throw new Error ( `Failed to fetch data from all gateways for CID: ${ cid } ` ) ;
111
+ }
123
112
} ;
124
113
125
- const retrieveIpfsData = useCallback ( async ( ) => {
126
- if ( isTokenInfoSuccess && tokenInfo ) {
127
- const [ , tokenURI ] = tokenInfo as [ string , string ]
128
- const tokenCID = tokenURI . replace ( "ipfs://" , "" ) ;
129
- try {
130
- const response = await getDataFromGateways ( tokenCID ) ;
131
- const imgHash = response . data . image . replace ( "ipfs://" , "" ) ;
132
- const imageResponse = await getImageFromGateways ( imgHash , ipfsGateways ) ;
133
- const imageBuffer = Buffer . from ( imageResponse . data , 'binary' ) ;
134
- const imageSrc = 'data:image/jpeg;base64,' + imageBuffer . toString ( 'base64' ) ;
135
- setIpfsImage ( imageSrc ) ;
136
- } catch ( error ) {
137
- console . error ( error ) ;
138
- }
139
- } else if ( isTokenInfoError ) {
140
- console . log ( tokenInfoError )
141
- }
142
- } , [ ipfsGateways , isTokenInfoSuccess , isTokenInfoError ] ) ;
143
-
144
- useEffect ( ( ) => {
145
- retrieveIpfsData ( ) ;
146
- } , [ retrieveIpfsData ] ) ;
147
114
148
- return ipfsImage ;
149
- } ;
115
+ // export const useParseIpfsImage = (tokenId: number, contractProps: MintingContractProps) => {
116
+ // const debouncedContractProps = useDebounce(contractProps, 200);
117
+ // const [ipfsImage, setIpfsImage] = useState('');
118
+ // const {data: tokenInfo, isSuccess: isTokenInfoSuccess, isError: isTokenInfoError, error: tokenInfoError } = useContractRead({
119
+ // address: `0x${debouncedContractProps.address}`,
120
+ // abi: MintingContractJSON.abi,
121
+ // functionName: 'getTokenInfo',
122
+ // args: [tokenId],
123
+ // chainId: debouncedContractProps.chainId
124
+ // });
125
+
126
+
127
+ // // const IPFS_GATEWAYS = process.env.IPFS_GATEWAYS!.split(',');
128
+
129
+
130
+ // const getImageFromGateways = async (cid: string, gateways: string[]) => {
131
+ // for (const gateway of gateways) {
132
+ // try {
133
+ // const response = await axios.get(`${gateway}/${cid}`, { responseType: 'arraybuffer' }, { timeout: 5000 });
134
+ // return response;
135
+ // } catch (error) {
136
+ // console.error(`Failed to fetch from ${gateway}`, error);
137
+ // }
138
+ // }
139
+ // throw new Error(`Failed to fetch data from all gateways for CID: ${cid}`);
140
+ // };
141
+
142
+ // const retrieveIpfsData = useCallback(async () => {
143
+ // if (isTokenInfoSuccess && tokenInfo) {
144
+ // const [, tokenURI] = tokenInfo as [string, string]
145
+ // const tokenCID = tokenURI.replace("ipfs://", "");
146
+ // try {
147
+ // const response = await getDataFromGateways(tokenCID);
148
+ // const imgHash = response.data.image.replace("ipfs://", "");
149
+ // const imageResponse = await getImageFromGateways(imgHash, ipfsGateways);
150
+ // const imageBuffer = Buffer.from(imageResponse.data, 'binary');
151
+ // const imageSrc = 'data:image/jpeg;base64,' + imageBuffer.toString('base64');
152
+ // setIpfsImage(imageSrc);
153
+ // } catch (error) {
154
+ // console.error(error);
155
+ // }
156
+ // } else if (isTokenInfoError) {
157
+ // console.log(tokenInfoError)
158
+ // }
159
+ // }, [ipfsGateways, isTokenInfoSuccess, isTokenInfoError]);
160
+
161
+ // useEffect(() => {
162
+ // retrieveIpfsData();
163
+ // }, [retrieveIpfsData]);
164
+
165
+ // return ipfsImage;
166
+ // };
150
167
151
168
152
169
0 commit comments