VM affordances
The Stylus Rust SDK contains several modules for interacting with the Virtual Machine (VM), which can be imported from stylus_sdk
.
Let's see an example:
note
This code has yet to be audited. Please use at your own risk.
use stylus_sdk::{msg};
let callvalue = msg::value();
This page lists the modules that are available, as well as the methods within those modules.
block
Allows you to inspect the current block:
basefee
: gets the basefee of the current blockchainid
: gets the unique chain identifier of the Arbitrum chaincoinbase
: gets the coinbase of the current block, which on Arbitrum chains is the L1 batch poster's addressgas_limit
: gets the gas limit of the current blocknumber
: gets a bounded estimate of the L1 block number at which the sequencer sequenced the transaction. See Block gas limit, numbers and time for more information on how this value is determinedtimestamp
: gets a bounded estimate of the Unix timestamp at which the sequencer sequenced the transaction. See Block gas limit, numbers and time for more information on how this value is determined
use stylus_sdk::{block};
let basefee = block::basefee();
let chainid = block::chainid();
let coinbase = block::coinbase();
let gas_limit = block::gas_limit();
let number = block::number();
let timestamp = block::timestamp();
contract
Allows you to inspect the contract itself:
address
: gets the address of the current programargs
: reads the invocation's calldata. The entrypoint macro uses this under the hoodbalance
: gets the balance of the current programoutput
: writes the contract's return data. The entrypoint macro uses this under the hoodread_return_data
: copies the bytes of the last EVM call or deployment return result. Note: this function does not revert if out of bounds, but rather will copy the overlapping portionreturn_data_len
: returns the length of the last EVM call or deployment return result, or 0 if neither have happened during the program's execution
use stylus_sdk::{contract};
let address = contract::address();
contract::args();
let balance = contract::balance();
contract::output();
contract::read_return_data();
contract::return_data_len();