5/5
_Follow along the course with this video._ --- ### SVG NFT Flipping the Mood With the assurance that our tokenURI function is returning a correctly formatting string, for both our tokenURI itself _and_ our imageURI, I think we're ready to make this NFT dynamic! Because our SVGs are on-chain, this affords us the ability to easily swap between them by calling a function. Let's write that function now. Our first consideration should be that _only the owner_ of an NFT should be able to flip its mood. We can use the \_isApprovedOrOwner function, included within the ERC721 standard to verify this before our flipMood function execution. ```solidity function flipMood(uint256 tokenId) public { if(!_isApprovedOrOwner(msg.sender, tokenId)){ revert MoodNFT__CantFlipMoodIfNotOwner(); } } ``` Remember to create our new custom error at the start of the contract! `error MoodNFT__CantFlipMoodIfNotOwner();`. From here, we'll just check if it NFT is happy, and if so, make it sad, otherwise we'll make it happy. This will flip the NFT's mood regardless of it's current mood. With openzeppelin version 5.0.0 `_isApprovedOrOwner` was removed in favor of a new `_isAuthorized` function. However, this function has a downside. _WARNING: This function assumes that `owner` is the actual owner of `tokenId` and does not verify this assumption._ A more elegant solution is to check for approval and for the owner of the token separately. Therfore, we can use `getApproved()` and `ownerOf()` from openzeppelin contract in `ERC721.sol`. ```solidity function flipMood(uint256 tokenId) public view { if(getApproved(tokenId) != msg.sender && ownerOf(tokenId) != msg.sender){ revert MoodNFT__CantFlipMoodIfNotOwner(); } if(s_tokenIdToMood[tokenId] == Mood.HAPPY){ s_tokenIdToMood[tokenId] == Mood.SAD; } else{ s_tokenIdToMood[tokenId] == Mood.HAPPY; } } ``` ### Wrap Up This was easy enough, we've now got a function which an owner can use to flip the mood of their NFT. Wonderful! In the next lesson we'll walkthrough the creation of our deployment script!
A step-by-step walkthrough to Deploying SVG NFTs with Foundry: Dynamic On-Chain Metadata Script - Master Foundry scripting to deploy NFTs with on-chain SVGs by reading local files, encoding data URIs dynamically within the script, and verifying the entire process with unit and integration tests.
Previous lesson
Previous
Next lesson
Next
Give us feedback
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