As with fungible tokens, NFTs are not stored in the user’s wallet. Instead, each NFT lives in an NFT contract that works as a bookkeeper, being in charge of creating, storing, and transferring NFTs.
Be mindful of not confusing an NFT contract with an NFT marketplace. NFT contracts simply store information (metadata), while NFT marketplaces are contracts where NFTs can be listed and exchanged for a price.
The Standard
NEP-171 is the blueprint for all non-fungible tokens on NEAR. It defines a common set of rules and functions that the contract MUST implement to be considered a non-fungible token contract. NEP-177 extends NEP-171 to define the metadata for both non-fungible tokens and NFT contracts.Deploying an NFT Contract
You can deploy your own NFT contract using the reference implementation.Deploying by hash creates an immutable contract that never changes. Deploying by account ID creates an updatable contract that changes when the referenced account’s contract is updated.
Minting an NFT
To create a new NFT (minting), you call thenft_mint method with the metadata that defines the NFT.
See the metadata standard for the full list of
TokenMetadata parameters.Royalties
Royalties enable you to create a list of users that should get paid when the token is sold in a marketplace. For example, ifanna has 5% royalties, each time the NFT is sold, anna should get 5% of the selling price.
Querying NFT Data
You can query the NFT’s information and metadata by callingnft_token.
Transferring an NFT
Transferring an NFT can happen in two scenarios: (1) you ask to transfer an NFT you own, or (2) an authorized account asks to transfer the NFT. In both cases, invoke thenft_transfer method with the token ID and receiver.
Attaching NFTs to a Call
Natively, only NEAR tokens can be attached to function calls. However, the NFT standard enables attaching non-fungible tokens by using the NFT contract as an intermediary. Instead of attaching tokens directly, you ask the NFT contract to do both a transfer and a function call on your behalf usingnft_transfer_call.
How it Works
- You call
nft_transfer_callin the NFT contract passing: the receiver, a message, and the token ID. - The NFT contract transfers the NFT to the receiver.
- The NFT contract calls
receiver.nft_on_transfer(sender, token-owner, token-id, msg). - The NFT contract handles errors in the
nft_resolve_transfercallback. - The NFT contract returns
trueif it succeeded.
nft_on_transfer method:
Approving Users
You can authorize other users to transfer an NFT you own. This is useful for listing your NFT in a marketplace.If the
msg parameter is included, a cross-contract call will be made to <authorized_account>.nft_on_approve(msg), which will then make a callback to nft_resolve_transfer in your NFT contract.