Environment
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
Environment Variables
- 🌐 JavaScript
- 🦀 Rust
Variable Name | SDK Variable | Description |
---|---|---|
Predecessor | near.predecessorAccountId() | Account ID that called this method |
Current Account | near.currentAccountId() | Account ID of this smart contract |
Signer | near.signerAccountId() | Account ID that signed the transaction leading to this execution |
Attached Deposit | near.attachedDeposit() | Amount in NEAR attached to the call by the predecessor |
Account Balance | near.accountBalance() | Balance of this smart contract (including Attached Deposit) |
Prepaid Gas | near.prepaidGas() | Amount of gas available for execution |
Timestamp | near.blockTimestamp() | Current timestamp (number of non-leap-nanoseconds since January 1, 1970 0:00:00 UTC) |
Current Epoch | near.epochHeight() | Current epoch in the blockchain |
Block Index | near.blockIndex() | Current block index (a.k.a. block height) |
Storage Used | near.storageUsage() | Current storage used by this smart contract |
Used Gas | near.usedGas() | Amount of gas used for execution |
Signer Public Key | near.signerAccountPk() | Sender Public Key |
Account Locked Balance | near.accountLockedBalance() | Balance of this smart contract that is locked |
Variable Name | SDK Variable | Description |
---|---|---|
Predecessor | env::predecessor_account_id() | Account ID that called this method |
Current Account | env::current_account_id() | Account ID of this smart contract |
Signer | env::signer_account_id() | Account ID that signed the transaction leading to this execution |
Attached Deposit | env::attached_deposit() | Amount in NEAR attached to the call by the predecessor |
Account Balance | env::account_balance() | Balance of this smart contract (including Attached Deposit) |
Prepaid Gas | env::prepaid_gas() | Amount of gas available for execution |
Timestamp | env::block_timestamp() | Current timestamp (number of non-leap-nanoseconds since January 1, 1970 0:00:00 UTC) |
Current Epoch | env::epoch_height() | Current epoch in the blockchain |
Block Index | env::block_index() | Current block index (a.k.a. block height) |
Storage Used | env::storage_usage() | Current storage used by this smart contract in bytes |
Storage Byte Cost | env::storage_byte_cost() | Current storage cost per byte in yoctoNEAR |
Used Gas | env::used_gas() | Amount of gas used for execution |
Signer Public Key | env::signer_account_pk() | Sender Public Key |
Account Locked Balance | env::account_locked_balance() | Balance of this smart contract that is locked |
Who is Calling? Who am I?
The environment gives you access to 3 important users: the current_account
, the predecessor
, and the signer
.
Current Account
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.
Predecessor and Signer
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.
Balances and Attached NEAR
The environment gives you access to 3 token-related parameters, all expressed in yoctoNEAR (1 Ⓝ = 1024yⓃ):
Attached Deposit
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
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
.
Storage Used
storage_used
represents the amount of storage that is currently being used by your contract.
If you want to know how much storage a structure uses, print the storage before and after storing it.
Telling the Time
The environment exposes three different ways to tell the pass of time, each representing a different dimension of the underlying blockchain.
Timestamp
The timestamp
attribute represents the approximated UNIX timestamp at which this call was executed. It quantifies time passing in a human way, enabling to check if a specific date has passed or not.
Current Epoch
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.