Near Learn

Make it cheap · Intermediate

Gas & storage

NEAR contracts pay two separate costs: gas for computation, burnt on every call, and storage staking, NEAR that stays locked for every byte the contract stores, including its own code. Most savings come from understanding which of the two a pattern affects.

This module collects practical patterns for cutting both: measuring before optimising, choosing collections, shrinking state and Wasm, making users pay for the storage they add, budgeting gas for cross-contract calls, and reclaiming locked NEAR when data is no longer needed. Examples use near-sdk 5.x.

10 lessons~60 min30 quiz questionsFree · no sign-up

What you’ll learn

  • Tell gas fees apart from storage staking, and know who pays each
  • Measure gas with env::used_gas(), transaction outcomes and near-workspaces sandbox tests
  • Pick between LookupMap, IterableMap, Vector and friends, with safe storage prefixes
  • Estimate the bytes and NEAR each stored entry costs, and shrink them
  • Charge users for storage with deposits and refunds, or the NEP-145 standard
  • Keep the root contract struct small with lazily loaded state
  • Configure a size-optimised release build and keep Wasm small
  • Budget static gas and gas weights for cross-contract calls and callbacks
  • Paginate views and keep every loop bounded
  • Release staked NEAR by deleting data, migrating old state and removing unused accounts

Lessons