Know which account called you, how much gas and tokens were attached, and more.
The environment is a set of variables and functions that are available to your smart contract when it is executed. It provides information such as who called the method, how much money was attached to the call, and how many computational resources are available.Every method execution has an environment associated with information such as:
Who called the method
How much money is attached to the call
How many computational resources are available
The current timestamp
Helper functions for Public Key derivation, for example
The current_account contains the address in which your contract is deployed. This is very useful to implement ownership, e.g. making a public method only callable by the contract itself.
The predecessor is the account that called the method in the contract. Meanwhile, the signer is the account that signed the initial transaction.During a simple transaction (no cross-contract calls) the predecessor is the same as the signer. For example, if alice.near calls contract.near, from the contract’s perspective, alice.near is both the signer and the predecessor. However, if contract.near creates a cross-contract call, then the predecessor changes down the line. In the example below, when pool.near executes, it would see contract.near as the predecessor and alice.near as the signer.You can access information about the users interacting with your smart contract
In most scenarios you will only need to know the predecessor. However, there are situations in which the signer is very useful. For example, when adding NFTs into this marketplace, the contract checks that the signer, i.e. the person who generated the transaction chain, is the NFT owner.
signer_account_pk returns the signer’s public key as a near-sdk-rs PublicKey — not a preformatted <scheme>:<base58> string. If the transaction was signed with a post-quantum ml-dsa-65 key, that public key is much larger — 1952 bytes, versus 32 for ed25519 — so keep that in mind if your contract stores, compares, or serializes signer public keys.
attached_deposit represents the amount of yoctoNEAR the predecessor attached to the call.This amount is already deposited in your contract’s account, and is automatically returned to the predecessor if your method panics.
If you make a cross-contract call and it panics, the funds are sent back to your contract. See how to handle this situation in the callback section
account_balance represents the balance of your contract (current_account).It includes the attached_deposit, since it was deposited when the method execution started.If the contract has any locked $NEAR, it will appear in account_locked_balance.
The timestamp attribute represents the approximated UNIX timestamp in nanoseconds at which this call was executed. It quantifies time passing in a human way, enabling us to check if a specific date has passed or not.
The NEAR blockchain groups blocks in Epochs. The current_epoch attribute measures how many epochs have passed so far. It is very useful to coordinate with other contracts that measure time in epochs, such as the validators.
Your contract has a limited number of computational resources to use on each call. Such resources are measured in Gas.Gas can be thought of as wall time, where 1 PetaGas (1_000 TGas) is ~1 second of compute time.Each code instruction costs a certain amount of Gas, and if you run out of it, the execution halts with the error message Exceeded the prepaid gas.The environment gives you access to two gas-related arguments: prepaid_gas and used_gas.
Recovers an ECDSA signer address from a 32-byte message hash and a corresponding signature along with v recovery byte. Takes in an additional flag to check for malleability of the signature which is generally only ideal for transactions. Returns 64 bytes representing the public key if the recovery was successful.
Panic String
env::panic_str(message)
Terminates the execution of the program with the UTF-8 encoded message.
Log String
env::log_str(message)
Logs the string message. This message is stored on chain.
Validator Stake
env::validator_stake(account_id)
For a given account return its current stake. If the account is not a validator, returns 0.
Validator Total Stake
env::validator_total_stake()
Returns the total stake of validators in the current epoch.