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 / near-host-abi-bindings

NEAR host ABI bindings

near-sys is the lowest layer that connects a NEAR smart contract compiled to WebAssembly with the NEAR runtime. It describes the host function ABI (application binary interface): the names, argument shapes, return values, pointers, and output channels that let higher layers read runtime data, use storage, create promises, and call built-in cryptographic operations. The safer near_sdk::env layer and the narrower near-sdk-env layer sit above these bindings. The crypto and host interfaces page places this boundary in the wider system.

System role

The host binding layer turns a contract's request into a call across the WebAssembly boundary. It does not decide how to represent account IDs, gas, tokens, promise results, or storage errors. Those rules belong to the environment and SDK layers that use the bindings.

ABI contract

The ABI is a set of unsafe external C-unwind function declarations in near-sys/…/lib.rs. On WebAssembly, the declarations import functions from the runtime's env module. Outside WebAssembly, the same declarations resolve to the SDK unit-test mock, whose host calls can panic and be caught by a test. The runtime or mock supplies the function bodies, so near-sys provides the call contract rather than a local implementation.

The declarations use fixed-width integers for handles and lengths. Byte inputs cross the boundary as a pointer plus a length. Some calls return a scalar or a status code. Other calls write bytes into a host-managed register, which the caller must read after the call. The surface also includes optional deterministic-account operations, post-quantum signature verification, BLS12-381 operations, and a compatibility alias for the current block height.

References: near-sys/…/lib.rs• L1-16 near-sys/…/lib.rs• L43-83 near-sys/…/lib.rs• L321-352 near-sys/Cargo.toml• L1-22 near-sys/CHANGELOG.md• L8-45

Data exchange model

Registers are host-managed byte buffers that carry variable-size data between the runtime and the contract. A host call receives a register ID for its output. The caller then asks for the register length and copies the bytes into contract memory.

This model supports context values such as account IDs and input data. It also carries hash results, storage values, promise results, universal-account state, and curve-operation results. Fixed-size values such as balances use a caller-provided memory address instead. The wrapper layer must interpret every status code and output register according to the ABI contract.

References: near-sys/…/lib.rs• L12-14 near-sys/…/lib.rs• L18-31 near-sys/…/lib.rs• L35-59 near-sys/…/lib.rs• L252-300 near-sys/…/lib.rs• L304-317 near-sdk/…/env.rs• L65-83 near-sdk/…/env.rs• L125-165

Runtime capability groups

The bindings expose the main capabilities that a contract needs from the NEAR runtime. Each group represents a different contract concern and feeds a matching environment or SDK feature.

CapabilitySystem purpose
Context and economicsRead the current account, call origin, block data, balances, gas, and storage usage.
Registers and return valuesMove variable-size data into and out of the runtime.
StorageRead, write, remove, and iterate over contract key-value data.
Promises and actionsSchedule calls, chain or combine them, add account and contract actions, use global contracts, and manage yield operations.
Promise resultsInspect asynchronous results and return a promise to the caller.
Cryptography and mathHash data, verify classical and post-quantum signatures, and run selected pairing and curve operations, including BLS12-381 operations. See Cryptographic hash utilities, Digest algorithm implementations, and BLS12-381 cryptography.
Logging and terminationReturn data, write logs, or stop execution with a panic or abort.
Validator and universal-account operationsRead validator stake and support universal-account state actions.

The grouping is conceptual rather than a second API. The runtime sees individual host calls, while the higher layers combine them into typed operations and user-facing workflows.

References: near-sys/…/lib.rs• L12-92 near-sys/…/lib.rs• L96-248 near-sys/…/lib.rs• L252-317 near-sys/…/lib.rs• L321-341 near-sys/CHANGELOG.md• L31-45 near-sys/CHANGELOG.md• L80-96

Layer relationships

The host ABI is the base of a small stack. The stack keeps raw memory rules and runtime status codes near the boundary while presenting more useful types and behavior to contract authors.

The diagram shows how contract code reaches the runtime through typed and narrow environment layers. Local hash work can use a Rust fallback when no NEAR host is present.

flowchart LR
    A[Contract code] --> B[near_sdk::env]
    B --> C[near-sdk-env]
    B --> D[near-sys]
    C --> D
    D --> E[NEAR VM host functions]
    C --> F[Pure-Rust local fallback]
    G[SDK Promise and state APIs] --> B

Most contract code starts at the SDK or its environment API. Hash helpers may pass through near-sdk-env. Both paths reach near-sys when they need the NEAR host.

near-sdk-env abstraction

near-sdk-env is a narrow abstraction over near-sys. Its main public role is to provide hash functions and related panic and abort behavior with the same contract-facing shape on WebAssembly and in local code.

On a contract target, its host branch calls near-sys and reads the result from a reserved register. On a non-contract target, its local branch uses Rust hash crates instead. This lets off-chain code compute the same hash values without requiring the NEAR VM. The broader runtime context and promise APIs remain in near_sdk::env.

References: near-sdk-env/…/lib.rs• L1-4 near-sdk-env/…/lib.rs• L23-44 near-sdk-env/…/hash.rs• L15-29 near-sdk-env/…/hash.rs• L68-97 near-sdk-env/…/macros.rs• L1-23

near_sdk::env safety layer

near_sdk::env is the primary typed wrapper around the host ABI. It converts raw runtime data into SDK types such as AccountId, NearToken, Gas, and PromiseIndex. It also turns register absence, invalid data, and host status values into Option, Result, booleans, or controlled aborts.

The wrapper owns common boundary rules. It validates account IDs, decodes balances, limits register reads, maps promise results, and uses reserved registers for temporary output. Contract authors should use this layer for direct environment access. For cross-contract work, the SDK's Promise and PromiseOrValue APIs provide a higher-level model than raw promise indices.

References: near-sdk/…/env.rs• L1-8 near-sdk/…/env.rs• L24-42 near-sdk/…/env.rs• L59-165 near-sdk/…/env.rs• L192-215 near-sdk/…/env.rs• L1065-1085 near-sdk/…/env.rs• L2163-2175

Direct sys access

Direct binding access is an escape hatch for code that needs the exact host ABI. The SDK re-exports near_sys as near_sdk::sys only when its unstable feature is enabled. This surface exposes raw pointers, lengths, registers, and status codes, so the caller must supply the memory and runtime assumptions that the environment layer normally checks.

Use direct access only when a required host capability is not available through a typed wrapper. A change to a raw binding can affect every layer above it because the function signature and its data rules form a protocol with the runtime. Current package metadata lists global-contracts, deterministic-account-ids, and universal-accounts features. Release notes also record that global-contract feature gates were removed in an earlier release, so consumers must follow the current feature behavior rather than rely on older gate assumptions.

References: near-sdk/…/lib.rs• L1600-1606 near-sdk/Cargo.toml• L21-35 near-sdk/Cargo.toml• L113-119 near-sys/Cargo.toml• L17-24 near-sys/CHANGELOG.md• L59-72

Execution targets

The same conceptual environment can run in different places, but only a NEAR host can provide real blockchain state. Build and test conditions select whether a call reaches the host, a mock runtime, or a local Rust implementation.

On-chain contract execution

For a deployed contract, WebAssembly imports the host functions from the runtime's env module. The wasm32 target selects this host path. The host supplies context, storage, promise, cryptographic, and termination behavior during contract execution.

The ABI is therefore part of the contract-runtime boundary. A contract can compile while still failing at execution if it targets the wrong environment or uses a function that the running protocol does not support.

References: near-sys/…/lib.rs• L1-8 near-sdk-env/…/macros.rs• L1-23 near-sdk-env/…/lib.rs• L6-21

Unit-test and local execution

Unit-tested contracts use the SDK's mocked blockchain interface to supply runtime behavior outside a real chain. The non-WebAssembly ABI declarations resolve to that mock path, where host calls can panic and tests can catch the unwind. The unit-testing feature connects the SDK environment to the mock, while near-sdk-env can use pure-Rust implementations for local hash work when the host path is not selected.

This split preserves the public environment shape but does not make local execution a full replacement for a deployed runtime. Tests that depend on host protocol behavior still need a matching mock or an on-chain check.

References: near-sys/…/lib.rs• L3-8 near-sdk-env/…/lib.rs• L31-49 near-sdk-env/…/macros.rs• L1-23 near-sdk/Cargo.toml• L130-140

Change checklist

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

  • A host function name, argument type, pointer rule, or return code change must stay aligned between near-sys, the environment wrapper, and the NEAR runtime protocol.
  • A new variable-size output must define its register ownership, absence behavior, expected length, and bounded-read behavior before a typed wrapper exposes it.
  • A feature-gated binding must have matching feature propagation in near-sdk and any layer that calls it. Check current feature behavior against the release history because older global-contract gate assumptions are no longer valid.
  • A target-configuration change must preserve the intended split between on-chain host calls, mocked unit tests, and pure-Rust local fallbacks.
  • A cryptographic host change must be checked against the corresponding hash, signature, or curve abstraction so on-chain and off-chain results remain compatible. See runtime environment and context for the higher-level environment contract.

Important failure modes

TriggerConsequenceGuard
A raw host call uses an invalid pointer or length.The runtime can read the wrong memory or the contract can fail at the ABI boundary.Use near_sdk::env wrappers unless direct ABI access is required.
A caller reads a register without checking whether the host populated it.The contract can abort when it expects data that is absent.Follow the register status contract and use bounded environment readers.
A callback reads an unbounded result from an unknown contract.A large result can consume gas and cause an out-of-gas failure.Use promise_result_checked with an application limit.
A contract uses a host function that the running protocol does not support.Execution fails even though the Wasm module compiled.Check the host requirement and protocol support before deployment.
A feature is enabled in the SDK but not in the binding layer.The needed symbol or typed API is not available for the build.Propagate the feature through every dependent crate and check the current near-sys feature contract.

Key files

FileWhy to read it
near-sys/…/lib.rsDefines the raw NEAR host ABI and groups the runtime capabilities exposed to contracts.
near-sys/Cargo.tomlDefines the binding package version, supported feature names, and documentation feature policy.
near-sys/CHANGELOG.mdRecords host capability additions and protocol-alignment changes across releases.
near-sdk-env/…/lib.rsExplains the narrow bridge between host calls and local Rust behavior.
near-sdk-env/…/macros.rsShows how host and local execution paths are selected.
near-sdk-env/…/hash.rsShows the register-backed host hash path and the matching local digest path.
near-sdk/…/env.rsShows how raw calls become typed context, storage, promise, crypto, and result APIs.
near-sdk/…/lib.rsShows the public boundary between the stable environment API and the unstable raw sys re-export.
near-sdk/Cargo.tomlShows dependency and feature links between the SDK, near-sys, and near-sdk-env.