Home Blockchain 4. Non-Fungible Tokens – Flow Documentation

4. Non-Fungible Tokens – Flow Documentation

by Assessor
Published: Last Updated on

Rate this post

On this tutorial, we’ll deploy, retailer, and switch Non-Fungible Tokens (NFTs).

The NFT is an integral a part of blockchain know-how. We want NFTs to characterize property which might be distinctive and indivisible (like CryptoKitties! Or High Shot Moments!, or tickets to a extremely enjoyable live performance!).

As a substitute of being represented in a central ledger, like in most good contract languages, Cadence represents every NFT as a useful resource object that customers retailer of their accounts.

We’ll take you thru these steps to get snug with the NFT:

  1. Deploy the NFT contract and sort definitions.
  2. Create an NFT object and retailer it in your account storage.
  3. Create an NFT assortment object to retailer a number of NFTs in your account.
  4. Create an NFTMinter and use it to mint an NFT.
  5. Create references to your assortment that others can use to ship you tokens.
  6. Arrange one other account the identical method.
  7. Switch an NFT from one account to a different.
  8. Use a script to see what NFTs are saved in every account’s assortment.

Earlier than continuing with this tutorial, we extremely advocate following the directions in Getting Began, Hi there, World!, and Fungible Tokens to discover ways to use the Playground instruments and to be taught the basics of Cadence. We’ll cowl a few of the ideas once more right here whereas including new ones, however not all.

Non-Fungible Tokens on the Circulate Emulator

In Cadence, every NFT is represented by a useful resource with an integer ID. Sources are an ideal kind to characterize NFTs as a result of sources have necessary possession guidelines which might be enforced by the sort system. They will solely have one proprietor, can’t be copied, and can’t be by accident or maliciously misplaced or duplicated. These protections be sure that house owners know that their NFT is protected and might characterize an asset that has actual worth.

An NFT can also be normally represented by some type of metadata like a reputation or an image. Traditionally, most of this metadata has been saved off-chain, and the on-chain token solely accommodates a URL or one thing comparable that factors to the off-chain metadata. In Circulate, that is attainable, however the aim is to make it attainable for all of the metadata related to a token to be saved on-chain. That is out of the scope of this tutorial although. This paradigm has been outlined by the Circulate neighborhood and the small print are contained within the NFT metadata proposal.

When customers on Circulate need to transact with one another, they will accomplish that peer-to-peer and with out having to work together with a central NFT contract by calling resource-defined strategies in every customers’ account.

Including an NFT Your Account

On this contract, the NFT is a useful resource with an integer ID and a discipline for metadata.

That is completely different from the Fungible Token as a result of a fungible token merely was a vault with a stability that would change when tokens have been withdrawn or deposited. Every of these Vaults have been precisely the identical.

Right here, every NFT useful resource has a singular ID, in order that they can’t be mixed or duplicated, until the good contract permits it.

One other distinctive characteristic of this design is that every NFT can include its personal metadata. On this instance, we use a easy String-to-String mapping, however you possibly can think about a way more wealthy model that may permit the storage of complicated file codecs and different such knowledge.

An NFT might even personal different NFTs! This instance is proven in a later tutorial.

Within the contract’s init operate, we create a brand new NFT object and transfer it into the account storage.

Right here we entry the AuthAccount object on the account the contract is deployed to and name its save technique, specifying @NFT as the sort it’s being saved as. We additionally create the NFT in the identical line and go it as the primary argument to avoid wasting. We put it aside to the /storage area, the place objects are supposed to be saved.

You need to now have an NFT in your account. Let’s run a transaction to verify.

Right here, we try to straight borrow a reference from the NFT in storage. If the item exists, the borrow will succeed and the reference non-compulsory won’t be nil, but when the borrow fails, the non-compulsory can be nil.

You need to see one thing that claims “The token exists!”.

Good work! You may have your first NFT in your account.

Storing A number of NFTs in a Assortment

We might retailer our NFTs on the prime stage of storage, however this might begin to get complicated to prepare all of your NFTs if in case you have many. You would need to use a unique path title for every NFT which might begin to get very tough to maintain observe of.

This method will not be as scalable, however we will overcome this concern through the use of an information construction that may maintain as many NFTs as we would like. We might accomplish this through an array or dictionary, however these varieties are comparatively opaque. As a substitute, we will use a useful resource as our NFT assortment to allow more-sophisticated methods to work together with our NFTs.

Any person who owns a number of ExampleNFT can have an occasion of this @ExampleNFT.Assortment useful resource saved of their account. This assortment shops all of their NFTs in a dictionary that maps integer IDs to NFTs, just like how a Vault useful resource shops all of the tokens within the stability discipline.

One other similarity is how every assortment has a deposit and withdraw operate. These capabilities permit customers to comply with the sample of first withdrawing the token from their assortment after which depositing to a different assortment, or doing one thing else with it!

When a person desires to retailer NFTs of their account, they may instantiate an empty Assortment by calling the createEmptyCollection operate within the ExampleNFT good contract. This returns an empty Assortment object that they will retailer of their account storage.

There are just a few new options that we use on this instance, so let’s stroll by way of them.

Path fields

Within the ExampleNFT contract, we outline three fields for paths in account storage:

These point out what storage paths the gathering and minter ought to be saved at and which public path the gathering public functionality ought to be linked at.

In program design, it is very important keep away from hard-coding (straight typing a worth into an announcement) as a result of that introduces alternatives for human error to trigger issues in code. As a result of we outline paths within the contract for these varieties, exterior customers can all the time reference the trail discipline names as an alternative of typing within the paths straight.

This programming rule applies to all kinds. It’s virtually all the time higher to make use of a variable or discipline for values as an alternative of hard-coding, particularly in good contracts!


This useful resource makes use of a Dictionary: a mutable, unordered assortment of key-value associations.

In a dictionary, all keys should have the identical kind, and all values should have the identical kind. On this case, we’re mapping integer (Int) IDs to NFT useful resource objects. Dictionary definitions do not normally have the @ image within the kind specification, however as a result of the ownedNFTs mapping shops sources, the entire discipline additionally has to change into a useful resource kind, which is why the sphere has the @ image indicating that it’s a useful resource kind.

Which means that all the principles that apply to sources apply to this kind.

If the NFT assortment useful resource is destroyed with the destroy command, it must know what to do with the sources it shops within the dictionary. That is why sources that retailer different sources have to incorporate a destroy operate that runs when destroy known as on it. This destroy operate has to both explicitly destroy the contained sources or transfer them some other place. On this instance, we destroy them.

When the Assortment useful resource is created, the init operate is run and should explicitly initialize all member variables. This helps stop points in some good contracts the place uninitialized fields may cause bugs. The init operate can by no means run once more after this. Right here, we initialize the dictionary as a useful resource kind with an empty dictionary.

One other characteristic for dictionaries is the power to get an array of the keys of the dictionary utilizing the built-in keys operate.

This can be utilized to iterate by way of the dictionary or simply to see a listing of what’s saved. As you may see, a variable size array kind is asserted by enclosing the member kind inside sq. brackets.

Sources Proudly owning Sources

This NFT Assortment instance in nftgamef.com illustrates an necessary characteristic: sources can personal different sources.

Within the instance, a person can switch one NFT to a different person. Moreover, for the reason that Assortment explicitly owns the NFTs in it, the proprietor might switch the entire NFTs without delay by simply transferring the one assortment.

This is a vital characteristic as a result of it allows quite a few extra use instances. Along with permitting straightforward batch transfers, which means if a singular NFT desires to personal one other distinctive NFT, like a CryptoKitty proudly owning a hat accent, the Kitty actually shops the hat in its personal storage and successfully owns it. The hat belongs to the CryptoKitty that it’s saved in, and the hat could be transferred individually or together with the CryptoKitty that owns it,

Capabilities can’t be created for sources which might be saved in different sources, however references can. The proudly owning useful resource has management over it and subsequently controls the kind of entry that exterior calls have on the saved useful resource.

Limiting Entry to the NFT Assortment

Within the NFT Assortment, all of the capabilities and fields are public, however we don’t want everybody within the community to have the ability to name our withdraw operate. That is the place Cadence’s second layer of entry management is available in. Cadence makes use of functionality safety, which implies that for any given object, a person is allowed to entry a discipline or technique of that object in the event that they both:

  • Are the proprietor of the item
  • Have a legitimate reference to that discipline or technique (notice that references can solely be created from capabilities, and capabilities can solely be created by the proprietor of the item)

When a person shops their NFT Assortment of their account storage, it’s by default not out there for different customers to entry. A person’s licensed account object (AuthAccount, which provides entry to personal storage) is simply accessible by its proprietor. To provide exterior accounts entry to the deposit operate, the getIDs operate, and the idExists operate, the proprietor creates an interface that solely contains these fields:

Then, utilizing that interface, they might create a hyperlink to the item in storage, specifying that the hyperlink solely accommodates the capabilities within the NFTReceiver interface. This hyperlink creates a functionality. From there, the proprietor can then do no matter they need with that functionality: they may go it as a parameter to a operate for one-time-use, or they may put within the /public/ area of their account in order that anybody can entry it. If a person tried to make use of this functionality to name the withdraw operate, it would not work as a result of it does not exist within the interface that was used to create the potential.

The creation of the hyperlink and functionality is seen within the nftgamef.com contract init() operate

The hyperlink operate specifies that the potential is typed as &AnyResource{NFTReceiver} to solely expose these fields and capabilities. Then the hyperlink is saved in /public/ which is accessible by anybody. The hyperlink targets the /storage/NFTCollection (by way of the nftgamef.comectionStoragePath contract discipline) that we created earlier.

Now the person has an NFT assortment of their account /storage/, together with a functionality for it that others can use to see what NFTs they personal and to ship an NFT to them.

Let’s affirm that is true by working a script!

Run a Script

Scripts in Cadence are easy transactions that run with none account permissions and solely learn data from the blockchain.

As a result of account 0x02 at the moment does not personal any in its assortment, it is going to simply print an empty array:

If the script can’t be executed, it in all probability implies that the NFT assortment hasn’t been saved appropriately in account 0x02. If you happen to run into points, just be sure you deployed the contract in account 0x02 and that you simply adopted the earlier steps appropriately.

Mint and Distribute Tokens as an Admin

One approach to create NFTs is by having an admin mint new tokens and ship them to a person. Most would implement this by having an NFT Minter useful resource. The proprietor of this useful resource can mint tokens, or in the event that they need to give different customers and contracts the power to mint tokens, the proprietor might give out a functionality that solely exposes the mintNFT operate to make the most of the potential safety mannequin. No have to explicitly verify the sender of a transaction like in ledger-based fashions!

Let’s use an NFT Minter to mint some tokens.

If you happen to refer again to the ExampleNFT contract in nftgamef.com, you may see that it outlined one other useful resource, NFTMinter. It is a easy instance of what an admin with minting permissions would personal to mint new NFTs. This merely has a single operate to mint the NFTs and an incrementing integer discipline for assigning distinctive IDs to the NFTs.

There ought to be an nftgamef.cominter useful resource saved in account 0x02’s account storage.

Now we will use our saved NFTMinter to mint a brand new NFT and deposit it into consideration 0x02’s assortment.

You need to see that account 0x02 owns the NFT with id=1

Transferring an NFT

Earlier than we’re capable of switch an NFT to a different account, we have to arrange that account with an NFTCollection of their very own so they’re able to obtain NFTs.

Account 0x01 ought to now have an empty Assortment useful resource saved in its account storage. It has additionally created and saved a functionality to the gathering in its /public/ area.

Now we will verify each accounts’ collections to make it possible for account 0x01 owns the token and account 0x02 has nothing.

You need to see one thing like this within the output:

Account 0x01 has one NFT with ID=1 and account 0x02 has none. This exhibits that the NFT was transferred from account 0x02 to account 0x01.

Congratulations, you now have a working NFT!

Placing It All Collectively

This was solely a primary instance how a NFT may work on Circulate. Please confer with the Circulate NFT Normal repo for details about the official Circulate NFT customary and an instance implementation of it.

Additionally try the Kitty Gadgets Repo for a manufacturing prepared model!

Create a Circulate Market

Now that you’ve a working NFT, you may try to increase its performance by yourself, or you may discover ways to create a market that makes use of each fungible tokens and NFTs. Transfer on to the following tutorial to study Marketplaces in Cadence!

Related Posts