@@ -38,19 +38,27 @@ export async function fetchTokenInfo({
3838 try {
3939 const response = await fetch ( uri ) ;
4040 if ( response . ok ) {
41- const result = tokenMetadataSchema . safeParse ( await response . json ( ) ) ;
41+ const contentType = response . headers . get ( "content-type" ) ;
4242
43- if ( result . success ) {
44- // Override with data from URI JSON
45- metadataAccountData = {
46- name : result . data . name ,
47- symbol : result . data . symbol ,
48- } ;
49- if ( result . data . image ) {
50- metadataUriJson = { image : result . data . image } ;
51- }
43+ // If the URI is an image, use it directly as the image URI
44+ if ( contentType ?. startsWith ( "image" ) ) {
45+ metadataUriJson = { image : uri } ;
5246 } else {
53- console . error ( "Invalid token metadata:" , result . error ) ;
47+ // Otherwise, try to parse it as JSON
48+ const result = tokenMetadataSchema . safeParse ( await response . json ( ) ) ;
49+
50+ if ( result . success ) {
51+ // Override with data from URI JSON
52+ metadataAccountData = {
53+ name : result . data . name ,
54+ symbol : result . data . symbol ,
55+ } ;
56+ if ( result . data . image ) {
57+ metadataUriJson = { image : result . data . image } ;
58+ }
59+ } else {
60+ console . error ( "Invalid token metadata:" , result . error ) ;
61+ }
5462 }
5563 }
5664 } catch ( error ) {
@@ -59,10 +67,36 @@ export async function fetchTokenInfo({
5967 }
6068
6169 // Create token info with all collected data
62- return createTokenInfo ( {
70+ const tokenInfo = createTokenInfo ( {
6371 mint : mint . address ,
6472 mintAccount : { decimals } ,
6573 metadataAccount : metadataAccountData ,
6674 metadataUriJson,
6775 } ) ;
76+
77+ // Fallback: Try to fetch from certified token list if no icon URL
78+ if ( ! tokenInfo . iconURL ) {
79+ const certifiedTokenInfoUrl = `https://raw.githubusercontent.com/CLBExchange/certified-token-list/refs/heads/master/101/${ mint . address } .json` ;
80+ try {
81+ const response = await fetch ( certifiedTokenInfoUrl ) ;
82+ if ( response . ok ) {
83+ const data = ( await response . json ( ) ) as {
84+ name : string ;
85+ symbol : string ;
86+ logoURI : string ;
87+ } ;
88+ if ( ! tokenInfo . name ) {
89+ tokenInfo . name = data . name ;
90+ }
91+ if ( ! tokenInfo . symbol ) {
92+ tokenInfo . symbol = data . symbol ;
93+ }
94+ tokenInfo . iconURL = data . logoURI ;
95+ }
96+ } catch ( error ) {
97+ console . warn ( "Could not fetch certified token info:" , error ) ;
98+ }
99+ }
100+
101+ return tokenInfo ;
68102}
0 commit comments