How to Create an NFT Project on Flow
This tutorial dives into the technical steps required to craft an NFT on the Flow blockchain, providing developers with a clear roadmap from setup to deployment.
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):
Upon execution, the command will generate the following directory structure:
Now, navigate into the project directory:
To begin, let's create a contract file named FooBar
for the FooBar
token, which will be the focus of this tutorial:
With the contract file in place, you can now set up the basic contract structure:
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:
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:
We also need to keep track of the total supply of NFTs in existance. To do this let’s create a totalSupply
variable on our contract and increase it by one whenever a new NFT is created. We can set this on the initialization of the NFT using the resource init
function:
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:
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:
Setting Up an NFT Collection
Storing individual NFTs directly in an account's storage can cause issues, especially if you want to store multiple NFTs. Instead, it's required to create a collection that can hold multiple NFTs. This collection can then be stored in the account's storage.
Start by creating a new resource named Collection
. This resource will act as a container for your NFTs, storing them in a dictionary indexed by their IDs. Additionally, to ensure that all NFTs within a collection are destroyed when the collection itself is destroyed, you can add a destroy
function:
To allow accounts to create their own collections, add a function in the main contract that creates a new Collection
and returns it:
To manage the NFTs within a collection, you'll need functions to deposit and withdraw NFTs. Here's how you can add a deposit
function:
Similarly, you can add a withdraw
function to remove an NFT from the collection:
To facilitate querying, you'll also want a function to retrieve all the NFT IDs within a collection:
For security reasons, you might not want to expose all the functions of the Collection to everyone. Instead, you can create an interface that exposes only the methods you want to make public. In Cadence, interfaces act as a blueprint for resources and structures, ensuring that certain methods or properties exist. By leveraging these interfaces, you establish clear boundaries and standardized interactions. In this case, you might want to expose only the deposit
and getIDs
methods. This interface can then be used to create capabilities, ensuring that only the allowed methods are accessible.
Fitting the Flow NFT Standard
To ensure compatibility and interoperability within the Flow ecosystem, it's crucial that your NFT contract adheres to the Flow NFT standard. This standard defines the events, functions, resources, and other elements that a contract should have. By following this standard, your NFTs will be compatible with various marketplaces, apps, and other services within the Flow ecosystem.
Applying the Standard
To start, you need to inform the Flow blockchain that your contract will implement the NonFungibleToken
standard. Since it's a standard, there's no need for deployment. It's already available on the Emulator, Testnet, and Mainnet for the community's benefit.
Begin by importing the token standard into your contract:
Adding Standard Events
To ensure interoperability, the Flow NFT standard requires certain events to be emitted during specific operations.
Adding ContractInitialized Event
For instance, when the contract is initialized, a ContractInitialized
event should be emitted:
Adding Withdraw and Deposit Events
Additionally, when NFTs are withdrawn or deposited, corresponding events should be emitted:
You can then update your deposit
and withdraw
functions to emit these events:
Update NFT Resource
The NFT
resource should also be updated to implement the NonFungibleToken.INFT
interface:
Adding Provider, Receiver, CollectionPublic
Your Collection
resource should also implement the Provider
, Receiver
, and CollectionPublic
interfaces from the standard:
With these implementations, you can now remove your custom CollectionPublic
interface since the standard already provides it.
To ensure users can access a read-only reference to an NFT in the collection without actually removing it, introduce the borrowNFT
function.
Lastly, update the ownedNFTs
, deposit
, and withdraw
variables/methods to use the NonFungibleToken.NFT
type:
Deploying the Contract
With your contract ready, it's time to deploy it. First, add the FooBar
contract to the flow.json
configuration file:
When prompted, enter the following name and location (press Enter
to skip alias questions):
Next, configure the deployment settings by running the following command:
Choose the emulator
for the network and emulator-account
for the account to deploy to. Then, select the FooBar
contract (you may need to scroll down). This will update your flow.json
configuration. After that, you can select No
when asked to deploy another contract.
To start the Flow emulator, run (you may need to approve a prompt to allow connection the first time):
In a separate terminal or command prompt, deploy the contract:
You’ll then see a message that says All contracts deployed successfully
.
Creating an NFTCollection
To manage multiple NFTs, you'll need an NFT collection. Start by creating a transaction file for this purpose:
Transactions, on the other hand, are pieces of Cadence code that can mutate the state of the blockchain. Transactions need to be signed by one or more accounts, and they can have multiple phases, represented by different blocks of code.
In this file, import the necessary contracts and define a transaction to create a new collection, storing it in the account's storage. Additionally, for the CollectionPublic
interface, create a capability that allows others to read from its methods. This capability ensures secure, restricted access to specific functionalities or information within a resource.
To store this new NFT collection, create a new account:
Name it test-acct
and select emulator
as the network. Then, using the Flow CLI, run the transaction:
Congratulations! You've successfully created an NFT collection for the test-acct
.
Get an Account's NFTs
To retrieve the NFTs associated with an account, you'll need a script. Scripts are read-only operations that allow you to query the blockchain. They don't modify the blockchain's state, and therefore, they don't require gas fees or signatures (read more about scripts here).
Start by creating a script file:
In this script, import the necessary contracts and define a function that retrieves the NFT IDs associated with a given account:
To check the NFTs associated with the test-acct
, run the script (note: replace 0x123
with the address for test-acct
from flow.json
):
Since you haven't added any NFTs to the collection yet, the result will be an empty array.
Minting and Depositing an NFT to a Collection
To mint and deposit an NFT into a collection, create a new transaction file:
In this file, define a transaction that takes a recipient's address as an argument. This transaction will borrow the minting capability from the contract account, borrow the recipient's collection capability, create a new NFT using the minter, and deposit it into the recipient's collection:
To run this transaction, use the Flow CLI. Remember, the contract account (which has the minting resource) should be the one signing the transaction. Pass the test account's address (from the flow.json
file) as the recipient argument (note: replace 0x123
with the address for test-acct
from flow.json
):
After executing the transaction, you can run the earlier script to verify that the NFT was added to the test-acct
's collection (remember to replace 0x123
):
You should now see a value in the test-acct
's collection array!
Transferring an NFT to Another Account
To transfer an NFT to another account, create a new transaction file:
In this file, define a transaction that takes a recipient's address and the ID of the NFT you want to transfer as arguments. This transaction will borrow the sender's collection, get the recipient's capability, withdraw the NFT from the sender's collection, and deposit it into the recipient's collection:
To transfer the NFT, first create a new account:
Name it test-acct-2
and select Emulator
as the network. Next, create a collection for this new account:
Now, run the transaction to transfer the NFT from test-acct
to test-acct-2
using the addresses from the flow.json
file (replace 0x124
with test-acct-2
's address. Also note that 0
is the id
of the NFT
we'll be transferring):
To verify the transfer, you can run the earlier script for test-acct-2
(replace 0x124
):
Adding MetadataViews
Many NFT projects include metadata associated with the NFT, such as a name, description, or image. However, different projects might store this metadata in various formats. To ensure compatibility across the Flow ecosystem, Flow uses MetadataViews
to standardize the representation of this metadata.
There are two types of Metadata Views: NFT level and contract level. In this guide, we’ll show you how to implement the most basic display, but for a deeper dive into what is possible, check out the MetadataViews API doc.
NFT Metadata
For the NFT metadata, you'll add a simple MetadataView
called Display
, which includes a name
, description
, and thumbnail
. This format is common for many NFT projects. (For more details, refer to the Display documentation).
Start by importing the MetadataViews
contract into your FooBar
contract:
Because this is already deployed to Emulator and our flow setup
command added it to our flow.json
, there is no more configuration we need to do.
Update the NFT
resource to implement the [ViewResolver
interface](https://github.com/onflow/flow-nft/blob/master/contracts/MetadataViews.cdc#L20) provided by the MetadataViews contract. This interface specifies that a getViews
function and a resolveView
function should exist. Then, add fields for name
, thumbnail
, and description
:
Now, add the methods from the ViewResolver
interface to the NFT
resource. These methods will return the metadata in the standardized Display
format:
Finally, to retrieve our NFT along with its metadata, we currently have a borrowNFT
function. However, this function only returns a NonFungibleToken.NFT
with an id
field. To address this, let's introduce a new function in our collection that borrows the NFT and returns it as a FooBar
NFT. We'll utilize the auth
syntax to downcast the NonFungibleToken.NFT
to our specific type.
Contract Metadata
For the contract level metadata, we need to create an interface that defines the required methods for the contract. Luckily, there is already a commonly used contract interface called ViewResolver
deployed both to the Emulator and other networks. This interface requires a getViews
and a resolveViews
method. It is also deployed on Testnet and Mainnet. You can find its address in the flow.json
we generated with the setup
command. To use it, return to your FooBar
contract, import this new contract, and specify that FooBar
should implement it.
Just like the NFT (except at a contract level), we’ll add functions for getView
which returns the Display
and resolveViews
which tells it how to get the Display
values:
Finally, we need a way to read this data like we did with the NFT. Let’s also make a borrowViewResolver
function that we add below the borrowFooBarNFT
method inside of the Collection
:
Congrats, you did it! You’re now ready to launch the next fun NFT project on Flow.
More
- Explore an example NFT repository
- Watch a video tutorial on creating an NFT project in the Flow Playground
- Dive into the details of the NFT Standard
- For a deeper dive into
MetadataViews
, consult the API documentation or the FLIP that introduced this feature. - Use a no code tool for creating NFT projects on Flow