Flow Non-Fungible Token Standard

This standard defines the minimum functionality required to implement a safe, secure, and easy-to-use non-fungible token contract on the Flow blockchain.

What is Cadence?

Cadence is the resource-oriented programming language for developing smart contracts on Flow.

Before reading this standard, we recommend completing the Cadence tutorials to build a basic understanding of the programming language.

Resource-oriented programming, and by extension Cadence, provides an ideal programming model for non-fungible tokens (NFTs). Users are able to store their NFT objects directly in their accounts and transact peer-to-peer. Learn more in this blog post about resources.

Core features

The NonFungibleToken contract defines the following set of functionality that must be included in each implementation.

Contracts that implement the NonFungibleToken interface are required to implement two resource interfaces:

  • NFT - A resource that describes the structure of a single NFT.

  • Collection - A resource that can hold multiple NFTs of the same type.

    Users typically store one collection per NFT type, saved at a well-known location in their account storage.

    For example, all NBA Top Shot Moments owned by a single user are held in a TopShot.Collection stored in their account at the path /storage/MomentCollection.

Create a new NFT collection

Create a new collection using the createEmptyCollection function.

This function MUST return an empty collection that contains no NFTs.

Users typically save new collections to a well-known location in their account and link the NonFungibleToken.CollectionPublic interface as a public capability.

1
let collection <- ExampleNFT.createEmptyCollection()
2
3
account.save(<-collection, to: /storage/ExampleNFTCollection)
4
5
// create a public capability for the collection
6
account.link<&{NonFungibleToken.CollectionPublic}>(
7
/public/ExampleNFTCollection,
8
target: /storage/ExampleNFTCollection
9
)

Withdraw an NFT

Withdraw an NFT from a Collection using the withdraw function. This function emits the Withdraw event.

1
let collectionRef = account.borrow<&ExampleNFT.Collection>(from: /storage/ExampleNFTCollection)
2
?? panic("Could not borrow a reference to the owner's collection")
3
4
// withdraw the NFT from the owner's collection
5
let nft <- collectionRef.withdraw(withdrawID: 42)

Deposit an NFT

Deposit an NFT into a Collection using the deposit function. This function emits the Deposit event.

This function is available on the NonFungibleToken.CollectionPublic interface, which accounts publish as public capability. This capability allows anybody to deposit an NFT into a collection without accessing the entire collection.

1
let nft: ExampleNFT.NFT
2
3
// ...
4
5
let collection = account.getCapability(/public/ExampleNFTCollection)
6
.borrow<&{NonFungibleToken.CollectionPublic}>()
7
?? panic("Could not borrow a reference to the receiver's collection")
8
9
collection.deposit(token: <-nft)

⚠️ Important

In order to comply with the deposit function in the interface, an implementation MUST take a @NonFungibleToken.NFT resource as an argument. This means that anyone can send a resource object that conforms to @NonFungibleToken.NFT to a deposit function. In an implementation, you MUST cast the token as your specific token type before depositing it or you will deposit another token type into your collection. For example:

1
let token <- token as! @ExampleNFT.NFT

List NFTs in an account

Return a list of NFTs in a Collection using the getIDs function.

This function is available on the NonFungibleToken.CollectionPublic interface, which accounts publish as public capability.

1
let collection = account.getCapability(/public/ExampleNFTCollection)
2
.borrow<&{NonFungibleToken.CollectionPublic}>()
3
?? panic("Could not borrow a reference to the receiver's collection")
4
5
let ids = collection.getIDs()

NFT Metadata

NFT metadata is represented in a flexible and modular way using the standard proposed in FLIP-0636.

When writing an NFT contract, you should implement the MetadataViews.Resolverinterface, which allows your NFT to implement one or more metadata types called views.

Each view represents a different type of metadata, such as an on-chain creator biography or an off-chain video clip. Views do not specify or require how to store your metadata, they only specify the format to query and return them, so projects can still be flexible with how they store their data.

How to read metadata

This example shows how to read basic information about an NFT including the name, description, image and owner.

Source: get_nft_metadata.cdc

1
import ExampleNFT from "..."
2
import MetadataViews from "..."
3
4
// ...
5
6
// Get the regular public capability
7
let collection = account.getCapability(ExampleNFT.CollectionPublicPath)
8
.borrow<&{ExampleNFT.ExampleNFTCollectionPublic}>()
9
?? panic("Could not borrow a reference to the collection")
10
11
// Borrow a reference to the NFT as usual
12
let nft = collection.borrowExampleNFT(id: 42)
13
?? panic("Could not borrow a reference to the NFT")
14
15
// Call the resolveView method
16
// Provide the type of the view that you want to resolve
17
// View types are defined in the MetadataViews contract
18
// You can see if an NFT supports a specific view type by using the `getViews()` method
19
if let view = nft.resolveView(Type<MetadataViews.Display>()) {
20
let display = view as! MetadataViews.Display
21
22
log(display.name)
23
log(display.description)
24
log(display.thumbnail)
25
}
26
27
// The owner is stored directly on the NFT object
28
let owner: Address = nft.owner!.address!
29
30
// Inspect the type of this NFT to verify its origin
31
let nftType = nft.getType()
32
33
// `nftType.identifier` is `A.f3fcd2c1a78f5eee.ExampleNFT.NFT`

How to implement metadata

The example NFT contract shows how to implement metadata views.

List of common views

NamePurposeStatusSource
DisplayReturn the basic representation of an NFT.ImplementedMetadataViews.cdc
HTTPFileA file available at an HTTP(S) URL.ImplementedMetadataViews.cdc
IPFSFileA file stored in IPFS.ImplementedMetadataViews.cdc
RoyaltiesAn array of Royalty Cuts for a given NFT.ImplementedMetadataViews.cdc

Royalty View

The MetadataViews contract also includes a standard view for Royalties.

This view is meant to be used by 3rd party marketplaces to take a cut of the proceeds of an NFT sale and send it to the author of a certain NFT. Each NFT can have its own royalty view:

1
pub struct Royalties {
2
3
/// Array that tracks the individual royalties
4
access(self) let cutInfos: [Royalty]
5
}

and the royalty can indicate whatever fungible token it wants to accept via the type of the generic {FungibleToken.Reciever} capability that it specifies:

1
pub struct Royalty {
2
/// Generic FungibleToken Receiver for the beneficiary of the royalty
3
/// Can get the concrete type of the receiver with receiver.getType()
4
/// Recommendation - Users should create a new link for a FlowToken receiver for this using `getRoyaltyReceiverPublicPath()`,
5
/// and not use the default FlowToken receiver.
6
/// This will allow users to update the capability in the future to use a more generic capability
7
pub let receiver: Capability<&AnyResource{FungibleToken.Receiver}>
8
9
/// Multiplier used to calculate the amount of sale value transferred to royalty receiver.
10
/// Note - It should be between 0.0 and 1.0
11
/// Ex - If the sale value is x and multiplier is 0.56 then the royalty value would be 0.56 * x.
12
///
13
/// Generally percentage get represented in terms of basis points
14
/// in solidity based smart contracts while cadence offers `UFix64` that already supports
15
/// the basis points use case because its operations
16
/// are entirely deterministic integer operations and support up to 8 points of precision.
17
pub let cut: UFix64
18
}

If someone wants to make a listing for their NFT on a marketplace, the marketplace can check to see if the royalty receiver accepts the seller's desired fungible token by checking the concrete type of the reference. If the concrete type is not the same as the type of token the seller wants to accept, the marketplace has a few options. They could either get the address of the receiver by using the receiver.owner.address field and check to see if the account has a receiver for the desired token, they could perform the sale without a royalty cut, or they could abort the sale since the token type isn't accepted by the royalty beneficiary.

You can see example implementations of royalties in the ExampleNFT contract and the associated transactions and scripts.

=======

NamePurposeStatusSource
DisplayReturn the basic representation of an NFT.ImplementedMetadataViews.cdc
HTTPFileA file available at an HTTP(S) URL.ImplementedMetadataViews.cdc
IPFSFileA file stored in IPFS.ImplementedMetadataViews.cdc
RoyaltiesAn array of Royalty Cuts for a given NFT.ImplementedMetadataViews.cdc
EditionReturn information about one or more editions for an NFT.ImplementedMetadataViews.cdc
NFTCollectionDataProvides storage and retrieval information of an NFTImplementedMetadataViews.cdc
NFTCollectionDisplayReturns the basic representation of an NFT's Collection.ImplementedMetadataViews.cdc

Important Royalty Instructions for Royalty Receivers

If you plan to set your account as a receiver of royalties, you'll likely want to be able to accept as many token types as possible. This won't be immediately possible at first, but eventually, we will also design a contract that can act as a sort of switchboard for fungible tokens. It will accept any generic fungible token and route it to the correct vault in your account. This hasn't been built yet, but you can still set up your account to be ready for it in the future. Therefore, if you want to receive royalties, you should set up your account with the setup_account_to_receive_royalty.cdc transaction.

This will link generic public path from MetadataViews.getRoyaltyReceiverPublicPath() to your chosen fungible token for now. Then, use that public path for your royalty receiver and in the future, you will be able to easily update the link at that path to use the fungible token switchboard instead.

How to propose a new view

Please open a pull request to propose a new metadata view or changes to an existing view.

Feedback

As Flow and Cadence are still new, we expect this standard to evolve based on feedback from both developers and users.

We'd love to hear from anyone who has feedback. For example:

  • Are there any features that are missing from the standard?
  • Are the current features defined in the best way possible?
  • Are there any pre and post conditions that are missing?
  • Are the pre and post conditions defined well enough? Error messages?
  • Are there any other actions that need an event defined for them?
  • Are the current event definitions clear enough and do they provide enough information?
  • Are the variable, function, and parameter names descriptive enough?
  • Are there any openings for bugs or vulnerabilities that we are not noticing?

Please create an issue in this repository if there is a feature that you believe needs discussing or changing.

Comparison to other standards on Ethereum

This standard covers much of the same ground as ERC-721 and ERC-1155, but without most of the downsides.

  • Tokens cannot be sent to contracts that don't understand how to use them, because an account needs to have a Receiver or Collection in its storage to receive tokens.
  • If the recipient is a contract that has a stored Collection, the tokens can just be deposited to that Collection without having to do a clunky approve, transferFrom.
  • Events are defined in the contract for withdrawing and depositing, so a recipient will always be notified that someone has sent them tokens with their own deposit event.
  • This version can support batch transfers of NFTs. Even though it isn't explicitly defined in the contract, a batch transfer can be done within a transaction by just withdrawing all the tokens to transfer, then depositing them wherever they need to be, all atomically.
  • Transfers can trigger actions because users can define custom Receivers to execute certain code when a token is sent.
  • Easy ownership indexing: rathing than iterating through all tokens to find which ones you own, you have them all stored in your account's collection and can get the list of the ones you own instantly.

How to test the standard

If you want to test out these contracts, we recommend either testing them with the Flow Playground or with the Visual Studio Code Extension.

The steps to follow are:

  1. Deploy NonFungibleToken.cdc
  2. Deploy ExampleNFT.cdc, importing NonFungibleToken from the address you deployed it to.

Then you can experiment with some of the other transactions and scripts in transactions/ or even write your own. You'll need to replace some of the import address placeholders with addresses that you deploy to, as well as some of the transaction arguments.

Running automated tests

You can find automated tests in the lib/go/test/nft_test.go file. It uses the transaction templates that are contained in the lib/go/templates/templates.go file.

Tests have also been written in JavaScript and can be found in lib/js/test/tests/nft_test.js. Similar to the tests written in Go, test helper functions can be found in lib/js/test/templates/ directory.

Entering the make test command from the root directory will run both Go and JavaScript test suites. If you'd like to run just one test suite, you can run make test from the test suite's test/ directory (e.g. running make test from lib/js/test will run just your JavaScript tests).

Bonus features

NFT Forwarding

While this utility contract is not a standard, it is a demonstration of how an account could be configured to forward NFTs to a specified forwarding recipient's collection.

The NFTForwarder resource itself can be referenced like any NonFungibleToken.Receiver resource, allowing a sender to deposit NFT's as they usually would. However, deposit() as implemented in this resource forwards the deposited NFT to the designated recipient's collection.

Several transactions are included in this repo to demonstrate how to interact with the NFTForwarder resource. Those are:

  • create_forwarder.cdc - Creates the NFTForwarder resource and links the capability to ExampleNFT.CollectionPublicPath, where an Example NFT Collection would expect to be found.
  • transfer_nft_to_receiver.cdc - Transfers an NFT to the forwarder by way of deposit() found in NonFungibleToken.Receiver` interface. By construction of the NFTForwarder resource, the NFT deposited by the signer is further forwarded to the forwarding recipient designated in the NFTForwarder resource.
  • change_forwarder_recipient.cdc - Changes the designated recipient collection to which NFT will be forwarded.
  • unlink_forwarder_link_collection.cdc - Unlinks the forwarder resource from ExampleNFT.CollectionPublicPath, restoring the accounts CollectionPublic capability.

(These could each be defined as a separate interface and standard and are probably not part of the main standard) They are not implemented in this repository yet

10- Withdrawing tokens from someone else's Collection by using their Provider reference.

  • approved withdraw event
  • Providing a resource that only approves an account to withdraw a specific amount per transaction or per day/month/etc.
  • Returning the list of tokens that an account can withdraw for another account.
  • Reading the balance of the account that you have permission to send tokens for
  • Owner is able to increase and decrease the approval at will, or revoke it completely
    • This is much harder than anticipated

11 - Standard for Composability/Extensibility

12 - Minting a specific amount of tokens using a specific minter resource that an owner can control

  • tokens minted event
  • Setting a cap on the total number of tokens that can be minted at a time or overall
  • Setting a time frame where this is allowed

13 - Burning a specific amount of tokens using a specific burner resource that an owner controls

  • tokens burnt event
  • Setting a cap on the number of tokens that can be burned at a time or overall
  • Setting a time frame where this is allowed

14 - Pausing Token transfers (maybe a way to prevent the contract from being imported? probably not a good idea)

15 - Cloning the token to create a new token with the same distribution

License

The works in these files:

are under the Unlicense.

Deploying updates

Testnet

1
TESTNET_PRIVATE_KEY=xxxx flow project deploy --update --network testnet