Non-fungible token standard
The non-fungible token support in near-contract-standards provides a reusable NEP-171 token system for unique assets. It gives a contract a common model for token ownership, optional approvals, metadata, owner-facing lists, transfers, receiver calls, and indexer events while leaving mint and burn policy to the contract that uses it.
This page is a child of Reusable contract standards. The model is similar to the Fungible token standard, but each asset has its own string token ID and owner. Storage deposits and refunds are part of the contract’s cost model; see Storage management and contract upgrades for the shared storage concerns. For event log rules shared across standards, see Events, JSON, and contract types.
Standard scope
The NFT module supplies a core token type and separate traits for optional capabilities. A consuming contract selects the capabilities it exposes and forwards those trait methods to its token state. This lets a basic token stay small while allowing marketplaces, wallets, and other contracts to use common interfaces when the extensions are present.
The main capabilities are:
- Core tokens: Store one owner for each unique string token ID. Return the token and support direct or call-based transfers.
- Approval management: Let an owner authorize one or more accounts for a specific token. Each approval has an ID that can protect a transfer from stale authorization.
- Enumeration: Return total supply, all tokens, and paged token lists for an account.
- Metadata: Return collection metadata and optional metadata for each token.
- Events: Emit NEP-171 mint, transfer, and burn events for indexers and clients.
The primary shared type is NonFungibleToken in near-contract-standards/…/core_impl.rs. A contract normally stores this value and implements the public traits around it. The core interface also exposes the generated ext_nft_core call surface for type-safe calls to another contract that implements the same NFT interface. The example in examples/…/lib.rs shows a contract that enables all of these extensions.
Token model
An NFT is identified by a TokenId, which is a string rather than a numeric value. The string form keeps IDs flexible for clients and for systems that move assets between chains. A returned Token combines the ID, its current owner, optional token metadata, and optional approved accounts.
The core state treats ownership as the source of truth. Metadata, enumeration, and approvals are optional parts of the token record, so callers must not assume that every NFT contract supports them. A token exists only when its ID has an owner, and minting rejects an ID that already exists.
Ownership and approvals
Ownership model
The core standard exposes nft_token as the view of a token’s current owner and nft_transfer as the guarded ownership change. The direct transfer requires a one-yoctoNEAR deposit and accepts an optional memo for client or indexer use. A successful transfer changes the owner and clears approvals when approval management is enabled.
Minting is a supporting operation rather than a core standard method. The shared helper checks token uniqueness and requires per-token metadata when the metadata extension is enabled. The consuming contract decides who may mint and which public mint rules apply, as shown by nft_mint in the example contract.
References: near-contract-standards/…/token.rs• L7-18 near-contract-standards/…/mod.rs• L72-102 near-contract-standards/…/core_impl.rs• L198-253 near-contract-standards/…/core_impl.rs• L272-374
Approval model
Approval management gives an owner an escrow-like way to authorize an account for one token. The owner can add an approval, revoke one approval, revoke all approvals, or check an account and optional approval ID. Approvals are specific to a token, and re-approval receives a new ID so an old transfer request cannot silently reuse the authorization.
Approval changes require at least a one-yoctoNEAR deposit. New approvals also need enough deposit to cover their storage, and the module refunds storage cost when an approval is removed. If an approval includes a message, the approved account receives an nft_on_approve notification. A contract can use this callback to begin a sale or another action without making approval itself a transfer.
References: near-contract-standards/…/mod.rs• L58-132 near-contract-standards/…/approval_impl.rs• L23-65 near-contract-standards/…/approval_impl.rs• L67-153 near-contract-standards/…/approval_receiver.rs• L4-26
Metadata
NFT metadata has two scopes. NFTContractMetadata describes the collection and identifies the metadata format as nft-1.0.0; TokenMetadata describes one asset. The contract-level provider returns the collection record, while the core token view includes the per-token record when that extension is enabled.
Metadata can include names, descriptions, media URLs, copy counts, dates, extra data, and references to off-chain JSON. A media or reference URL must be paired with a 32-byte hash. These checks help clients know whether a referenced asset has a content check, but the contract remains responsible for choosing and updating its metadata policy.
The module does not define a metadata update method. A contract that allows updates must expose its own policy and keep the token view, metadata provider, and related events consistent.
References: near-contract-standards/…/metadata.rs• L4-18 near-contract-standards/…/metadata.rs• L20-67 near-contract-standards/…/core_impl.rs• L417-425
Enumeration
Enumeration adds view methods for discovery. Clients can ask for total supply, page through all tokens, count tokens for an owner, or page through one owner’s tokens. Counts use U128 JSON values so large supplies are represented as strings rather than unsafe JSON numbers.
The all-token list and owner list both accept a starting index and a limit. Clients should use a finite limit for large collections because an unlimited view can exceed execution limits. Enumeration is an optional extension; calls to owner-specific enumeration methods fail when the token state was not created with owner lists.
Transfers and minting update the owner lists along with the owner record. This relationship is important: a contract that changes ownership through an unguarded custom path must also maintain enumeration state or its discovery views will become wrong.
References: near-contract-standards/…/mod.rs• L43-87 near-contract-standards/…/enumeration_impl.rs• L21-94 near-contract-standards/…/core_impl.rs• L163-195 near-contract-standards/…/core_impl.rs• L342-361
Transfer flows
Direct transfers
nft_transfer is the simple path for moving a token to another account. The caller must be the owner or an account approved for that token. An approved caller can provide the expected approval ID, which prevents a request based on an old approval from being accepted after the owner changes authorization.
The transfer rejects a missing token, an unauthorized caller, and a transfer to the current owner. It updates owner-facing enumeration when enabled, emits a transfer event, and removes approvals on success. The one-yoctoNEAR requirement protects the method from calls that do not prove an intended signed action.
The core trait also generates ext_nft_core, a type-safe call interface for contracts that need to invoke nft_transfer, nft_transfer_call, or nft_token on an external NFT contract. External transfer calls still use the standard one-yoctoNEAR deposit and the same ownership and approval rules as local callers.
References: near-contract-standards/…/mod.rs• L55-72 near-contract-standards/…/mod.rs• L72-102 near-contract-standards/…/core_impl.rs• L198-253 near-contract-standards/…/core_impl.rs• L377-388
Receiver calls and resolution
nft_transfer_call transfers the token first, then calls nft_on_transfer on the receiving contract with the sender, previous owner, token ID, and caller message. The receiver must protect this callback by accepting calls only from NFT contracts it trusts. It returns whether the token should go back to the sender.
The NFT contract then calls its own nft_resolve_transfer callback. A failed receiver call or a receiver result that requests a return causes a rollback when the receiver still owns the token. The resolver also restores the original approvals. If the receiver already moved or burned the token, the module cannot take it back, so the resolution reports that no further return was possible.
The flow is asynchronous because the receiver may make more cross-contract calls before resolution. The resolver must be private to the NFT contract; otherwise another account could attempt to forge a transfer result.
This diagram shows how a call-based transfer moves through the receiver and the final ownership decision.
sequenceDiagram
participant S as Sender
participant N as NFT contract
participant R as Receiver contract
S->>N: nft_transfer_call(receiver, token, msg)
N->>N: Transfer and clear approvals
N->>R: nft_on_transfer(sender, previous owner, token, msg)
R-->>N: Keep token or request return
N->>N: nft_resolve_transfer(result)
alt Receiver failed or requested return
N->>N: Restore owner and original approvals
else Receiver keeps token
N->>N: Keep new owner
end
References: near-contract-standards/…/mod.rs• L104-144 near-contract-standards/…/receiver.rs• L4-30 near-contract-standards/…/resolver.rs• L28-62 near-contract-standards/…/core_impl.rs• L390-415 near-contract-standards/…/core_impl.rs• L428-481
Events
The event module supplies NEP-171 version 1.0.0 events in the NEP-297 event format. Indexers can use these logs to track mint, transfer, and burn activity without reading every contract state change.
Mint and transfer helpers emit events for the standard paths. The NftBurn event type is available to a contract’s burn logic because burning is not a core public operation. Events can describe one or many token IDs and can include a memo; transfer and burn records also have an optional authorized account field.
References: near-contract-standards/…/events.rs• L1-18 near-contract-standards/…/events.rs• L20-125 near-contract-standards/…/core_impl.rs• L247-269 near-contract-standards/…/core_impl.rs• L300-313
Extension boundaries
The NonFungibleToken value is configured with optional support for metadata, enumeration, and approvals. The contract then implements the matching traits as public interfaces. This makes the trait set part of the contract’s compatibility promise: enabling state without exposing its trait methods does not give clients a usable standard endpoint, and exposing methods without configuring their state causes calls to fail.
The current preferred integration path is manual trait implementation. The module still exports helper macros for core, approval, and enumeration forwarding, but those macros are deprecated. Receiver and resolver traits remain separate because one runs on the receiving contract and the other runs on the NFT contract. The core trait also exposes ext_nft_core so other contracts can call a compatible NFT contract through the same standard interface.
The module also exposes storage helpers for deposits and approval refunds. Minting and approval changes can increase storage use, so their public contract methods must use the attached deposit and refund behavior expected by the standard. See the storage page for the wider storage-accounting contract.
Change checklist
When these files or sections are changed, remember to consider these:
- When extension configuration changes in
NonFungibleToken::new, keep the configured state, the implemented public traits, and the fields returned bynft_tokenaligned. - When mint or burn rules change, update owner enumeration, token metadata, approvals, storage refunds, and the matching NEP-171 event path together.
- When transfer-call arguments or return handling changes, keep
nft_on_transfer, the private resolver, approval restoration, and receiver-side trust checks compatible. - When approval storage changes, preserve approval ID monotonicity and refund behavior so marketplaces cannot use stale approvals or strand storage deposits.
- When metadata fields or validation rules change, update both collection metadata consumers and per-token metadata consumers, including URL-hash pairing.
- When event names, fields, or versions change, check indexer consumers because event logs are an external data contract.
- When the core trait signature or generated external call interface changes, check every contract and client that uses
ext_nft_coreor forwards the core trait.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A transfer or revoke call has no required one-yoctoNEAR deposit | The method rejects the request before changing token state | Attach the required security deposit to payable methods |
| A non-owner transfers without a valid approval, or supplies a stale approval ID | The transfer fails and ownership does not change | Check the owner or current per-token approval before submitting the transfer |
| Minting reuses an existing token ID | Minting fails because token identity must be unique | Allocate and check unique string IDs |
| A mint does not attach enough deposit for new storage | The mint fails instead of allowing the contract to absorb an unfunded state increase | Cover measured token storage and use the module’s refund path |
| An approval is added without enough storage deposit | The approval fails and the approved account is not added | Attach the storage cost for a new approved account |
| The receiver call fails or asks for a return | The resolver attempts to restore the previous owner and approvals while the receiver still owns the token | Implement a private resolver and keep receiver behavior compatible with nft_on_transfer |
| A client calls an absent enumeration extension, uses a zero limit, or gives an invalid page start | The view call fails instead of returning a token page | Configure enumeration before exposing it and page with valid non-zero limits |
| Metadata includes a media or reference URL without its required 32-byte hash | Metadata validation rejects the collection or token record | Set each URL and its matching hash as a pair |
| A caller uses an outdated core method signature or an incompatible external call shape | A forwarded call or ext_nft_core request fails before the intended NFT action | Keep implementers and external callers on the same core trait contract |
Key files
| File | Why to read it |
|---|---|
| near-contract-standards/…/core_impl.rs | Main token state, mint helper, ownership changes, direct transfers, call-based transfers, and resolver behavior |
| near-contract-standards/…/mod.rs | Core public interface, transfer requirements, receiver and resolver exports, and the external ext_nft_core call surface |
| near-contract-standards/…/token.rs | Token ID and returned token shape |
| near-contract-standards/…/mod.rs | Approval interface and its caller-facing rules |
| near-contract-standards/…/approval_impl.rs | Approval IDs, authorization changes, storage deposits, and refunds |
| near-contract-standards/…/receiver.rs and resolver.rs | Cross-contract receiver callback and transfer resolution contract |
| near-contract-standards/…/enumeration_impl.rs | Supply and paged ownership views |
| near-contract-standards/…/metadata.rs | Collection metadata, token metadata, and validation rules |
| near-contract-standards/…/events.rs | NEP-171 event types and indexer-facing log format |
| near-contract-standards/…/utils.rs | Storage-cost checks and approval refund support |
| examples/…/lib.rs | Complete contract-level composition of core, approval, enumeration, metadata, minting, and resolver interfaces |