Day 99/100 : Developing NFT

#100DaysOfSolidity RWE 099 : “Develop NFT Smart Contracts”

Solidity Academy
4 min readSep 12, 2023

🚀 Buy me a coffee! ☕ http://buymeacoffee.com/solidity

Welcome back to the 100DaysOfSolidity series! We’re on Day 99, and today’s topic is all about diving deep into the world of NFTs (Non-Fungible Tokens) and how to develop smart contracts to create your own unique digital assets on the blockchain. 🌐🔗

#100DaysOfSolidity RWE 099 : “Develop NFT Smart Contracts”

But before we get started, make sure to follow us on Medium, Twitter, LinkedIn, and check out our free Solidity e-Books and “The Solidity Blueprint” on Amazon. We’ve got a treasure trove of resources for all your Solidity needs. 👇

📖 Medium: https://medium.com/@solidity101
🐦 Twitter: https://twitter.com/solidity101
🔗 LinkedIn Page: linkedin.com/company/solidity101
🌐 Stay Updated: https://linktr.ee/solidity101

Now, let’s dive into the fascinating world of NFTs and how you can create your very own NFT smart contracts with detailed steps and explanations!

Understanding NFTs: More Than Just Digital Collectibles 🖼️

NFTs, or Non-Fungible Tokens, have taken the digital world by storm. They represent ownership of unique digital assets and have applications far beyond just collectibles. From digital art and music to in-game assets and real estate, NFTs are revolutionizing how we perceive and transfer ownership of digital content.

What Makes NFTs Special? 🌟

Unlike cryptocurrencies like Bitcoin or Ethereum, NFTs are not interchangeable on a one-to-one basis. Each NFT has a distinct value and specific attributes, making them ideal for representing ownership of unique items.

The Power of Smart Contracts in NFTs 💪

At the heart of every NFT is a smart contract. Smart contracts are self-executing contracts with the terms of the agreement directly written into code. They automate tasks and remove the need for intermediaries, making them perfect for managing NFTs.

Let’s dive into the technicalities and explore how you can create NFTs using Solidity, Ethereum’s programming language for smart contracts. But first, make sure you have your development environment set up, including Solidity compiler, Ethereum wallet, and a code editor. If you’re new to this, don’t worry; we’ve got your back!

Setting Up Your Development Environment 🛠️

Before we start writing code, let’s make sure your environment is ready. We’ll use the Ethereum blockchain for this example.

1. Install Node.js and npm

Node.js and npm (Node Package Manager) are essential for running JavaScript-based tools and libraries. You can download and install them from nodejs.org

2. Install Truffle

Truffle is a development framework for Ethereum that makes it easier to write, test, and deploy smart contracts. Install it globally with the following command:

npm install -g truffle

3. Set Up a Development Blockchain

For local development and testing, we recommend using Ganache, a personal blockchain for Ethereum development. You can download it from trufflesuite.com/ganache

4. Create a New Truffle Project

Now, let’s create a new Truffle project. Open your terminal and run:

truffle init

5. Write Your NFT Smart Contract

In the newly created project directory, navigate to the `contracts` folder and create a new Solidity file, e.g., `MyNFT.sol`. Here’s a basic example of an NFT contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyNFT is ERC721Enumerable, Ownable {
constructor() ERC721("MyNFT", "MNFT") {}
function mint(address to, uint256 tokenId) public onlyOwner {
_mint(to, tokenId);
}
}

This contract extends the ERC721Enumerable and Ownable contracts, making it both an NFT and allowing the owner to mint new tokens.

6. Compile and Deploy Your Contract

Use Truffle to compile your contract:

truffle compile

Then, deploy it to your development blockchain:

truffle migrate — network development

7. Mint Your NFTs

Now, you can mint your NFTs by calling the `mint` function, which can only be accessed by the contract owner:

const MyNFT = artifacts.require("MyNFT");
module.exports = function (deployer) {
deployer.deploy(MyNFT).then(function (instance) {
instance.mint("0xYourAddress", 0); // Mint an NFT to your address with tokenId 0
});
};

Adding More Features to Your NFT Contract 🔍

Now that you’ve created a basic NFT contract, it’s time to explore additional features that can enhance your NFT project.

Metadata and IPFS 🖼️

NFTs often include metadata that provides additional information about the digital asset. You can use IPFS (InterPlanetary File System) to store and retrieve this metadata efficiently. Update your contract to include metadata:

string public baseTokenURI = "https://ipfs.io/ipfs/";
function setBaseURI(string memory _newBaseTokenURI) external onlyOwner {
baseTokenURI = _newBaseTokenURI;
}
function _baseURI() internal view virtual override returns (string memory) {
return baseTokenURI;
}

Royalties and Secondary Sales 💰

You can implement a royalty system to ensure that creators receive a percentage of each secondary sale. Add a royalties mapping and update the minting function:

mapping(uint256 => address) public tokenCreators;
mapping(uint256 => uint256) public tokenRoyalties;
function mintWithRoyalty(address to, uint256 tokenId, uint256 royaltyPercentage) public onlyOwner {
require(royaltyPercentage <= 100, "Royalty percentage must be <= 100");
_mint(to, tokenId);
tokenCreators[tokenId] = msg.sender;
tokenRoyalties[tokenId] = royaltyPercentage;
}
function _transfer(address from, address to,
uint256 tokenId) internal override {
super._transfer(from, to, tokenId);
if (tokenCreators[tokenId] != address(0)) {
address creator = tokenCreators[tokenId];
uint256 royalty = (msg.value * tokenRoyalties[tokenId]) / 100;
(bool success, ) = payable(creator).call{value: royalty}("");
require(success, "Royalty payment failed");
}
}

These additions allow creators to set royalties when minting, and royalties are automatically transferred to them on secondary sales.

Conclusion 🚀

NFTs have opened up exciting opportunities in the digital world, allowing creators and collectors to own and trade unique digital assets securely. Developing NFT smart contracts using Solidity and Ethereum is a powerful way to participate in this booming space.

Remember, this is just the beginning. You can enhance your NFT contract with additional features, such as royalties, metadata storage, and more. Keep exploring, learning, and building, and who knows, you might create the next viral NFT project!

So, grab your coffee, dive into Solidity, and start building your NFT empire! ☕📈

Don’t forget to follow us on Medium, Twitter, LinkedIn, and explore our Free Solidity e-Books and “The Solidity Blueprint” for in-depth knowledge. We’re here to support your journey into the world of Solidity and blockchain development. 💡📚

Happy coding, and stay tuned for more exciting content from #100DaysOfSolidity! 🚀📢

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/