This project demonstrates a fully on-chain NFT implementation where the NFT metadata, including an SVG image, is stored directly on the blockchain. The contract uses the ERC721URIStorage standard to allow metadata customization for each NFT.
- SVG Images Stored On-Chain: The NFT image is generated using an SVG, which is converted into a Base64-encoded string and stored on-chain.
- Fully On-Chain Metadata: The name, description, and image (encoded SVG) are part of the token URI and stored fully on-chain.
- OpenZeppelin Contracts: Uses OpenZeppelin's
ERC721URIStoragefor easy metadata management andOwnablefor ownership control.
OnChainNFT allows you to mint NFTs with SVG images that are stored directly on the blockchain. The contract generates the SVG image, encodes it in Base64, and adds it to the metadata JSON. The metadata is also Base64-encoded and stored fully on-chain, so no external resources (e.g., IPFS or centralized servers) are needed.
To get started, you need to clone the repository and install the necessary dependencies:
-
Clone the repository:
git clone https://github.com/Superior212/NFT-on-chain cd onchain-nft -
Install dependencies:
npm install
-
Install Hardhat and OpenZeppelin contracts:
npm install --save-dev hardhat @openzeppelin/contracts
You can modify the contract to change how the SVG image is generated or include additional metadata fields.
- Contract Files:
contracts/OnChainNFT.sol: The main NFT contract with on-chain SVG generation and Base64 encoding.contracts/libraries/Base64.sol: The Base64 library used to encode SVG and JSON data.
-
Compile the contract:
npx hardhat compile
-
Deploy the contract using Hardhat's deploy script. Ensure you have the correct network configuration in
hardhat.config.ts.npx hardhat ignition deploy OnChainNFTModule --network sepolia
Replace
<network-name>with the actual network, such assepoliaorgoerli. -
After deployment, your contract address will be printed to the console.
Only the contract owner can mint NFTs. Use the mint function to create an NFT with a custom SVG image:
function mint(string memory svg) public onlyOwner;- Call the
mintfunction by providing an SVG string:
npx hardhat console --network sepoliaconst contract = await ethers.getContractAt(
"OnChainNFT",
"0xYourContractAddress"
);
await contract.mint(
"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><circle cx='50' cy='50' r='40' fill='yellow'/></svg>"
);This will mint an NFT that renders a yellow circle. You can replace the SVG string with any valid SVG markup.
Once deployed, you can interact with the contract using various tools:
-
Open the Hardhat console:
npx hardhat console --network <network-name>
-
Interact with your contract:
const nftContract = await ethers.getContractAt( "OnChainNFT", "0xYourContractAddress" ); const totalSupply = await nftContract.totalSupply();
Use an NFT explorer such as OpenSea (if the network supports it) to see the on-chain metadata, or use a tool like Ethers.js or Web3.js to interact with the contract.
You can view the minted NFTs on OpenSea's testnets if supported. Use the following URL:
https://testnets.opensea.io/assets/<network-name>/<contract-address>/<token-id>
Replace <network-name>, <contract-address>, and <token-id> with the relevant values.
This project is licensed under the MIT License. See the LICENSE file for details.