Solidity Special Variables: Block and Transaction Information

This article will explain some of the most commonly used Solidity special variables with examples, in addition to their functions in the development of smart contracts.

The first category of Solidity special variables are those related to block and transaction information.

A special variable refers to a globally available variable that contains information about blockchains.

These variables provide important data about the current state of the Ethereum blockchain, including the current block number, the timestamp of the current block, and the address of the sender of a transaction.

Special variables are listed below:

Special VariablesOverview
blockhash(uint blockNumber) returns (bytes32)Block hash – only works for 256 most recent blocks, excluding current blocks.
block.coinbase (address payable)Shows the address of the recent miners.
block.difficulty (uint)Displays the difficulty of the block.
block.gaslimit (uint)Shows the gaslimit used by the respective block.
block.number (uint)The recent number of the block is printed.
block.timestampTime since the epoch of the unix block as measured in seconds.
gasleft() returns (uint256)Outputs the remaining gas of the block/contract. (bytes calldata)Makes an instance of data to the msg.
msg.sender (address payable)Shows the sender of the block.
msg.sig (bytes4)Displays the first 4 bytes of the msg.
msg.value (uint)Calculates the number of wei sent through the message.
now (uint)Displays the current time of the block
tx.gasprice (uint)Displays the price of the gas utilized in the block.
tx.origin (address payable)Shows the senders of the whole blockchain.

The following example shows the use of special variables:


// SPDX-License-Identifier: 3.0 pragma solidity ^0.8.0;contract SolidityTest{// It displays the blockhash of the number //function get_Block_Hash(uint mrx_Blocknumber) public view returns (bytes32) { return blockhash(mrx_Blocknumber); }// It displays the coin base of the blockfunction get_Block_coinbase() public view returns(address){address mrx_Blocknumber=block.coinbase;return mrx_Blocknumber; }// In order to obtain the timestamp of the block you can create the function as followfunction get_Block_timestamp() public view returns(uint){return block.timestamp;}// if you want to find the block difficulty you can make a function for it as follow:function get_Block_difficulty() public view returns(uint){return block.difficulty;}// In latest version of solidity the difficulty keyword was replaced by prevrandao:function get_Block_modified_Difficulty_Keyword() public view returns(uint){return block.prevrandao;}// To find the gas limit of the block have a look at the function below:function get_Block_gaslimit() public view returns(uint){return block.gaslimit;}// THe block number could also be determined by uisng the function below:function get_Block_Number() public view returns(uint){return block.number;}// To check the gas left in the block we have created the method below:function show_remaining_Gas() public view returns(uint256){uint256 gas_remain=gasleft();return gas_remain; }// To check the message data we can make a function below:function msg_data_Result() public pure returns(bytes memory){return;} // To check sender's address have a look at function below:function show_msg_address() public view returns(address){return msg.sender;}// To see first 4 bytes of the call datafunction show_4_bytes() public pure returns(bytes4){return msg.sig;}// To see the value of the message in ether we have created a function accordingly: function get_value() public payable {uint256 mrx;mrx += msg.value; }// To calculate the price of the gas we can consider the function below:function get_Gas_Value() public view returns(uint){return tx.gasprice;}// Sender of the transaction is shownfunction get_Sender_Origion() public view returns(address){return tx.origin;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
Note: if you find it difficult to execute the code above, have a look at our Solidity First Application article.


Solidity special variables are an essential feature that enables you to access and use crucial information during smart contract execution.

From providing details about the current block number to accessing the original transaction sender’s address, these special variables offer a range of functionalities that make smart contract development on Ethereum more efficient and secure.

We value your feedback.

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!

Leave a Reply

Your email address will not be published. Required fields are marked *