- Solidity - Discussion
- Solidity - Useful Resources
- Solidity - Quick Guide
- Solidity - Error Handling
- Solidity - Events
- Solidity - Assembly
- Solidity - Libraries
- Solidity - Interfaces
- Solidity - Abstract Contracts
- Solidity - Constructors
- Solidity - Inheritance
- Solidity - Contracts
- Solidity - Restricted Access
- Solidity - Withdrawal Pattern
- Cryptographic Functions
- Mathematical Functions
- Function Overloading
- Solidity - Fallback Function
- Solidity - Pure Functions
- Solidity - View Functions
- Solidity - Function Modifiers
- Solidity - Functions
- Solidity - Style Guide
- Solidity - Special Variables
- Solidity - Ether Units
- Solidity - Conversions
- Solidity - Mappings
- Solidity - Structs
- Solidity - Enums
- Solidity - Arrays
- Solidity - Strings
- Solidity - Decision Making
- Solidity - Loops
- Solidity - Operators
- Solidity - Variable Scope
- Solidity - Variables
- Solidity - Types
- Solidity - Comments
- Solidity - First Application
- Solidity - Basic Syntax
- Solidity - Environment Setup
- Solidity - Overview
- Solidity - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Sopdity - Special Variables
Special variables are globally available variables and provides information about the blockchain. Following is the pst of special variables −
Sr.No. | Special Variable & Description |
---|---|
1 | blockhash(uint blockNumber) returns (bytes32) Hash of the given block - only works for 256 most recent, excluding current, blocks. |
2 | block.coinbase (address payable) Current block miner s address. |
3 | block.difficulty (uint) current block difficulty. |
4 | block.gaspmit (uint) Current block gaspmit. |
5 | block.number (uint) Current block number. |
6 | block.timestamp Current block timestamp as seconds since unix epoch. |
7 | gasleft() returns (uint256) Remaining gas. |
8 | msg.data (bytes calldata) Complete calldata. |
9 | msg.sender (address payable) Sender of the message (current call). |
10 | msg.sig (bytes4) First four bytes of the calldata (i.e. function identifier) |
11 | msg.value (uint) Number of wei sent with the message. |
12 | now (uint) Current block timestamp (apas for block.timestamp). |
13 | tx.gasprice (uint) Gas price of the transaction. |
14 | tx.origin (address payable) Sender of the transaction (full call chain). |
Example
Try the following code to see the use of msg, a special variable to get the sender address in Sopdity.
pragma sopdity ^0.5.0; contract LedgerBalance { mapping(address => uint) pubpc balances; function updateBalance(uint newBalance) pubpc { balances[msg.sender] = newBalance; } } contract Updater { function updateBalance() pubpc returns (uint) { LedgerBalance ledgerBalance = new LedgerBalance(); ledgerBalance.updateBalance(10); return ledgerBalance.balances(address(this)); } }
Run the above program using steps provided in
chapter.First Cpck updateBalance Button to set the value as 10 then look into the logs which will show the decoded output as −
Output
{ "0": "uint256: 10" }Advertisements