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 even higher-level API available through callback_args, callback_args_vec, ext_contract, Promise, and PromiseOrValue.

Functionsยง

abort
Aborts the current contract execution without a custom message. To include a message, use panic_str.
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.
current_account_id
The id of the account that owns the current contract.
ecrecover
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.
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
Terminates the execution of the program with the UTF-8 encoded message.
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_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_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_transfer
Attach a transfer promise action to the NEAR promise index with the provided promise index.
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_result
If the current function is invoked by a callback we can access the execution results of the promises that caused the callback.
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_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_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.
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.
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_interface
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.
setup_panic_hook
Setups panic hook to expose error info to the blockchain.
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.
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.