This tutorial will guide you through writing and deploying an ERC-721 (Non-Fungible Token) smart contract on the Ethereum blockchain using tools like Hardhat, Alchemy, and Metamask. By the end, you'll understand how to mint your own NFT without needing prior blockchain development experience.
Key Tools and Technologies
- Metamask: Browser-based Ethereum wallet for managing transactions
- Hardhat: Development environment for Ethereum software
- Alchemy: Blockchain developer platform with API access
- Solidity: Programming language for Ethereum smart contracts
- Ropsten Test Network: Ethereum test environment for development
Step-by-Step Guide
1. Setting Up Your Development Environment
- Create an Alchemy Account
Sign up for a free account at Alchemy to access Ethereum node infrastructure without running your own node. - Install Metamask
Download the browser extension and create an account. Switch to the Ropsten Test Network for this tutorial. - Get Test ETH
Use the Ropsten faucet to obtain fake ETH for testing.
2. Initializing Your Project
mkdir my-nft
cd my-nft
npm init -y
npm install --save-dev hardhat
npx hardhatSelect "Create an empty hardhat.config.js" when prompted.
3. Project Structure
Organize your folders:
mkdir contracts scriptscontracts/: Stores smart contract codescripts/: Contains deployment and interaction scripts
4. Writing the Smart Contract
Create contracts/MyNFT.sol with this ERC-721 implementation:
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.3;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyNFT is ERC721, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721("MyNFT", "NFT") {}
function mintNFT(address recipient, string memory tokenURI)
public onlyOwner
returns (uint256)
{
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
}5. Configuring the Project
Install required dependencies:
npm install @openzeppelin/contracts @nomiclabs/hardhat-ethers ethers dotenvUpdate hardhat.config.js:
require('dotenv').config();
require("@nomiclabs/hardhat-ethers");
module.exports = {
solidity: "0.7.3",
networks: {
ropsten: {
url: process.env.API_URL,
accounts: [process.env.PRIVATE_KEY]
}
}
};6. Deploying the Contract
Create scripts/deploy.js:
async function main() {
const MyNFT = await ethers.getContractFactory("MyNFT");
const myNFT = await MyNFT.deploy();
console.log("Contract deployed to:", myNFT.address);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});Run deployment:
npx hardhat run scripts/deploy.js --network ropstenFAQ Section
What is an ERC-721 token?
ERC-721 is the Ethereum standard for non-fungible tokens (NFTs), allowing creation of unique digital assets with individual properties.
Why use the Ropsten test network?
Ropsten provides a safe environment to test smart contracts without spending real ETH on gas fees.
How do I view my deployed contract?
Check your contract address on Ropsten Etherscan. ๐ Learn more about blockchain explorers
What's next after deployment?
In Part II, we'll cover minting NFTs with your contract, and Part III will explain how to view NFTs in Metamask.
Key Takeaways
- NFT smart contracts use the ERC-721 standard
- Test networks like Ropsten allow risk-free development
- Tools like Hardhat streamline contract compilation and deployment
- Alchemy provides reliable blockchain access without running nodes
๐ Explore advanced NFT development techniques to enhance your project further.