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 / storage-management-and-upgrades

Storage management and contract upgrades

The storage management and upgrade modules define two shared concerns for NEAR contracts. Storage management makes callers fund the state that their accounts or assets add, while the upgrade module provides a delayed code deployment pattern for legacy contracts. Together they set important boundaries for token integrations, account refunds, access control, and state migration.

This page expands the contract standards overview. Read it with the fungible token standard and non-fungible token standard pages to see how these shared concerns fit into token contracts.

Shared module roles

Storage accounting

Storage accounting links NEAR state usage to the account or caller that causes that state to exist. The shared storage interface reports the required deposit and exposes registration, withdrawal, and account removal actions, so a contract can avoid paying for unbounded user state from its own balance. near-contract-standards/…/mod.rs• L1-13

The interface is designed for cross-contract use. A wallet, token contract, or other application can discover the required storage deposit through the same balance types and method names without knowing how the contract stores its data. near-contract-standards/…/mod.rs• L78-120

References: near-contract-standards/…/mod.rs• L1-13 near-contract-standards/…/mod.rs• L78-120

Upgrade staging

The upgrade module models a staged code change. An owner places new Wasm code in a temporary staging area with a future activation time, then a later call deploys that code to the current account. The delay gives operators time to review or stop a planned change before deployment. near-contract-standards/…/mod.rs• L14-24 near-contract-standards/…/mod.rs• L27-38

This module is deprecated because the repository does not define an upgrade standard. Treat it as a legacy helper rather than a current contract interface. near-contract-standards/…/lib.rs• L13-19

References: near-contract-standards/…/mod.rs• L14-24 near-contract-standards/…/mod.rs• L27-38 near-contract-standards/…/lib.rs• L13-19

Storage management interface

Account registration

Registration creates the state needed for an account and requires an attached deposit that covers the minimum storage balance. A repeat registration does not create another account record. The contract returns the attached amount when the account already exists or when the deposit exceeds the required minimum. near-contract-standards/…/mod.rs• L80-86 near-contract-standards/…/storage_impl.rs• L50-78

The account identifier can be supplied by the caller or can default to the predecessor. This lets one account fund another account's registration, while refunds still go to the account that made the call. near-contract-standards/…/storage_impl.rs• L53-75

References: near-contract-standards/…/mod.rs• L80-86 near-contract-standards/…/storage_impl.rs• L50-78

Storage balances

StorageBalance separates the total deposit held for an account from the part that the account may withdraw. StorageBalanceBounds gives the minimum required amount and an optional maximum. Consumers should query these values instead of assuming a fixed NEAR amount. near-contract-standards/…/mod.rs• L4-13

The basic fungible-token implementation measures the state cost of one account and sets the minimum and maximum to that same value. Its available balance is therefore zero. The interface still supports withdraw calls for compatibility, but a positive withdrawal is rejected and a zero-amount call only returns the current balance. near-contract-standards/…/core_impl.rs• L29-57 near-contract-standards/…/storage_impl.rs• L80-110

References: near-contract-standards/…/mod.rs• L4-13 near-contract-standards/…/core_impl.rs• L29-57 near-contract-standards/…/storage_impl.rs• L80-110

Account removal

Unregistration removes the predecessor's registered state and returns the storage deposit. It is safe by default only when the account has no positive token balance. A force option allows the contract to close the account while burning that balance, so the host contract must decide whether and how to report the closed account. near-contract-standards/…/mod.rs• L104-115 near-contract-standards/…/storage_impl.rs• L8-35

Withdrawal and unregistration require exactly one yoctoNEAR. This check protects restricted methods from calls made through an access key that should not be able to perform the action. near-contract-standards/…/mod.rs• L88-115

References: near-contract-standards/…/mod.rs• L88-115 near-contract-standards/…/storage_impl.rs• L8-35

Token integration boundaries

Fungible-token integration

The fungible-token module directly implements the storage management trait. A host contract exposes the shared methods by forwarding them to its embedded FungibleToken, which keeps account registration and token balance state under one storage policy. near-contract-standards/…/storage_impl.rs• L50-115 examples/…/lib.rs• L140-174

This boundary also gives the host contract a hook when an account closes. The hook can handle application-level cleanup or logging, but it must respect the token implementation's result and the effect of forced removal on total supply. near-contract-standards/…/macros.rs• L64-114

References: near-contract-standards/…/storage_impl.rs• L50-115 examples/…/lib.rs• L140-174 near-contract-standards/…/macros.rs• L64-114

Non-fungible-token storage

The standard non-fungible-token module does not implement the shared StorageManagement trait. It instead accounts for storage when tokens are minted and refunds the caller for the measured increase. This keeps token creation funded without presenting the same per-account registration API used by the basic fungible-token implementation. near-contract-standards/…/core_impl.rs• L25-54 near-contract-standards/…/core_impl.rs• L323-373

NFT extensions add state for metadata, owner listings, and approvals. The storage charged during minting therefore depends on the enabled extensions. An NFT host contract must preserve that funding and refund relationship when it adds its own mint flow. examples/…/lib.rs• L74-107

References: near-contract-standards/…/core_impl.rs• L25-54 near-contract-standards/…/core_impl.rs• L323-373 examples/…/lib.rs• L74-107

Transfer and callback interoperability

Storage policy sits beside token transfer callbacks rather than replacing them. A fungible-token or non-fungible-token transfer can call another contract and later resolve the result, so account removal or state changes during the callback must still leave token ownership and storage records consistent. near-contract-standards/…/core_impl.rs• L121-153 near-contract-standards/…/core_impl.rs• L390-424

The resolver handles the case where a sender account disappears while a transfer is in flight. For fungible tokens, an unrefundable amount is burned when the sender no longer exists. For NFTs, a failed callback can restore ownership only when the receiver still owns the token. near-contract-standards/…/core_impl.rs• L165-232 near-contract-standards/…/core_impl.rs• L428-481

References: near-contract-standards/…/core_impl.rs• L121-153 near-contract-standards/…/core_impl.rs• L165-232 near-contract-standards/…/core_impl.rs• L390-424 near-contract-standards/…/core_impl.rs• L428-481

Upgrade lifecycle

Ownership and staging

The legacy upgrade helper uses an owner account as the authority to change ownership or stage code. The requested deployment time must be later than the current block time plus the configured staging duration. Staging a new code blob replaces the pending blob and sets a new activation time. near-contract-standards/…/mod.rs• L6-12 near-contract-standards/…/mod.rs• L40-65

The helper does not apply the owner check inside deploy_code. A contract that exposes this operation must add its own access control or private-call boundary. The time gate protects the delay, but it does not by itself identify the caller who may deploy. near-contract-standards/…/mod.rs• L56-78

References: near-contract-standards/…/mod.rs• L6-12 near-contract-standards/…/mod.rs• L40-78

Deployment and migration

Deployment succeeds only after the staging time. The helper reads the pending code, removes it, and returns a promise that deploys the code to the current account. A missing code blob or an early call stops the operation. near-contract-standards/…/mod.rs• L67-78

The upgrade flow separates approval, waiting, deployment, and state conversion. This diagram shows the relationship between those stages.

flowchart LR
    A[Owner stages code] --> B[Staging delay]
    B --> C[Deploy code]
    C --> D[Run migration]
    D --> E[Serve new state]

Code deployment and state migration are separate responsibilities. The Upgradable trait provides a migration hook with no default migration logic, so the new contract must define how old state becomes valid new state. The SDK also provides direct state storage reads and writes for migration flows that cannot deserialize old state as the new type. near-contract-standards/…/mod.rs• L14-24 near-sdk/…/env.rs• L2547-2602

One supported migration pattern keeps old and new state variants together. The first call through the shared contract path converts an old variant into the current one before changing business state. examples/…/lib.rs• L7-31

References: near-contract-standards/…/mod.rs• L14-24 near-contract-standards/…/mod.rs• L67-78 near-sdk/…/env.rs• L2547-2602 examples/…/lib.rs• L7-31

Integration responsibilities

Contract composition

A contract that embeds a standard module owns the public integration boundary. It must expose the traits that external callers need, attach the required method attributes and deposits, and forward results without changing their meaning. The fungible-token example composes core token calls, transfer resolution, storage management, and metadata as separate interfaces over one contract state. examples/…/lib.rs• L95-180

The same principle applies to upgrade code. The host contract must choose who can stage and deploy, when migration runs, and how the old state remains readable during the change. The shared helper supplies a narrow lifecycle, not a complete governance or migration policy. near-contract-standards/…/mod.rs• L6-24

References: examples/…/lib.rs• L95-180 near-contract-standards/…/mod.rs• L6-24

External contract expectations

External callers can rely on stable method and data shapes only when the host contract exposes the matching standard trait. Storage clients should use storage_balance_bounds and storage_balance_of before funding or withdrawing. Token receivers should implement the matching callback and resolver path when they use transfer calls. near-contract-standards/…/mod.rs• L78-120 near-contract-standards/…/core.rs• L1-82 near-contract-standards/…/mod.rs• L55-72

Storage funding is not a general permission grant. Registration pays for state, while one yoctoNEAR checks protect state-changing methods and contract-specific authorization still decides who may mint, transfer, remove accounts, or upgrade code. This distinction is essential when several standards share one contract. near-contract-standards/…/mod.rs• L88-115 examples/…/lib.rs• L90-107

References: near-contract-standards/…/mod.rs• L78-120 near-contract-standards/…/core.rs• L1-82 near-contract-standards/…/mod.rs• L55-72 examples/…/lib.rs• L90-107

Change checklist

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

  • Update the storage balance calculation whenever an account record or its related collections change. The reported minimum must continue to cover the state created by registration.
  • Check all fungible-token account-close hooks when forced unregistration changes token supply or removes an account that has a pending transfer callback.
  • Keep the storage key layout stable when changing token state. If the layout must change, provide a migration path before new methods read the state.
  • Treat any upgrade entry point as a security boundary. Add caller checks around deployment and make the migration call order explicit.
  • Preserve the external storage method shapes and balance semantics when composing a contract with other standards.

Important failure modes

TriggerConsequenceGuard
Registration deposit is below the reported minimumThe account is not registered and the call panics.Query storage_balance_bounds and attach at least min.
Positive amount passed to basic fungible-token storage_withdrawThe call panics because the implementation has no available balance.Read storage_balance_of and withdraw only a reported available amount.
Unregistering an account with a positive token balance without forceThe account stays registered and the call panics.Transfer or burn the balance first, or use force only when the application accepts the burn.
Storage methods omit the required one yoctoNEARThe protected call fails before it changes state.Attach exactly one yoctoNEAR to withdrawal and unregistration calls.
Staging code with a timestamp inside the staging windowThe code is rejected and no new staging time is set.Choose a timestamp later than the current block time plus the staging duration.
Deploying before the staging time or without staged codeDeployment panics and no code deployment promise is returned.Wait for the time gate and verify that a code blob is staged.
Host contract exposes deploy_code without its own caller checkAny caller that can reach the method may trigger the pending deployment after the delay.Keep deployment private or enforce owner authorization in the host contract.
New code reads old state without migrationState decoding or business behavior can fail after deployment.Convert or read the old state before exposing the new state shape.

Key files

FileWhy to read it
near-contract-standards/…/mod.rsDefines the shared storage balance types and the external account storage contract.
near-contract-standards/…/storage_impl.rsShows how registration, refunds, withdrawal limits, and forced account removal work for fungible tokens.
near-contract-standards/…/core_impl.rsShows the per-account state cost and how account removal interacts with token supply and transfer resolution.
near-contract-standards/…/core_impl.rsShows NFT storage measurement, mint refunds, and callback recovery without the shared storage interface.
near-contract-standards/…/mod.rsDefines the deprecated owner, staging, deployment, and migration helper.
near-contract-standards/…/lib.rsRecords which modules are current standards and which upgrade module is deprecated.
examples/…/lib.rsDemonstrates how a host contract exposes the token and storage interfaces together.
examples/…/lib.rsDemonstrates lazy conversion from an old state variant to a new state variant.
near-sdk/…/env.rsDocuments the low-level storage operations used by migration code and upgrade staging.