Global contract support
The SDK supports a shared-code model in which one contract binary can serve many accounts. It lets an application publish code, select that code by a stable identifier, attach it to accounts, and create accounts with deterministic initial state. The near-global-contracts package provides shared identifiers and account-derivation and state-init types for these paths.
Shared global contract model
Global code identity
A global contract is contract code published for reuse across accounts. Its GlobalContractId identifies the code by either its 32-byte code hash or the account that published it. The selected form determines whether consumers depend on one exact code version or on code associated with a provider account. See Global contract identifiers and state initialization for the identifier rules.
References: near-global-contracts/…/global_contract_identifier.rs• L32-55 near-global-contracts/…/global_contract_identifier.rs• L89-103 near-sdk/…/lib.rs• L1620-1632
Contract-code context
Shared code still runs for one consumer account. The execution environment reports whether that account uses local code or global code. For global code, it also reports the selected hash or provider account through an optional GlobalContractId.
This separates the code source from the account that owns the state. Shared code can use the information when it must apply rules based on its source, while local contract execution remains distinguishable from global-contract execution.
References: near-sdk/…/env.rs• L207-249 near-sdk/…/context.rs• L60-69 near-sdk/…/mocked_blockchain.rs• L1090-1110
Deployment and use flow
Global code publication
Global code is published through a promise action. The SDK supports publication by code hash and by provider account. The caller supplies the contract bytes and the value needed by the runtime action.
The publication target becomes the account through which the code is available. Applications normally retain the chosen hash or provider account so later account creation or attachment selects the intended code.
References: near-sdk/…/promise.rs• L518-550 near-sdk/…/env.rs• L2102-2151 examples/…/lib.rs• L20-46
Global code attachment
An account begins using published code through a separate promise action. The consumer selects either an exact code hash or a provider account. After the action succeeds, calls to the consumer account execute the selected shared code while the consumer keeps its own account state.
| Lookup form | Consumer dependency |
|---|---|
| Code hash | The exact published code represented by that hash |
| Provider account | The global code associated with the named publishing account |
References: near-sdk/…/promise.rs• L552-583 near-sdk/…/env.rs• L2153-2209 examples/…/lib.rs• L48-72
Publication and attachment sequence
The lifecycle separates code publication from account use. The application records the selected identity between these steps and uses it when attaching shared code to a consumer account.
flowchart LR
Bytes[Contract bytes] --> Publish[Publish global code]
Publish --> Hash[Code hash]
Publish --> Provider[Provider account]
Hash --> Record[Application code record]
Provider --> Record
Record --> Select[Select global code]
Select --> Attach[Attach code to consumer account]
Attach --> Execute[Execute calls with shared code]
References: examples/…/lib.rs• L22-29 examples/…/lib.rs• L31-45 examples/…/lib.rs• L48-72
Deterministic account initialization
State initialization input
The SDK supports account creation from a complete state-initialization value. The value can select global code and carry initial data or account access settings. The universal account ID is derived from the exact canonical encoding of that value, so on-chain and off-chain code can calculate the same ID.
The standalone near-global-contracts package supplies the identifier and account-derivation surface for deterministic (NEP-616, 0s) and universal (NEP-655, 0u) accounts. It also defines the versioned UniversalStateInit value and its RawStateInit byte form. The SDK re-exports these types and supports typed derivation for known versions plus raw derivation for versions that the SDK does not yet understand. A factory, relayer, or wallet must preserve raw bytes when it forwards them. See Global contract identifiers and state initialization for the identifier and state-init rules, and Contract state and initialization for the wider state model.
References: near-global-contracts/Cargo.toml• L1-13 near-global-contracts/Cargo.toml• L21-43 near-global-contracts/…/lib.rs• L1-49 near-global-contracts/…/universal_state_init.rs• L10-27 near-global-contracts/…/universal_state_init.rs• L47-95 near-global-contracts/…/universal_state_init.rs• L244-270 near-sdk/…/env.rs• L30-34 near-sdk/…/env.rs• L2553-2619 near-sdk/…/env.rs• L2621-2683
Initialization actions
Initialization is an action in a promise batch. It funds the new account and can add initial data entries to the same state-init operation. The lower-level deterministic form supports selecting code by hash or provider account.
For universal accounts, the receiver must be the ID derived from the exact state-init bytes. The runtime checks this during receipt validation. A caller therefore derives the ID first, creates the promise for that receiver, and submits the matching initialization input.
References: near-sdk/…/env.rs• L1425-1533 near-sdk/…/env.rs• L2553-2582 near-sdk/…/env.rs• L2621-2683
Initialization funding and refunds
Initialization carries a deposit for the new account. If the action fails or does not use all of the deposit, the promise can send the refund to a chosen account.
This matters when a factory or relayer pays for an account on behalf of another party. The fund owner must not rely on the default recipient when that recipient differs from the account creating the action.
References: near-sdk/…/promise.rs• L483-493 near-sdk/…/promise.rs• L585-589 near-sdk/…/env.rs• L1399-1422 near-sdk/…/env.rs• L1425-1533
Shared-code lifecycle concerns
Code version selection
A code hash names one exact code version. A provider account creates an account-based dependency instead. Applications must manage that provider as a release dependency because the selected code can change independently of the consumer account.
The SDK supplies identifiers and actions. It does not choose release policy or maintain an application registry. See Contract SDK authoring for the broader SDK authoring context.
References: near-global-contracts/…/global_contract_identifier.rs• L32-55 near-sdk/…/promise.rs• L552-583 near-sdk/…/env.rs• L2153-2209 examples/…/lib.rs• L4-10 examples/…/lib.rs• L74-98
Per-account state boundaries
Global code is shared, but each consumer account remains the execution target and state owner. Its state and account-level funds stay separate from those of other consumers. The code source is therefore a different concern from the state boundary.
References: near-sdk/…/env.rs• L223-249 examples/…/lib.rs• L100-124 examples/…/workspaces.rs• L57-99
Asynchronous action outcomes
Publication, attachment, and initialization are promise actions. Creating the action does not confirm that the runtime published the code or made it usable by the target account. Callers must inspect the final outcome or handle failure in a callback before treating the lifecycle step as complete.
References: near-sdk/…/promise.rs• L496-503 examples/…/lib.rs• L105-124 examples/…/workspaces.rs• L139-188
Runtime support boundary
The SDK sends global-contract actions to host functions supplied by the NEAR runtime. A flow is usable only when the target network and its tools support those actions. Universal account derivation and universal state initialization require universal-account support that starts at nearcore protocol version 88 and shipped in nearcore 2.14.
Local unit tests can record actions without proving that a live network accepts them. The integration example uses a supported sandbox to check the live action flow, so runtime and test versions must match the protocol support expected by the application.
References: near-sys/…/lib.rs• L211-233 near-sdk/…/env.rs• L2564-2567 near-sdk/…/env.rs• L2635-2638 near-sdk/…/receipt.rs• L291-324 near-global-contracts/…/universal_state_init.rs• L268-271 examples/…/README.md• L64-77 examples/…/workspaces.rs• L7-32
Change checklist
When these files or sections are changed, remember to consider these:
- If
GlobalContractIdgains a new form, update publication, attachment, state initialization, serialization, and off-chain account-ID derivation together. - If hash or provider-account lookup changes, review application records and release flows that store the selected identity.
- If
UniversalStateInitgains a version or field, keep typed, raw, on-chain, and standalonenear-global-contractsderivation paths byte-for-byte compatible. - If canonical validation or raw-byte handling changes, review factories, relayers, wallets, and account-ID derivation that forward universal account data.
- If the receiver or encoding rule changes, update both account-ID derivation and account creation.
- If refund behavior changes, review factory flows that fund initialization for another account.
- If package features or dependency versions change in near-global-contracts/Cargo.toml, review the SDK re-export surface and off-chain, on-chain, serialization, schema, and runtime interoperability paths that those features enable.
- If host support changes, align SDK bindings, mock behavior, sandbox setup, and live network requirements.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A consumer selects a hash that is not published | Attachment fails and the target account does not gain the shared code | Store and validate the published hash before use. |
| A provider account is missing or exposes unexpected code | Account-based attachment fails or selects an unintended code source | Treat the provider as a controlled release dependency and verify the action result. |
| A global-contract or universal-account action runs on an unsupported runtime | Host action execution fails even though SDK code compiles | Use a supported network and a sandbox with matching protocol support. |
| A caller treats promise creation as completed work | Later calls can use an account without usable global code | Check the transaction result or handle failure in a callback. |
| State-init bytes change between ID calculation and submission | The runtime rejects the account creation because the receiver does not match the request | Derive the ID and submit one immutable state-init value. |
| Non-canonical state-init bytes are decoded and re-encoded | The new encoding can produce a different universal account ID | Validate canonical input and preserve original raw bytes when forwarding. |
| The universal state-init receiver differs from its derived ID | Receipt validation rejects account creation | Create the promise for the ID derived from the exact submitted bytes. |
| A relayer uses the default refund recipient for sponsored initialization | Unused or failed funds return to the wrong account | Set the promise refund account when the relayer and fund owner differ. |
Key files
| File | Why to read it |
|---|---|
| near-global-contracts/Cargo.toml | Defines the package purpose for global contract identifiers and deterministic and universal account derivation, plus the feature surface for serialization, schemas, and runtime interoperability. |
| near-global-contracts/…/lib.rs | Shows the package role and the types re-exported for direct off-chain and SDK use. |
| near-global-contracts/…/global_contract_identifier.rs | Defines the supported forms of global code identity. |
| near-global-contracts/…/universal_state_init.rs | Defines versioned universal initialization input, raw-byte validation, and account-ID derivation. |
| near-sdk/…/promise.rs | Shows the high-level actions for publishing, attaching, and initializing global code. |
| near-sdk/…/env.rs | Shows the execution context, global-code actions, and universal state-init boundary. |
| examples/…/lib.rs | Demonstrates publication, identity recording, attachment, and calls through shared code. |
| examples/…/workspaces.rs | Demonstrates live deployment, hash-based use, account-based use, and failure checks. |
| near-sys/…/lib.rs | Defines the runtime boundary for global-contract host actions. |
| docs/global-contract-identifiers-and-state-init.md | Explains the identifier and state-init contracts in more detail. |
| docs/contract-state-and-initialization.md | Explains how shared-code initialization relates to contract state. |
| docs/contract-sdk-authoring.md | Explains the SDK authoring context around shared-contract use. |