A ERC721 SVG Solidity Smart contract built on Foundry development framework.
The "MoodNft" smart contract inherits functions from "OpenZeppelin's" ERC721 standard to mint NFT, Insted of the usual reliance on "IPFS" to store NFT metadata off-chain, here we use "SVG" (Scalable Vector Graphics) files as our NFT, which can be stored 100% on-chain. This smart contract has an additional function which makes the SVG NFT Dynamic, which means we can change the metadata of a deployed NFT, here in this case using flipMood() changes a " Happy " NFT to " Sad " NFT.
constructor(string memory mood1SvgImageUri, string memory mood2SvgImageUri) ERC721("MOOD NFT", "MNFT")
- Initializes the ERC-721 with name/symbol, sets the token counter to 0 and stores the two on-chain SVG image URIs for the two moods (
s_mood1SvgImageUri,s_mood2SvgImageUri).
function mintNft() public
- Mints a new token to
msg.senderusing_safeMintwith the currents_tokenCounterastokenId, sets that token’s mood toMood.M1, then incrementss_tokenCounter.
function flipMood(uint256 tokenId) public
- Toggles the stored
MoodfortokenIdbetween M1 ( happy ) and M2 ( sad ). First obtains the token owner viaownerOf(tokenId)and enforces authorization (_checkAuthorized) so only the owner or an approved operator may call it. Reverts if the token does not exist or caller is not authorized.
function tokenURI(uint256 tokenId) public view override returns (string memory)
- Returns a
data:application/json;base64, URI containing a base64-encoded JSON metadata blob. Theimagefield is populated with the SVG URI corresponding to the token’s current mood (s_mood1SvgImageUriors_mood2SvgImageUri). Calls intoname()for thenamefield; reverts iftokenIddoes not exist (inherited check).
Interact with "MoodNft" smart contract deployed on Sepolia through Etherscan -
https://sepolia.etherscan.io/address/0xD026EF89A18392A7A5D171159E2256625AA6711e#code