There exist special variables and functions in solidity which exist in the global namespace and are mainly used to provide information about the blockchain or utility functions. They are of two types:
1) Block and Transaction Properties:
Block
|
Transaction Properties
|
block.coinbase (address payable) |
Current block miner’s address |
block.difficulty (uint) |
Current block difficulty |
msg.value (uint) |
Number of wei sent with the message |
block.number (uint): |
Current block number |
blockhash(uint blockNumber) returns (bytes32) |
Gives hash of the given block and will only work for the 256 most recent block due to the reason of scalability. |
block.timestamp: |
Current block timestamp as seconds since unix epoch |
gasleft() returns (uint256): |
Remaining gas |
msg.sender (address payable) |
Sender of the message (current call) |
msg.sig (bytes4) |
First four bytes of the calldata (i.e. function identifier) |
now (uint) |
Current block timestamp (alias for block.timestamp) |
tx.gasprice (uint) |
Gas price of the transaction |
block.gaslimit (uint) |
Current block gaslimit |
tx.origin (address payable) |
Sender of the transaction (full call chain) |
msg.data (bytes calldata) |
Complete calldata |
Note:
- The values of all members of msg can change for every external function call.
- block.timestamp, now and blockhash as a source of randomness are not secure. Timestamp and the blockhash can be influenced by the miners.
2) ABI encoding and decoding functions:
Function
|
Properties
|
abi.decode(bytes memory encodedData, (…)) returns (…) |
Decodes the given data, while the types are given in parentheses as second argument. |
abi.encode(…) returns (bytes memory) |
Encodes the given arguments |
abi.encodePacked(…) returns (bytes memory) |
Performs packed encoding of the arguments. |
abi.encodeWithSelector(bytes4 selector, …) returns (bytes memory) |
Encodes the given arguments starting from the second and prepends the given four-byte selector |
abi.encodeWithSignature(string memory signature, …) returns (bytes memory) |
Equivalent to abi.encodeWithSelector(bytes4(keccak256(bytes(signature))), …)` |
Example #1: In the below example, a contract is created to demonstrate msg.sender as a secure way to store the roll number.
Solidity
pragma solidity ^0.6.6;
contract GeeksForGeeksRandom
{
mapping (address => uint) rollNo;
function setRollNO(uint _myNumber) public
{
rollNo[msg.sender] = _myNumber;
}
function whatIsMyRollNumber()
public view returns (uint)
{
return rollNo[msg.sender];
}
}
|
Output:

Example#2: In the below example, a contract is created with a function that demonstrates the variable abi.encode.
Solidity
pragma solidity ^0.6.6;
contract GeeksForGeeks
{
function encode(string memory g)
public pure returns(bytes memory)
{
return abi.encode(g);
}
function encodepacked(string memory g)
public pure returns(bytes memory)
{
return abi.encodePacked(g);
}
}
|
Input:
Geeks
Output:

Example#3: In the below example, a contract is created to demonstrate the special variables block.number and blockhash.
Solidity
pragma solidity >=0.5.0 <0.9.0;
contract GeeksForGeeks
{
uint BNumber;
bytes32 BHashPresent;
bytes32 BHashPrevious;
function PresentHash()
public returns(bytes32)
{
BNumber = block.number;
BHashPresent =blockhash(BNumber);
return BHashPresent;
}
function PreviousHash()
public returns(bytes32)
{
BNumber = block.number;
BHashPrevious = blockhash(BNumber - 1);
return BHashPrevious;
}
}
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!