Important Starknet Methods
The table below contains important methods used while building starknet smart contracts. It contains the name of the method, a keyword to import such a method, and finally a simple single line usage of each method. Also note that multiple method imports can be chained to make the codebase simpler and also avoid repetition, e.g., `use starknet::{get_contract_address, ContractAddress}.
Table 1.0
METHODS | IMPORTATION | EXAMPLE USAGE | DESCRIPTION |
get_contract_address() | use starknet::get_contract_address | let ThisContract = get_contract_address(); | Returns the contract address of the contract containing this method. |
get_caller_address() | use starknet::get_caller_address | let user = get_caller_address(); | Returns the contract address of the user calling a certain function. |
ContractAddress | use starknet::ContractAddress | let user: ContractAddress = get_caller_address(); | Allows for the usage of the contract address data type in a contract. |
zero() | use starknet::ContractAddress | let addressZero: ContractAddress = zero(); | Returns address zero contract address |
get_block_info() | use starknet::get_block_info | let blockInfo = get_block_info(); | It returns a struct containing the block number, block timestamp, and the sequencer address. |
get_tx_info() | use starknet::get_tx_info | let txInfo = get_tx_info(); | Returns a struct containing transaction version, max fee, transaction hash, signature, chain ID, nonce, and transaction origin address. |
get_block_timestamp() | use starknet::get_block_timestamp | let timeStamp = get_block_timestamp(); | Returns the block timestamp of the block containing the transaction. |
get_block_number() | use starknet::get_block_number | Let blockNumber = get_block_number(); | Returns the block number of the block containing the transaction. |
ClassHash | use starknet::ClassHash | let childHash: ClassHash = contractClassHash; | Allows for the use of the class Hash datatype to define variables that hold a class hash. |