Special variables and functions in Solidity
List and explanation of the reserved/special variables and functions in Solidity - often to get access to blockchain data
Table of Contents for Special variables and functions in Solidity
Special functions
-
blockhash(uint blockNumber) returns (bytes32)
returns the block hash of the blockNumber. The block number must be one of the 256 most recent blocks, if you pass in anything else it will return0
-
If you need to find the amount of gas left for this transaction (a
uint
) then callgasleft()
Special variables
You can use these variables to get access to information about the blockchain, hash rate etc in your Solidity code.
- Find out the current block number (a
uint
) withblock.number
- Get the chain id by reading
block.chainid
(uint
). For testnets you can see the list of chain ids here - Get the current block difficulty (a uint) with
block.difficulty
- Get the current block gas limit with
block.gaslimit
(auint
) - Get the current miners address with
block.coinbase
(address payable
) - The
address
of the sender of the message (the current call) withmsg.sender
. To get the original sender of the transaction, seetx.origin
- The original sender (
address
) of the transaction withtx.origin
- The amount of wei (
uint
) sent with the message withmsg.value
- The full calldata (returns
bytes calldata
) withmsg.data
- The gas price (in
uint
) of the transaction withtx.gasprice
- The timestamp (seconds since unix epoch - jan 1st 1970) of the current block with
block.timestamp
- You can get the first four bytes (
bytes4
) of the calldata withmsg.sig
- this is useful if you want the function identifier. block.basefee
(uint
) returns the base fee for the specified block number. See EIP-3198 and EIP-1559
Spotted a typo or have a suggestion to make this crypto dev article better? Please let me know!
Next post
Previous post
π Solidity Auditing online quiz
Learn how to audit smart contracts by looking at some example code and trying to find the bugs
β½ Solidity Gas Optimizations Guide
How to optimize and reduce gas usage in your smart contracts in Solidity
π§ͺ Guide to testing with Foundry
Guide to adding testing for your Solidity contracts, using the Foundry and Forge tools
π Guide to UTXO
UTXO and the UTXO set (used by blockchains such as Bitcoin) explained
π Solidity Assembly Guide
Introduction guide to using assembly in your Solidity smart contracts
π¦ Ethereum EOF format explained
Information explaining what the upcoming Ethereum EOF format is all about