How to Create an NFT Project on Flow
For Cadence 0.42 go to Legacy Docs
This guide is an in-depth tutorial on launching NFT contracts from scratch. To launch in 2 minutes using a tool check out Touchstone
What are NFTsβ
NFTs, or Non-Fungible Tokens, represent a unique digital asset verified using blockchain technology. Unlike cryptocurrencies such as Bitcoin, which are fungible and can be exchanged on a one-for-one basis, NFTs are distinct and cannot be exchanged on a like-for-like basis. This uniqueness and indivisibility make them ideal for representing rare and valuable items like art, collectibles, tickets and even real estate. Their blockchain-backed nature ensures the authenticity and ownership of these digital assets.
Setting Up a Projectβ
To start creating an NFT on the Flow blockchain, you'll first need some tools and configurations in place.
Installing Flow CLIβ
The Flow CLI (Command Line Interface) provides a suite of tools that allow developers to interact seamlessly with the Flow blockchain.
If you haven't installed the Flow CLI yet and have Homebrew installed, you can run brew install flow-cli
. If you donβt have Homebrew, please follow the installation guide here.
Initializing a New Projectβ
π‘ Note: Here is a link to the completed code if you want to skip ahead or reference as you follow along.
Once you have the Flow CLI installed, you can set up a new project using the flow setup
command. This command initializes the necessary directory structure and a flow.json
configuration file (a way to configure your project for contract sources, deployments, accounts, and more):
_10flow setup foobar-nft
Upon execution, the command will generate the following directory structure:
_10/cadence_10 /contracts_10 /scripts_10 /transactions_10 /tests_10flow.json
Now, navigate into the project directory:
_10cd foobar-nft
To begin, let's create a contract file named FooBar
for the FooBar
token, which will be the focus of this tutorial. To do this, we can use the boilerplate generate
command from the Flow CLI:
_10flow generate contract FooBar
This will create a new file at cadence/contracts/FooBar.cdc
with the following contents:
_10access(all) contract FooBar {_10 init() {}_10}
Setting Up Our NFT on the Contractβ
Understanding Resourcesβ
On the Flow blockchain, "Resources" are a key feature of the Cadence programming language. They represent unique, non-duplicable assets, ensuring that they can only exist in one place at a time. This concept is crucial for representing NFTs on Flow, as it guarantees their uniqueness.
To begin, let's define a basic NFT
resource. This resource requires an init
method, which is invoked when the resource is instantiated:
_10access(all) contract FooBar {_10_10 access(all) resource NFT {_10 init() {}_10 }_10_10 init() {}_10}
Every resource in Cadence has a unique identifier assigned to it. We can use it to set an ID for our NFT. Here's how you can do that:
_12access(all) contract FooBar {_12_12 access(all) resource NFT {_12 access(all) let id: UInt64_12_12 init() {_12 self.id = self.uuid_12 }_12 }_12_12 init() {}_12}
To control the creation of NFTs, it's essential to have a mechanism that restricts their minting. This ensures that not just anyone can create an NFT and inflate its supply. To achieve this, you can introduce an NFTMinter
resource that contains a createNFT
function:
_14access(all) contract FooBar {_14_14 // ...[previous code]..._14_14 access(all) resource NFTMinter {_14 access(all) fun createNFT(): @NFT {_14 return <-create NFT()_14 }_14_14 init() {}_14 }_14_14 init() {}_14}
In this example, the NFTMinter
resource will be stored on the contract account's storage. This means that only the contract account will have the ability to mint new NFTs. To set this up, add the following line to the contract's init
function:
_10access(all) contract FooBar {_10_10 // ...[previous code]..._10_10 init() {_10 self.totalSupply = 0_10 self.account.save(<- create NFTMinter(), to: /storage/fooBarNFTMinter)_10 }_10}