diff --git a/README.md b/README.md index 55de7e7..d64e757 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Copy-paste code snippets for common web3 tasks. Each recipe has EVM and Solana i | fetch-nft-collection | NFTs | | buy-nft | NFTs | | display-nft-metadata | NFTs | +| onchain-svg-nft | NFTs | | swap-tokens | DeFi | | provide-liquidity | DeFi | | stake-tokens | DeFi | diff --git a/recipes/onchain-svg-nft/README.md b/recipes/onchain-svg-nft/README.md new file mode 100644 index 0000000..3b8d068 --- /dev/null +++ b/recipes/onchain-svg-nft/README.md @@ -0,0 +1,53 @@ +# On-chain SVG NFT + +Decode and render fully on-chain SVG NFTs, read Chainlink price feeds, and mint dynamic NFTs whose metadata changes based on real-world data. + +## What this does + +Given an ERC-721 contract address and token ID, reads the base64-encoded `tokenURI`, decodes the JSON metadata, extracts the embedded SVG image, and renders it safely in a sandboxed iframe. Optionally reads a Chainlink price feed to demonstrate how dynamic NFTs react to on-chain state changes. + +## On-chain vs off-chain NFT storage + +| | Fully on-chain (SVG) | Off-chain (IPFS/Arweave) | Off-chain (HTTP) | +| -------------- | ------------------------------------------ | ---------------------------------- | -------------------------------- | +| **Storage** | SVG + JSON in contract bytecode | URI on-chain, data off-chain | URI on-chain, data on server | +| **Permanence** | Permanent — survives as long as chain runs | IPFS: fragile / Arweave: perm. | Server can go down | +| **Dynamic?** | Yes — contract generates metadata per call | No — content-addressed = immutable | Yes — server can change response | +| **Gas cost** | High (deploy) / Free (read) | Low (just store URI) | Low (just store URI) | +| **Examples** | Nouns, Autoglyphs, Loot | Bored Apes, Azuki | Early/low-budget projects | + +## How dynamic NFTs work + +A dynamic NFT contract generates its `tokenURI` response at read time instead of storing a fixed URI. The contract reads on-chain state (prices, timestamps, ownership history) and constructs different SVG/JSON based on that state. The metadata changes without any transaction — a view call returns different data each time conditions change. + +## Why EVM only + +On-chain SVG generation via `abi.encodePacked` inside `tokenURI()` is an EVM-specific pattern. Solana NFTs use Metaplex, which stores a URI pointing off-chain — it does not generate art inside the program. Chainlink price feeds are also EVM-native. For Solana oracle patterns, see the Pyth network documentation. + +## Security notes + +- **SVG XSS** — on-chain SVGs can contain `