Skip to content

Commit

Permalink
token-extension-metadata Refactored off-chain metadata fetching with …
Browse files Browse the repository at this point in the history
…async/await and added err… (#358)

* Refactored off-chain metadata fetching with async/await and added error handling

- Replaced .then() with async/await for consistency and readability.
- Added a try/catch block for better error handling when fetching and parsing off-chain metadata.
- Utilized optional chaining for safer access to the URI property.

* fix(): syntax fix

---------

Co-authored-by: Onyewuchi Emeka <[email protected]>
  • Loading branch information
EmekaManuel and Onyewuchi Emeka authored Sep 4, 2024
1 parent 609d5d1 commit c138670
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions content/courses/token-extensions/token-extensions-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -1082,11 +1082,14 @@ const onChainMetadata = await getTokenMetadata(connection, mint.publicKey);
console.log("onchain metadata =====>", onChainMetadata);
// And we can even get the off-chain json now
if (onChainMetadata && onChainMetadata.uri) {
const offChainMetadata = await fetch(onChainMetadata.uri).then(res =>
res.json(),
);
console.log("Mint off-chain metadata =====>", offChainMetadata);
if (onChainMetadata?.uri) {
try {
const response = await fetch(onChainMetadata.uri);
const offChainMetadata = await response.json();
console.log("Mint off-chain metadata =====>", offChainMetadata);
} catch (error) {
console.error("Error fetching or parsing off-chain metadata:", error);
}
}
```

Expand Down Expand Up @@ -1290,11 +1293,14 @@ export default async function createNFTWithEmbeddedMetadata(
console.log("onchain metadata =====>", onChainMetadata);
// And we can even get the off-chain JSON now
if (onChainMetadata && onChainMetadata.uri) {
const offChainMetadata = await fetch(onChainMetadata.uri).then(res =>
res.json(),
);
console.log("Mint off-chain metadata =====>", offChainMetadata);
if (onChainMetadata?.uri) {
try {
const response = await fetch(onChainMetadata.uri);
const offChainMetadata = await response.json();
console.log("Mint off-chain metadata =====>", offChainMetadata);
} catch (error) {
console.error("Error fetching or parsing off-chain metadata:", error);
}
}
}
```
Expand Down

0 comments on commit c138670

Please sign in to comment.