Skip to main content

Module env

Module env 

Source
Expand description

Blockchain-specific methods available to the smart contract that allow to interact with NEAR runtime. This is a wrapper around a low-level near_sys.

Unless you know what you are doing prefer using env::* whenever possible.

In case of cross-contract calls prefer using higher-level API available through crate::Promise, and crate::PromiseOrValue<T>.

§Hash function naming

The older hashes come in a Vec<u8> form and an _array form (sha256 and sha256_array). Hashes added from nearcore 2.14 on follow a single convention instead: the bare name returns the fixed-size array, so sha3_256 returns [u8; 32] and there is no sha3_256_array. The Vec/_array pair is kept for the functions that already had it and is not extended to new hashes.

Functions§

abort
account_balance
The balance attached to the given account. This includes the attached_deposit that was attached to the transaction
account_locked_balance
The balance locked for potential validator staking.
alt_bn128_g1_multiexp
Compute alt_bn128 g1 multiexp.
alt_bn128_g1_sum
Compute alt_bn128 g1 sum.
alt_bn128_pairing_check
Compute pairing check
attached_deposit
The balance that was attached to the call that will be immediately deposited before the contract execution starts
block_height
Returns the height of the block the transaction is being executed in.
block_indexDeprecated
Current block index.
block_timestamp
Current block timestamp, i.e, number of non-leap-nanoseconds since January 1, 1970 0:00:00 UTC.
block_timestamp_ms
Current block timestamp, i.e, number of non-leap-milliseconds since January 1, 1970 0:00:00 UTC.
bls12381_g1_multiexp
Computes the BLS12-381 G1 multiexponentiation.
bls12381_g2_multiexp
Computes the BLS12-381 G2 multiexponentiation.
bls12381_map_fp2_to_g2
Maps Fp2 elements to BLS12-381 G2 points.
bls12381_map_fp_to_g1
Maps Fp elements to BLS12-381 G1 points.
bls12381_p1_decompress
Decompresses BLS12-381 G1 points.
bls12381_p1_sum
Computes the BLS12-381 G1 sum.
bls12381_p2_decompress
Decompresses BLS12-381 G2 points.
bls12381_p2_sum
Computes the BLS12-381 G2 sum.
bls12381_pairing_check
Performs a BLS12-381 pairing check.
chain_id
Returns the chain ID of the NEAR network the contract is deployed on, as configured in the network’s genesis.config.chain_id (for example "mainnet" or "testnet").
current_account_id
The id of the account that owns the current contract.
current_contract_codedeterministic-account-ids
The code of the current contract.
current_global_contract_iddeterministic-account-ids
Returns global contract identifier of the contract’s code currently being executed. Otherwise, returns None if the current contract is not using globally deployed code.
ecrecoverunstable
Recovers an ECDSA signer address from a 32-byte message hash and a corresponding signature along with v recovery byte.
ed25519_verify
Verifies signature of message using provided ED25519 Public Key
epoch_height
Current epoch height.
input
The input to the contract call serialized as bytes. If input is not provided returns None.
is_valid_account_id
Returns true if the given account ID is valid and false otherwise.
keccak256
Hashes the random sequence of bytes using keccak256.
keccak512
Hashes the random sequence of bytes using keccak512.
keccak256_array
Hashes the bytes using the Keccak-256 hash function. This returns a 32 byte hash.
keccak512_array
Hashes the bytes using the Keccak-512 hash function. This returns a 64 byte hash.
logDeprecated
Log the UTF-8 encodable message.
log_str
Logs the string message message. This message is stored on chain.
ml_dsa_verify
Verifies a FIPS 204 ML-DSA-65 (post-quantum) signature of a message using the provided public key.
p256_signature_is_low_s
Checks whether a P-256 ECDSA signature is in low-S form.
p256_verify
Verifies a P-256 (secp256r1) ECDSA signature of a prehashed message using provided public key
panicDeprecated
Terminates the execution of the program with the UTF-8 encoded message. panic_str should be used as the bytes are required to be UTF-8
panic_str
predecessor_account_id
The id of the account that was the previous contract in the chain of cross-contract calls. If this is the first contract, it is equal to signer_account_id.
prepaid_gas
The amount of gas attached to the call that can be used to pay for the gas fees.
promise_and
Creates a new promise which completes when time all promises passed as arguments complete.
promise_batch_action_add_gas_key_with_full_access
Attach promise action that adds a gas key with full access to the NEAR promise index with the provided promise index.
promise_batch_action_add_gas_key_with_function_call
Attach promise action that adds a gas key restricted to function calls with a specific allowance to the NEAR promise index with the provided promise index.
promise_batch_action_add_key_allowance_with_function_call
Attach promise action that adds a key with function call with specifi allowance to the NEAR promise index with the provided promise index.
promise_batch_action_add_key_with_full_access
Attach promise action that adds a full access key to the NEAR promise index with the provided promise index.
promise_batch_action_add_key_with_function_callDeprecated
Examples
promise_batch_action_create_account
Attach a create account promise action to the NEAR promise index with the provided promise index.
promise_batch_action_delete_account
Attach promise action that deletes the account to the NEAR promise index with the provided promise index.
promise_batch_action_delete_key
Attach promise action that deletes the key to the NEAR promise index with the provided promise index.
promise_batch_action_deploy_contract
Attach a deploy contract promise action to the NEAR promise index with the provided promise index.
promise_batch_action_deploy_global_contract
Deploys a global contract using the provided contract code.
promise_batch_action_deploy_global_contract_by_account_id
Deploys a global contract by referencing another account’s deployed code.
promise_batch_action_function_call
Attach a function call promise action to the NEAR promise index with the provided promise index.
promise_batch_action_function_call_weight
Attach a function call with specific gas weight promise action to the NEAR promise index with the provided promise index.
promise_batch_action_stake
Attach a stake promise action to the NEAR promise index with the provided promise index.
promise_batch_action_state_initdeterministic-account-ids
Appends DeterministicStateInit action to the batch of actions for the given promise pointed by promise_index. Uses low-level crate::sys::promise_batch_action_state_init
promise_batch_action_state_init_by_account_iddeterministic-account-ids
Appends DeterministicStateInit action to the batch of actions for the given promise pointed by promise_index. Uses low-level crate::sys::promise_batch_action_state_init_by_account_id
promise_batch_action_transfer
Attach a transfer promise action to the NEAR promise index with the provided promise index.
promise_batch_action_transfer_to_gas_key
Attach a transfer-to-gas-key promise action to the NEAR promise index with the provided promise index.
promise_batch_action_universal_state_inituniversal-accounts
Appends a UniversalStateInit action to the batch of actions for the given promise pointed by promise_index, creating the 0u universal account that the state-init bytes describe and funding it with amount.
promise_batch_action_use_global_contract
Uses an existing global contract by code hash.
promise_batch_action_use_global_contract_by_account_id
Uses an existing global contract by referencing the account that deployed it.
promise_batch_create
Examples
promise_batch_then
Examples
promise_create
Creates a promise that will execute a method on account with given arguments and attaches the given amount and gas.
promise_resultDeprecated
If the current function is invoked by a callback we can access the execution results of the promises that caused the callback.
promise_result_checked
Same as promise_result but bounded: if the result is longer than max_len, then Err(PromiseResultError::TooLong(len)) is returned.
promise_results_count
If the current function is invoked by a callback we can access the execution results of the promises that caused the callback. This function returns the number of complete and incomplete callbacks.
promise_return
Consider the execution result of promise under promise_idx as execution result of this function.
promise_set_refund_todeterministic-account-ids
Set the account id that will receive the refund if the promise panics. Uses low-level crate::sys::promise_set_refund_to
promise_then
Attaches the callback (which is a near_primitives::action::FunctionCallAction) that is executed after promise pointed by promise_idx is complete.
promise_yield_create
Creates a promise that will execute a method on the current account with given arguments. Writes a resumption token (data id) to the specified register. The callback method will execute after promise_yield_resume is called with the data id OR enough blocks have passed. The timeout length is specified as a protocol-level parameter yield_timeout_length_in_blocks = 200.
promise_yield_create_id
Helper function that creates a yield promise and returns both the promise index and the yield ID.
promise_yield_create_with_id
Like promise_yield_create, but the caller supplies a custom 32-byte yield_id and may attach a deposit. The yield is later resumed with the same yield_id via promise_yield_resume_with_yield_id, so both the creator and the resumer can derive the id deterministically (e.g. from a request id) without first reading a runtime-generated data_id out of a register.
promise_yield_resume
Accepts a resumption token data_id created by promise_yield_create on the local account. data is a payload to be passed to the callback method as a promise input. Returns false if no promise yield with the specified data_id is found. Returns true otherwise, guaranteeing that the callback method will be executed with a user-provided payload.
promise_yield_resume_with_yield_id
Resumes a yield previously created with promise_yield_create_with_id, using the same caller-provided yield_id. data is the payload passed to the yield callback as a promise result.
random_seed
Returns the random seed from the current block. This 32 byte hash is based on the VRF value from the block. This value is not modified in any way each time this function is called within the same method/block.
random_seed_array
Returns the random seed from the current block. This 32 byte hash is based on the VRF value from the block. This value is not modified in any way each time this function is called within the same method/block. Example of usage:
read_register
Reads the content of the register_id. If register is not used returns None.
read_register_bounded
Same as read_register but bounded: if data in the register is longer than max_len, then Some(Err(len)) is returned.
refund_to_account_iddeterministic-account-ids
The account id that will receive the refund if the contract panics.
register_len
Returns the size of the register. If register is not used returns None.
ripemd160_array
Hashes the bytes using the RIPEMD-160 hash function. This returns a 20 byte hash.
set_blockchain_interfaceunit-testing and non-WebAssembly
Replaces the current low-level blockchain interface accessible through env::* with another low-level blockchain interface with builtin functions of the NEAR runtime. In most cases you want to use testing_env! macro to set it.
set_state_init_data_entrydeterministic-account-ids
Appends a data entry to an existing DeterministicStateInit action. Uses low-level crate::sys::set_state_init_data_entry
setup_panic_hook
Setups panic hook to expose error info to the blockchain.
sha3_256
Hashes the bytes using the SHA3-256 (FIPS-202) hash function. This returns a 32 byte hash.
sha3_384
Hashes the bytes using the SHA3-384 (FIPS-202) hash function. This returns a 48 byte hash.
sha3_512
Hashes the bytes using the SHA3-512 (FIPS-202) hash function. This returns a 64 byte hash.
sha256
Hashes the random sequence of bytes using sha256.
sha256_array
Hashes the bytes using the SHA-256 hash function. This returns a 32 byte hash.
signer_account_id
The id of the account that either signed the original transaction or issued the initial cross-contract call.
signer_account_pk
The public key of the account that did the signing.
state_exists
Returns true if the contract state exists and false otherwise.
state_read
Load the state of the given object.
state_write
Writes the specified state to storage.
storage_byte_cost
Price per 1 byte of storage from mainnet genesis config. TODO: will be using the host function when it will be available.
storage_get_evicted
Reads the most recent value that was evicted with storage_write or storage_remove command.
storage_has_key
Checks if there is a key-value in the storage.
storage_read
Reads the value stored under the given key.
storage_remove
Removes the value stored under the given key. If key-value existed returns true, otherwise false.
storage_usage
Current total storage usage of this smart contract that this account would be paying for.
storage_write
Writes key-value into storage. If another key-value existed in the storage with the same key it returns true, otherwise false.
universal_state_init_to_account_iduniversal-accounts
Returns the 0u account id of the given state-init bytes: SHA3-256 of exactly those bytes, Crockford-base32 encoded (NEP-655), once the host has checked that they are a canonical state init.
used_gas
The gas that was already burnt during the contract execution (cannot exceed prepaid_gas)
validator_stake
For a given account return its current stake. If the account is not a validator, returns 0.
validator_total_stake
Returns the total stake of validators in the current epoch.
value_return
Sets the blob of data as the return value of the contract.