Skip to content

Repository files navigation

Mood NFT

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.


Features

  • 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

Contract Overview

The MoodNft contract allows users to:

  1. Mint a Mood NFT
  2. Store a mood state (HAPPY or SAD)
  3. Flip the mood anytime if they own the NFT
  4. Dynamically update NFT metadata & image URI

Tech Stack

  • Solidity ^0.8.18
  • OpenZeppelin ERC721
  • Base64 Encoding
  • SVG NFTs

Contract Structure

State Variables

uint256 private s_tokenCounter;
string private s_sadSvgImageUri;
string private s_happySvgImageUri;

Stores:

  • NFT counter
  • Happy SVG URI
  • Sad SVG URI

Mood Enum

enum Mood {
    HAPPY,
    SAD
}

Defines available NFT moods.


Mood Mapping

mapping(uint256 => Mood) private s_tokenIdToMood;

Tracks the mood of each token.


Constructor

constructor(
    string memory sadSvgImageUri,
    string memory happySvgImageUri
)

Initializes the NFT collection and stores both SVG image URIs.


Functions

mintNft()

function mintNft() public

Mints a new NFT to the caller.

Behavior

  • Assigns NFT to msg.sender
  • Sets initial mood to HAPPY
  • Increments token counter

flipMood()

function flipMood(uint256 tokenId) public

Allows the NFT owner to switch between moods.

Access Control

Only the NFT owner can call this function.

Logic

  • HAPPY → SAD
  • SAD → HAPPY

tokenURI()

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

Example Metadata

{
  "name": "Mood NFT",
  "description": "An NFT that reflects your mood.",
  "attributes": [
    {
      "trait_type": "moodiness",
      "value": 100
    }
  ],
  "image": "data:image/svg+xml;base64,..."
}

How Dynamic NFTs Work Here

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.


Deployment

Install Dependencies

npm install @openzeppelin/contracts

Compile

Using Foundry:

forge build

Example Usage

Mint NFT

moodNft.mintNft();

Flip Mood

moodNft.flipMood(0);

Security Notes

  • Only NFT owners can change moods
  • Uses OpenZeppelin’s authorization checks
  • Metadata is fully on-chain
  • No centralized image hosting

License

MIT


Author

Chimezie Obi

Basic NFT

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.


Features

  • ERC721 compliant NFT
  • Simple NFT minting
  • Custom token metadata
  • Token URI storage
  • OpenZeppelin implementation
  • Beginner-friendly NFT contract

Contract Overview

The BasicNft contract allows users to:

  1. Mint NFTs
  2. Attach custom metadata URIs
  3. Retrieve NFT metadata through tokenURI()

Each NFT can point to:

  • IPFS metadata
  • Arweave metadata
  • Centralized API metadata
  • Image assets

Tech Stack

  • Solidity ^0.8.18
  • OpenZeppelin ERC721

Contract Structure

State Variables

uint256 private s_tokenCounter;
mapping(uint256 => string) private s_tokenIdToUri;

Purpose

  • s_tokenCounter

    • Tracks the next NFT token ID
  • s_tokenIdToUri

    • Stores metadata URI for each NFT

Constructor

constructor() ERC721("Dogie", "DOG")

Initializes the NFT collection with:

Property Value
Name Dogie
Symbol DOG

Functions

mintNft()

function mintNft(string memory tokenUri) public

Mints a new NFT with a custom metadata URI.

Parameters

Name Type Description
tokenUri string Metadata URI for the NFT

Behavior

  • Stores the token URI
  • Mints NFT to msg.sender
  • Increments token counter

tokenURI()

function tokenURI(uint256 tokenId)
    public
    view
    override
    returns (string memory)

Returns the metadata URI associated with a token.


Example Metadata

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 Mint Flow

1. Upload Metadata to IPFS

Example URI:

ipfs://QmXyz123456789/metadata.json

2. Mint NFT

basicNft.mintNft(
    "ipfs://QmXyz123456789/metadata.json"
);

Deployment

Install Dependencies

npm install @openzeppelin/contracts

Compile with Foundry

forge build

Security Notes

  • Uses OpenZeppelin ERC721 implementation
  • Safe minting with _safeMint()
  • No access restrictions on minting
  • Metadata URIs are user-provided

License

MIT


Author

Chimezie Obi

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages