Environment Variables and Constants¶
Environment Variables¶
Environment variables always exist in the namespace and are primarily used to provide information about the blockchain or current transaction.
Block and Transaction Properties¶
Name |
Type |
Value |
---|---|---|
|
|
Current block miner’s address |
|
|
Current block difficulty |
|
|
Current randomness beacon provided by the beacon chain |
|
|
Current block number |
|
|
Current block’s gas limit |
|
|
Current block’s base fee |
|
|
Current block’s blob gas base fee |
|
|
Equivalent to |
|
|
Current block epoch timestamp |
|
|
Chain ID |
|
|
Message data |
|
|
Remaining gas |
|
|
Sender of the message (current call) |
|
|
Number of wei sent with the message |
|
|
Sender of the transaction (full call chain) |
|
|
Gas price of current transaction in wei |
Note
block.prevrandao
is an alias for the block.difficulty
opcode. Since block.difficulty
is considered deprecated according to EIP-4399 after “The Merge” (Paris hard fork), we recommend using block.prevrandao
.
The self Variable¶
self
is an environment variable used to reference a contract from within itself. Along with the normal address members, self
allows you to read and write to state variables and to call private functions within the contract.
Name |
Type |
Value |
---|---|---|
|
|
Current contract’s address |
|
|
Current contract’s balance |
Accessing State Variables¶
self
is used to access a contract’s state variables, as shown in the following example:
state_var: uint256
@external
def set_var(value: uint256) -> bool:
self.state_var = value
return True
@external
@view
def get_var() -> uint256:
return self.state_var
Calling Internal Functions¶
self
is also used to call internal functions within a contract:
@internal
def _times_two(amount: uint256) -> uint256:
return amount * 2
@external
def calculate(amount: uint256) -> uint256:
return self._times_two(amount)
Custom Constants¶
Custom constants can be defined at a global level in Vyper. To define a constant, make use of the constant
keyword.
TOTAL_SUPPLY: constant(uint256) = 10000000
total_supply: public(uint256)
@deploy
def __init__():
self.total_supply = TOTAL_SUPPLY