A dynamic on-chain SVG NFT built with Solidity and OpenZeppelin. This NFT changes its metadata and image based on the owner’s selected mood.
The contract stores two SVG image URIs (Happy & Sad) and allows NFT owners to flip between moods after minting.
- ERC721 compliant NFT
- Fully on-chain metadata
- Base64 encoded JSON metadata
- Dynamic NFT image rendering
- Mood switching functionality
- Owner-only mood updates
- Uses OpenZeppelin contracts
The MoodNft contract allows users to:
- Mint a Mood NFT
- Store a mood state (
HAPPYorSAD) - Flip the mood anytime if they own the NFT
- Dynamically update NFT metadata & image URI
- Solidity
^0.8.18 - OpenZeppelin ERC721
- Base64 Encoding
- SVG NFTs
uint256 private s_tokenCounter;
string private s_sadSvgImageUri;
string private s_happySvgImageUri;Stores:
- NFT counter
- Happy SVG URI
- Sad SVG URI
enum Mood {
HAPPY,
SAD
}Defines available NFT moods.
mapping(uint256 => Mood) private s_tokenIdToMood;Tracks the mood of each token.
constructor(
string memory sadSvgImageUri,
string memory happySvgImageUri
)Initializes the NFT collection and stores both SVG image URIs.
function mintNft() publicMints a new NFT to the caller.
- Assigns NFT to
msg.sender - Sets initial mood to
HAPPY - Increments token counter
function flipMood(uint256 tokenId) publicAllows the NFT owner to switch between moods.
Only the NFT owner can call this function.
HAPPY → SADSAD → HAPPY
function tokenURI(
uint256 tokenId
) public view override returns (string memory)Generates fully on-chain metadata encoded in Base64.
The metadata includes:
- Name
- Description
- Attributes
- Dynamic SVG image
{
"name": "Mood NFT",
"description": "An NFT that reflects your mood.",
"attributes": [
{
"trait_type": "moodiness",
"value": 100
}
],
"image": "data:image/svg+xml;base64,..."
}The NFT image changes depending on the current mood stored on-chain.
| Mood | Image |
|---|---|
| HAPPY | Happy SVG |
| SAD | Sad SVG |
Calling flipMood() updates the displayed NFT image automatically.
npm install @openzeppelin/contractsUsing Foundry:
forge buildmoodNft.mintNft();moodNft.flipMood(0);- Only NFT owners can change moods
- Uses OpenZeppelin’s authorization checks
- Metadata is fully on-chain
- No centralized image hosting
MIT
Chimezie Obi
A simple ERC721 NFT smart contract built with Solidity and OpenZeppelin.
This contract allows anyone to mint NFTs with custom metadata URIs. Each NFT stores its own token URI, making it easy to link images and metadata hosted on IPFS or decentralized storage platforms.
- ERC721 compliant NFT
- Simple NFT minting
- Custom token metadata
- Token URI storage
- OpenZeppelin implementation
- Beginner-friendly NFT contract
The BasicNft contract allows users to:
- Mint NFTs
- Attach custom metadata URIs
- Retrieve NFT metadata through
tokenURI()
Each NFT can point to:
- IPFS metadata
- Arweave metadata
- Centralized API metadata
- Image assets
- Solidity
^0.8.18 - OpenZeppelin ERC721
uint256 private s_tokenCounter;
mapping(uint256 => string) private s_tokenIdToUri;-
s_tokenCounter- Tracks the next NFT token ID
-
s_tokenIdToUri- Stores metadata URI for each NFT
constructor() ERC721("Dogie", "DOG")Initializes the NFT collection with:
| Property | Value |
|---|---|
| Name | Dogie |
| Symbol | DOG |
function mintNft(string memory tokenUri) publicMints a new NFT with a custom metadata URI.
| Name | Type | Description |
|---|---|---|
| tokenUri | string | Metadata URI for the NFT |
- Stores the token URI
- Mints NFT to
msg.sender - Increments token counter
function tokenURI(uint256 tokenId)
public
view
override
returns (string memory)Returns the metadata URI associated with a token.
Typical NFT metadata stored on IPFS:
{
"name": "Dogie NFT",
"description": "A cute Dogie NFT",
"image": "ipfs://QmExampleHash/image.png",
"attributes": [
{
"trait_type": "Cuteness",
"value": 100
}
]
}Example URI:
ipfs://QmXyz123456789/metadata.json
basicNft.mintNft(
"ipfs://QmXyz123456789/metadata.json"
);npm install @openzeppelin/contractsforge build- Uses OpenZeppelin ERC721 implementation
- Safe minting with
_safeMint() - No access restrictions on minting
- Metadata URIs are user-provided
MIT
Chimezie Obi