Frapper des NFT
Identifiants de jetons uniques
Frapper des NFT est une leçon Web3 & DApp Development Fundamentals gratuite sur CoddyKit. Ceci est la leçon 2 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Web3 & DApp Development Fundamentals, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Web3 & DApp Development Fundamentals comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
Creating Unique Tokens
Minting an NFT creates a brand new token with a unique tokenId and assigns it an owner. Unlike ERC-20 minting (which adds to a balance), each NFT mint produces a distinct, identifiable asset.
Ownership Mapping
The core NFT state is a mapping from tokenId to owner address, plus a balance count per owner.
<code>mapping(uint256 => address) private _owners;
mapping(address => uint256) private _balances;</code>A Basic Mint Function
Minting assigns the token to a recipient, increments their balance, and emits a Transfer from the zero address - just like ERC-20 mint conventions.
<code>function _mint(address to, uint256 tokenId) internal {
require(to != address(0), 'mint to zero');
require(_owners[tokenId] == address(0), 'already minted');
_owners[tokenId] = to;
_balances[to] += 1;
emit Transfer(address(0), to, tokenId);
}</code>Preventing Duplicate IDs
A token id can only be minted once. The check _owners[tokenId] == address(0) ensures you never overwrite an existing token.
<code>require(_owners[tokenId] == address(0), 'token already exists');</code>Auto-Incrementing IDs
To avoid id collisions, many contracts use a counter that increases with each mint, guaranteeing unique sequential ids.
<code>uint256 private _nextId;
function mint(address to) public {
uint256 id = _nextId;
_nextId += 1;
_mint(to, id);
}</code>Safe Minting
_safeMint behaves like _mint but, if the recipient is a contract, verifies it implements onERC721Received. This prevents NFTs from being locked in contracts that cannot handle them.
<code>function _safeMint(address to, uint256 tokenId) internal {
_mint(to, tokenId);
require(_checkOnERC721Received(to, tokenId), 'unsafe recipient');
}</code>Access Control
Like ERC-20, an unrestricted mint is dangerous. Gate minting behind ownership or a minter role so only authorized accounts can create tokens.
<code>function mint(address to, uint256 id) public onlyOwner {
_safeMint(to, id);
}</code>Public Mint with Payment
Many NFT drops let the public mint by paying a fee. The function is payable and checks the sent value.
<code>uint256 public price = 0.05 ether;
function publicMint() public payable {
require(msg.value >= price, 'insufficient payment');
_safeMint(msg.sender, _nextId);
_nextId += 1;
}</code>Setting Metadata at Mint
When minting you often record a per-token metadata URI so each NFT can have its own image and attributes.
<code>mapping(uint256 => string) private _tokenURIs;
function mintWithURI(address to, uint256 id, string memory uri) public onlyOwner {
_safeMint(to, id);
_tokenURIs[id] = uri;
}</code>Supply Limits
Collections usually cap the total number of NFTs. Enforce it with a max-supply check during minting.
<code>uint256 public constant MAX_SUPPLY = 10000;
function mint(address to) public onlyOwner {
require(_nextId < MAX_SUPPLY, 'sold out');
_safeMint(to, _nextId);
_nextId += 1;
}</code>Batch Minting
To mint several NFTs in one transaction, loop the mint logic. Be mindful of the block gas limit for large batches.
<code>function batchMint(address to, uint256 count) public onlyOwner {
for (uint256 i = 0; i < count; i++) {
_safeMint(to, _nextId);
_nextId += 1;
}
}</code>Quick Check
Test your understanding of minting NFTs.
Recap
You learned how to mint NFTs:
- Each mint assigns a unique tokenId to an owner and emits Transfer from address(0)
- Prevent duplicate ids; auto-increment counters keep ids unique
_safeMintprotects against locking NFTs in incompatible contracts- Add access control, optional payment, metadata, supply caps, and batch minting
Questions Fréquemment Posées
La leçon « Frapper des NFT » est-elle gratuite ?
Oui — le texte complet de « Frapper des NFT » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Web3 & DApp Development Fundamentals, passe à CoddyKit PRO. Le cours Web3 & DApp Development Fundamentals comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Frapper des NFT » ?
Identifiants de jetons uniques Tu pratiques Web3 & DApp Development Fundamentals avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Web3 & DApp Development Fundamentals ?
Aucune expérience préalable n'est requise. Web3 & DApp Development Fundamentals sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 2 sur 4.
Combien de temps prend la leçon « Frapper des NFT » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Web3 & DApp Development Fundamentals ?
Oui. Chaque leçon Web3 & DApp Development Fundamentals inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Norme ERC-721
- Frapper des NFT
- Métadonnées des jetons
- Places de marché