New to blockchain software development? Read my beginners guide here

Special variables and functions in Solidity

Created on August 2022 β€’ Tags: solidityethereumguides

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 return 0

  • If you need to find the amount of gas left for this transaction (a uint) then call gasleft()

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) with block.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 (a uint)
  • Get the current miners address with block.coinbase (address payable)
  • The address of the sender of the message (the current call) with msg.sender. To get the original sender of the transaction, see tx.origin
  • The original sender (address) of the transaction with tx.origin
  • The amount of wei (uint) sent with the message with msg.value
  • The full calldata (returns bytes calldata) with msg.data
  • The gas price (in uint) of the transaction with tx.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 with msg.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!

See all posts (70+ more)

See all posts (70+ more)

Was this post helpful? πŸ“§

If you liked this content and want to receive emails about future posts like this, enter your email. I'll never spam you.

Or follow me on @CryptoGuide_Dev on twitter

By using this site, you agree that you have read and understand its Privacy Policy and Terms of Use.
Use any information on this site at your own risk, I take no responsibility for the accuracy of safety of the information on this site.