ERC-721 is a standard for creating unique digital items called NFTs on the blockchain. It helps make sure these items can be owned, transferred, and tracked easily.
ERC-721 implementation in Blockchain / Solidity
pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract MyNFT is ERC721 { constructor() ERC721("MyNFT", "MNFT") {} function mint(address to, uint256 tokenId) public { _mint(to, tokenId); } }
The contract inherits from OpenZeppelin's ERC721 implementation to save time and avoid errors.
The mint function creates a new unique token and assigns it to an owner.
pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract SimpleNFT is ERC721 { constructor() ERC721("SimpleNFT", "SNFT") {} function mintToken(address to, uint256 tokenId) public { _mint(to, tokenId); } }
pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; contract MetadataNFT is ERC721URIStorage { constructor() ERC721("MetadataNFT", "MNFT") {} function mintToken(address to, uint256 tokenId, string memory uri) public { _mint(to, tokenId); _setTokenURI(tokenId, uri); } }
This contract lets anyone mint a new unique NFT. Each token has a unique ID starting from 0 and increasing by 1.
pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract MyNFT is ERC721 { uint256 public nextTokenId; constructor() ERC721("MyNFT", "MNFT") {} function mint() public { _mint(msg.sender, nextTokenId); nextTokenId++; } }
Always use a well-tested library like OpenZeppelin to avoid security issues.
Each token ID must be unique to represent a unique item.
Minting functions can be restricted to certain users for control.
ERC-721 lets you create unique tokens called NFTs.
Use OpenZeppelin's ERC721 contract to simplify your code.
Minting creates new tokens with unique IDs assigned to owners.
Practice
What is the main purpose of the ERC-721 standard in blockchain?
Solution
Step 1: Understand ERC-721 token type
ERC-721 defines a standard for unique tokens, unlike fungible tokens which are identical.Step 2: Identify the purpose of ERC-721
It is used to create NFTs, which are unique digital assets on the blockchain.Final Answer:
To create unique, non-fungible tokens (NFTs) -> Option CQuick Check:
ERC-721 = Unique NFTs [OK]
- Confusing ERC-721 with ERC-20 fungible tokens
- Thinking ERC-721 manages contract ownership
- Assuming ERC-721 speeds up transactions
Which of the following is the correct way to inherit OpenZeppelin's ERC721 contract in Solidity?
contract MyNFT is ? {}Solution
Step 1: Identify the correct base contract for NFTs
ERC721 is the OpenZeppelin contract for NFTs, ERC20 is for fungible tokens.Step 2: Confirm inheritance syntax
Usingis ERC721correctly inherits the NFT functionality.Final Answer:
ERC721 -> Option BQuick Check:
Inherit ERC721 for NFTs [OK]
- Using ERC20 instead of ERC721
- Confusing Ownable with ERC721
- Trying to inherit SafeMath for NFTs
What will be the output of the following Solidity function when called?
function mintToken(address to, uint256 tokenId) public {
_mint(to, tokenId);
}
// Assume _mint works as expected and tokenId = 1, to = 0x123...What happens after calling mintToken(0x123..., 1)?
Solution
Step 1: Understand the _mint function
_mint creates a new token with the given tokenId and assigns it to the 'to' address.Step 2: Analyze the function call
Calling mintToken with address 0x123... and tokenId 1 creates that NFT for 0x123....Final Answer:
A new NFT with tokenId 1 is assigned to address 0x123... -> Option AQuick Check:
mintToken creates NFT for 'to' address [OK]
- Thinking _mint burns tokens
- Assuming _mint transfers tokens
- Expecting syntax errors from correct code
Identify the error in this Solidity snippet for minting an ERC-721 token:
function mint(address to, uint256 tokenId) public {
_mint(tokenId, to);
}Solution
Step 1: Check _mint function parameter order
OpenZeppelin's _mint expects (address to, uint256 tokenId).Step 2: Compare with given code
The code uses _mint(tokenId, to), reversing the parameters.Final Answer:
_mint parameters are reversed; should be (to, tokenId) -> Option DQuick Check:
_mint(to, tokenId) correct order [OK]
- Swapping parameters of _mint
- Thinking _mint can't be public
- Confusing tokenId type
You want to create an ERC-721 contract that only allows the contract owner to mint new tokens. Which of these Solidity code snippets correctly enforces this?
contract MyNFT is ERC721, Ownable {
function mint(address to, uint256 tokenId) public {
?
_mint(to, tokenId);
}
}Solution
Step 1: Understand ownership check
Only the contract owner should mint, so check if msg.sender equals owner().Step 2: Analyze each require statement
require(msg.sender == owner(), "Not owner"); correctly checks sender is owner; others check wrong conditions.Final Answer:
require(msg.sender == owner(), "Not owner"); -> Option AQuick Check:
Owner check uses msg.sender == owner() [OK]
- Checking 'to' address instead of sender
- Allowing anyone to mint without checks
- Using wrong require conditions
Ready to prove it?
Pass the test and this pattern moves unknown
