1/5
_Follow along the course with this video._ --- ### SVG NFT Ok, we've gained lots of context and understand about data storage in general and the benefits of `SVGs` specifically. Let's begin creating our very own dynamic `MoodNFT` with its `SVG` art stored on-chain. At the core of the NFT we'll build is a `flipMood` function which allows the owner to flip their NFT between happy and sad images. ::image{src='/foundry-nfts/12-svg-nft/svg-nft1.png' style='width: 100%; height: auto;'} Start with creating the file `src/MoodNft.sol` and filling out the usual boilerplate. We're definitely getting good at this by now. ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.18; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract MoodNft is ERC721 { constructor() ERC721("Mood NFT", "MN"){} } ``` Looking good! We want to store the `SVG` art on chain, we're actually going to pass these to our `constructor` on deployment. ```solidity constructor(string memory sadSvg, string memory happySvg) ERC721("Mood NFT", "MN"){} ``` We know we'll need a `tokenCounter`, along with this let's declare our `sadSvg` and `happySvg` as storage variables as well. All together, before getting into our functions, things should look like this: ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.18; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract MoodNft is ERC721 { string private s_sadSvg; string private s_happySvg; uint256 private s_tokenCounter; constructor(string memory sadSvg, string memory happySvg) ERC721("Mood NFT", "MN"){ s_tokenCounter = 0; s_sadSvg = sadSvg; s_happySvg = happySvg; } } ``` Now we need a `mint` function, anyone should be able to call it, so it should definitely be `public`. This shouldn't be anything especially new to us so far. ```solidity function mintNft() public { _safeMint(msg.sender, s_tokenCounter); s_tokenCounter++; } ``` And now the moment of truth! As we write the `tokenURI` function, we know this is what defines what our NFT looks like and the metadata associated with it. Remember that we'll need to `override` this `virtual` function of the `ERC721` standard. ```solidity function tokenURI(uint256 tokenId) public view override returns (string memory){} ``` ### Wrap Up Our on-chain, dynamic, `SVG NFT` is slowly coming to life! In the next lesson, let's walk through the contents of our `tokenURI` function and how we can encode our `SVGs` in a way such that they can be reasonably stored on the blockchain. See you there!
A detailed guide to Building Fully On-Chain NFTs: Encoding SVG Images and Metadata - Explore creating truly decentralized ERC721 NFTs by storing both SVG images and JSON metadata directly on-chain. Learn to use Base64 encoding techniques and implement a dynamic `tokenURI` function returning fully on-chain data URIs.
Previous lesson
Previous
Next lesson
Next
Give us feedback
OpenZeppelin Updates
Last updated on May 31, 2024
Course Overview
About the course
Advanced smart contract development
How to develop a stablecoin
How to develop a DeFi protocol
How to develop a DAO
Advanced smart contracts testing
Fuzz testing
Manual verification
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Guest lecturers:
Last updated on May 20, 2025
Solidity Developer
Advanced FoundryDuration: 36min
Duration: 3h 06min
Duration: 5h 02min
Duration: 6h 02min
Duration: 2h 47min
Duration: 1h 23min
Duration: 4h 28min
Duration: 1h 19min
Duration: 1h 10min
Course Overview
About the course
Advanced smart contract development
How to develop a stablecoin
How to develop a DeFi protocol
How to develop a DAO
Advanced smart contracts testing
Fuzz testing
Manual verification
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Guest lecturers:
Last updated on May 20, 2025