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.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|
- 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:
|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.
Example#2: In the below example, a contract is created with a function that demonstrates the variable abi.encode.
Example#3: In the below example, a contract is created to demonstrate the special variables block.number and blockhash.
- Solidity - Variables
- Solidity - Types
- Solidity - Functions
- What are Events in Solidity?
- Solidity - Inheritance
- Solidity - Polymorphism
- Solidity - View and Pure Functions
- Solidity - Encapsulation
- Solidity - Abstract Contract
- How to Install Solidity in Windows?
- Solidity - While, Do-While, and For Loop
- Solidity - Break and Continue Statements
- Solidity - Error Handling
- Steps to Execute Solidity Smart Contract using Remix IDE
- Solidity - Arrays
- Solidity - Enums and Structs
- Solidity - Libraries
- Introduction to Solidity
- Solidity - Constructors
- Solidity - Decision Making Statements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.