This guide will walk you through the process of deploying an ERC721 token on a local blockchain using Remix IDE and interacting with it via MetaMask.
Step 1: Set up Remix IDE
- Open Remix IDE in your browser at https://remix.ethereum.org/
- Create a new file named
MyNFT.sol
in the File Explorer
Step 2: Write the ERC721 Token Contract
Paste the following code into MyNFT.sol
:
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract MyNFT is ERC721 {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721("MyNFT", "MNFT") {}
function mintNFT(address recipient) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
return newItemId;
}
}
Step 3: Compile the Contract
- Click on the “Solidity Compiler” tab in the left sidebar
- Select the appropriate compiler version (0.8.0 or higher)
- Click “Compile MyNFT.sol”
Step 4: Deploy the Contract
- Switch to the “Deploy & Run Transactions” tab
- Set the environment to “Injected Provider - MetaMask”
- Ensure MetaMask is connected to your local blockchain
- Click “Deploy” and confirm the transaction in MetaMask
Step 5: Mint an NFT
- After deployment, you’ll see the contract functions in the Remix IDE
- Find the “mintNFT” function
- Enter your address (or any recipient address) in the input field
- Click “transact” and confirm the transaction in MetaMask
Step 6: View the NFT in MetaMask
Unlike ERC20 tokens, ERC721 tokens (NFTs) are not typically added to MetaMask in the same way. However, you can still interact with them:
- Copy the deployed contract address
- In MetaMask, go to the “NFTs” tab (you may need to enable this in settings)
- Click “Import NFTs”
- Paste the contract address and enter the token ID (should be 1 for the first minted NFT)
- Click “Add”
Note that MetaMask may not display the NFT visually if it doesn’t have associated metadata or an image URL.
Remember that each NFT is unique, so you’re dealing with individual token IDs rather than fungible amounts like with ERC20 tokens.