Mint NFT
Introduction to minting an NFT on Flow
14 Oct 2022
Contributed by Flow Blockchain
This is for minting NFTS. It includes the minting resource that you can use in your smart contract, as well as the transaction to mint from the deployed account. It can be included in your smart contract along with the NFT Standard.
Smart Contract Example
The Recipe contract facilitates the minting of NFTs by leveraging the NFTMinter resource from the ExampleNFT contract, which implements the Flow NFT standard. To use the Recipe contract, the ExampleNFT contract must define an NFT resource with metadata like name, description, and thumbnail, and the recipient account must have a linked collection capable of receiving and storing NFTs. The contract uses the NFTMinter resource to create new tokens and deposits them directly into the recipient's collection.
To see how to link a Collection Interface to an account, check out the linking collections example: https://github.com/onflow/collection-for-holding-nfts.
Transaction Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import "NonFungibleToken"
import "ExampleNFT"
transaction {
let minter: &ExampleNFT.NFTMinter
let receiver: &{NonFungibleToken.CollectionPublic}
prepare(signer: auth(Storage, Capabilities) &Account) {
// Borrow the ExampleNFT.NFTMinter reference
self.minter = signer.storage.borrow<&ExampleNFT.NFTMinter>(
from: /storage/exampleNFTMinter
) ?? panic("Could not borrow a reference to the ExampleNFT NFTMinter")
// Borrow the signer's NFT collection reference
self.receiver = signer.storage.borrow<&{NonFungibleToken.CollectionPublic}>(
from: /storage/exampleNFTCollection
) ?? panic("Could not borrow a reference to the signer's NFT collection")
}
execute {
// Call the mintNFT function from the ExampleNFT.NFTMinter
let newNFT <- self.minter.mintNFT(
name: "Hardcoded NFT Name",
description: "This is a hardcoded description of the NFT.",
thumbnail: "https://example.com/hardcoded-thumbnail.png",
royalties: []
)
// Deposit the minted NFT into the recipient's collection
self.receiver.deposit(token: <-newNFT)
log("Minted and deposited an NFT into the signer's collection.")
}
}
This transaction is designed to mint an NFT using the minting resource stored in a specified account. It begins by identifying the recipient of the NFT and verifying that their account has the necessary capability to store NFTs in their collection. Once confirmed, the transaction borrows the minting resource from the account that holds it and uses it to mint a new NFT. The minted NFT is then deposited into the recipient's collection. This ensures that only accounts with the proper setup can receive NFTs and that minting operations are securely executed.
For granting minting rights to other accounts, refer to the Admin Minting example.
Up Next: Collection for Holding NFTs
17%