Introducing a new way to review your PRs.

Read the announcement

near / near-sdk-rs

Rust library for writing NEAR smart contracts

View on GitHub

20 documents · Updated Nov 20, 2026

docs / contract-standards

Reusable contract standards

The near-contract-standards package gives NEAR Rust contracts shared interfaces and ready-to-use behavior for token contracts and related account services. Its main aim is interoperability: a contract can expose common token methods, accept token calls from other contracts, report metadata and events in known formats, and account for the storage that its users fund.

Standards scope

The package groups standards by the external behavior that another contract, wallet, or indexer can rely on. The token modules provide the main product behavior. The storage, metadata, and event modules make that behavior usable across contract boundaries. The package also supports NEAR SDK compatibility needs and optional ABI generation for published documentation.

Standard families

FamilySystem purposeMain interoperability result
Fungible tokensRepresent divisible balances and supplyContracts can transfer, query, and receive NEP-141 tokens
Non-fungible tokensRepresent individually owned token IDsContracts can transfer, query, enumerate, and approve NEP-171 tokens
Storage managementFund state that grows per account or tokenCallers can register, inspect, and close storage-backed records
MetadataDescribe a token collection and its assetsWallets and other clients can identify and render tokens
EventsReport token changes to indexersIndexers can consume standard mint, transfer, and burn records
Contract source metadataDescribe source and supported standardsTools can inspect the deployed contract's identity and standards

near-contract-standards/…/lib.rs is the package-level map of these families. The fungible-token and non-fungible-token pages describe their token-specific behavior. See Fungible token standard, Non-fungible token standard, and Storage management and contract upgrades.

References: near-contract-standards/…/lib.rs• L5-23 near-contract-standards/README.md• L1-6 near-contract-standards/Cargo.toml• L16-31

Interface boundaries

Each public standard is expressed as a contract interface. A host contract connects the interface to its own entry points and state. The same interface also supports type-safe calls from a contract that uses another contract's standard.

The boundary separates shared protocol meaning from product rules. For example, a token contract decides who may mint or burn. The standard defines how balances, owners, transfers, and callbacks are exposed to other actors. The NFT core interface also exposes a generated cross-contract call surface, so callers can use the same transfer and query contract as any implementation of the standard.

References: near-contract-standards/…/core.rs• L5-12 near-contract-standards/…/core.rs• L54-82 near-contract-standards/…/mod.rs• L16-22 near-contract-standards/…/mod.rs• L55-72

Token behavior

The two token families share a contract pattern but serve different assets. Fungible tokens move numeric balances. Non-fungible tokens move unique string token IDs and their ownership records.

Fungible token model

The fungible-token standard models a supply with one balance per registered account. Its core interface lets users transfer tokens and query both an account balance and total supply. Its transfer-call form sends tokens to a receiving contract with a message, then resolves unused tokens back to the sender.

The reusable token object supplies the common accounting behavior. A host contract adds product-specific issuance and policy around it. Read Fungible token standard for the full token flow and extension choices.

References: near-contract-standards/…/core.rs• L82-131 near-contract-standards/…/core_impl.rs• L19-38 near-contract-standards/…/core_impl.rs• L121-163

Non-fungible token model

The non-fungible-token standard models each asset as a unique string token ID with an owner. A token may also carry token metadata and approved accounts when those extensions are enabled. Its core interface supports direct transfer, transfer calls, and lookup of one token, while also exposing a type-safe cross-contract call boundary for those actions.

Enumeration adds collection and owner views. Approval management adds controlled transfer rights for a specific token. These are optional extensions, so a contract exposes only the views and actions that match its product rules. Read Non-fungible token standard for the ownership and extension flows.

References: near-contract-standards/…/token.rs• L7-18 near-contract-standards/…/mod.rs• L72-148 near-contract-standards/…/mod.rs• L43-87 near-contract-standards/…/mod.rs• L58-132

Transfer-call flow

Transfer calls let token movement start an action in another contract while keeping a resolution step in the token contract. The receiving contract reports whether it used the asset. A failed call or a request to return the asset causes the standard to refund fungible tokens or restore an NFT when that remains safe.

The sequence below shows the shared relationship between a token contract, a receiver contract, and the final resolver.

sequenceDiagram
    participant Sender
    participant Token as Token contract
    participant Receiver as Receiver contract
    Sender->>Token: transfer_call(asset, message)
    Token->>Token: Move asset to receiver
    Token->>Receiver: receiver callback(asset, message)
    Receiver-->>Token: used amount or return decision
    Token->>Token: Resolve transfer
    Token-->>Sender: Refund unused amount or restore asset

Receiver contracts must treat the callback as a controlled token entry point. Resolver callbacks must stay private to the token contract because they finalize ownership or balance changes after an external call.

References: near-contract-standards/…/receiver.rs• L4-30 near-contract-standards/…/resolver.rs• L40-47 near-contract-standards/…/receiver.rs• L4-29 near-contract-standards/…/resolver.rs• L28-62

Shared contract services

Token standards depend on common services that keep state use visible to callers and state changes visible to indexers. These services do not define a token's business policy. They define the external records and resource rules around that policy.

Storage accounting

Storage management links account registration to the NEAR deposit that pays for contract state. It gives callers a common way to deposit storage funds, inspect required and available balances, withdraw available funds without removing data, and unregister an account.

The basic fungible-token service uses a fixed account storage requirement. NFT contracts also need to account for token records and optional extension data. The storage rules therefore form a contract between every state-growing action and the account that funds it. Read Storage management and contract upgrades for the shared storage flow.

References: near-contract-standards/…/mod.rs• L3-13 near-contract-standards/…/mod.rs• L78-120 near-contract-standards/…/storage_impl.rs• L50-115 near-contract-standards/…/core_impl.rs• L295-373

Metadata records

Metadata gives clients stable descriptions without changing the core token actions. Fungible-token metadata describes the token name, symbol, icon, external reference, and decimal scale. NFT metadata has two levels: collection metadata describes the contract and token metadata describes an individual asset.

Reference fields are paired with hashes when present. This lets clients use external content while checking that the referenced content matches the published record. The package also defines contract source metadata under NEP-330. That record identifies source, supported standards, and optional reproducible-build details; it is separate from token display metadata.

References: near-contract-standards/…/metadata.rs• L4-30 near-contract-standards/…/metadata.rs• L4-18 near-contract-standards/…/metadata.rs• L20-67 near-contract-standards/…/contract_metadata.rs• L4-13 near-contract-standards/…/contract_metadata.rs• L54-80

Event records

Events turn state changes into records that indexers can consume. The package uses the NEAR event format and provides mint, transfer, and burn events for both fungible and non-fungible tokens. Each event identifies its standard and version, then carries one or more change records.

The event layer is shared at the package boundary. Token-specific event types supply the fields that describe ownership or balance changes. A contract should emit an event when the corresponding user-visible token action succeeds so downstream systems can keep their views in step with contract state.

References: near-contract-standards/…/event.rs• L5-29 near-contract-standards/…/events.rs• L1-14 near-contract-standards/…/events.rs• L21-95 near-contract-standards/…/events.rs• L1-14 near-contract-standards/…/events.rs• L20-99

Upgrade boundary

Upgrade support is not a current NEAR contract standard in this package. The package still exposes a deprecated legacy module for contracts that use an owner-controlled staging and deployment pattern. New contract design should treat upgrade policy as an application concern and read the shared upgrade page before relying on this legacy behavior.

Legacy upgrade support

The legacy model stages contract code for a delay, then lets the contract deploy the staged code after the delay. It also includes an owner check and a migration hook for state changes between versions. The package marks this module as deprecated because no NEP defines an interoperable upgrade standard. The package enables the NEAR SDK legacy feature for this compatibility surface and provides an optional abi feature for ABI generation.

Read Storage management and contract upgrades for the upgrade boundary and its relation to storage state.

References: near-contract-standards/Cargo.toml• L16-31 near-contract-standards/…/lib.rs• L13-19 near-contract-standards/…/mod.rs• L6-24 near-contract-standards/…/mod.rs• L27-78

Change checklist

When these files or sections are changed, remember to consider these:

  • Changing a public trait method can break both host contract entry points and cross-contract callers. Review the matching receiver and resolver interfaces together.
  • Changing token or approval state can invalidate storage deposits and refunds. Review storage bounds and account-close behavior with the token state change.
  • Changing metadata fields or standard versions can affect wallets and indexers that parse the published JSON shape.
  • Changing event names, versions, or fields can make indexer records diverge from token state. Review both token event types and the shared event envelope.
  • Changing upgrade state or migration rules can make stored contract state incompatible with new code. Review the deployment delay and migration contract as one lifecycle.

Important failure modes

TriggerConsequenceGuard
A transfer-call receiver fails or returns an invalid resultFungible tokens need a safe refund path. NFTs need a safe restore path.Keep the resolver in the token contract and apply the standard resolution rules.
A caller uses token state without required storage registrationThe contract can grow state without enough NEAR to pay for it.Require storage registration and enforce the reported minimum balance.
An NFT transfer uses a stale approval IDThe transfer is rejected rather than using an older authorization record.Validate the supplied approval ID against the current approval and clear approvals after a successful transfer.
A metadata reference is published without its matching hashClients cannot verify the referenced content under the metadata contract.Require the reference and hash to appear together with a valid hash length.
A legacy upgrade is deployed before its staging period ends or without staged codeDeployment fails or the upgrade process violates its safety delay.Enforce the owner boundary, staging time, and staged-code presence.

Key files

FileWhy to read it
near-contract-standards/…/lib.rsShows the package's public standard families and the deprecated upgrade boundary.
near-contract-standards/Cargo.tomlShows the package's NEAR SDK compatibility feature, optional ABI feature, and unit-test support.
near-contract-standards/…/core.rsDefines the interoperable fungible-token actions and queries.
near-contract-standards/…/mod.rsDefines the interoperable NFT ownership, transfer, and cross-contract call actions.
near-contract-standards/…/mod.rsDefines the shared storage funding and account lifecycle contract.
near-contract-standards/…/metadata.rsDefines the fungible-token metadata shape and validation rules.
near-contract-standards/…/metadata.rsDefines collection-level and token-level NFT metadata.
near-contract-standards/…/events.rsShows the standard fungible-token event records.
near-contract-standards/…/events.rsShows the standard NFT event records.
near-contract-standards/…/contract_metadata.rsDefines source, supported-standard, and build metadata for deployed contracts.
near-contract-standards/…/mod.rsDocuments the deprecated legacy upgrade lifecycle.