Skip to content

Commit

Permalink
Merge pull request #281 from helius-labs/fix/token-extension-token-pi…
Browse files Browse the repository at this point in the history
…ctures

[Fix] Display Token Extensions Metadata Properly
  • Loading branch information
0xIchigo authored Jan 26, 2024
2 parents 5d85c4a + c7ac49f commit e615b8d
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/lib/components/providers/token-provider.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,51 @@
metadata.image = $deprecatedImage?.data;
}
const fetchJsonMetadata = async (jsonUri: string) => {
try {
const response = await fetch(jsonUri);
if (!response.ok) {
throw new Error(`Status ${response.status}`);
}
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
throw new TypeError('Received non-JSON content type');
}
const jsonData = await response.json();
return jsonData.image;
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error fetching or parsing JSON metadata:', error);
return '';
}
};
$: if (metadata.mintExtensions) {
const name = metadata.mintExtensions?.metadata?.name;
const jsonUri = metadata.mintExtensions?.metadata?.uri;
if (name) {
metadata.name = name;
}
if (jsonUri && jsonUri.endsWith('.json')) {
(async () => {
try {
const imageUrl = await fetchJsonMetadata(jsonUri);
if (imageUrl) {
metadata.image = imageUrl;
}
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error in fetchJsonMetadata:', error);
}
})();
} else if (jsonUri) {
metadata.image = jsonUri;
}
}
$: tokenIsLoading =
(address !== SOL && $asset?.isLoading) ||
(address !== SOL && status?.isLoading);
Expand Down

0 comments on commit e615b8d

Please sign in to comment.