- Use the
msg,blockandtxglobal variables. - Explain the difference between
msg.senderandtx.origin. - Use Ether and time units and built-in cryptographic functions.
- Recognise deprecated globals (
now,block.difficulty) and unsafe uses.
What are Global Variables?
Global (special) variables and functions always exist in the global namespace. They give information about the current call (msg), the current block (block) and the transaction (tx), and provide utility functions. You do not declare them – you simply use them.
Global Variable Chart
| Sr.No. | Global Variable | Description |
|---|---|---|
| 1 | msg.sender (address) | Address of the account or contract that made the current call. |
| 2 | msg.value (uint) | Amount of wei sent with the call (only in payable functions). |
| 3 | msg.data (bytes calldata) | Complete call data. |
| 4 | msg.sig (bytes4) | First four bytes of the call data – the function selector. |
| 5 | block.timestamp (uint) | Current block’s timestamp in seconds since the Unix epoch. |
| 6 | block.number (uint) | Current block number. |
| 7 | block.coinbase (address payable) | Address of the current block’s proposer (fee recipient). |
| 8 | block.gaslimit (uint) | Current block’s gas limit. |
| 9 | block.basefee (uint) | Current block’s base fee (EIP-1559). |
| 10 | block.chainid (uint) | ID of the current chain (1 = Ethereum Mainnet). |
| 11 | block.prevrandao (uint) | Randomness value from the beacon chain (since the move to Proof-of-Stake). Replaces block.difficulty, which is now a deprecated alias. |
| 12 | blockhash(uint blockNumber) returns (bytes32) | Hash of a given block – works only for the 256 most recent blocks, excluding the current one; otherwise returns zero. |
| 13 | gasleft() returns (uint256) | Remaining gas in the current call. |
| 14 | tx.origin (address) | The externally owned account that started the whole transaction (full call chain). |
| 15 | tx.gasprice (uint) | Gas price of the transaction. |
| 16 | address(this).balance | Ether balance of the current contract, in wei. |
now was removed in Solidity 0.7.0 – use block.timestamp. block.difficulty is deprecated after Ethereum’s move to Proof-of-Stake – use block.prevrandao. Since 0.8.0, msg.sender and tx.origin are plain address; use payable(msg.sender) to send Ether.msg.sender vs tx.origin
tx.origin = Alice
tx.origin = Alice
Never use tx.origin for authorisation. If Alice is tricked into calling a malicious contract, that contract can call your contract and tx.origin will still be Alice. Use msg.sender.
Units and Built-in Functions
| Group | Examples |
|---|---|
| Ether units | 1 wei, 1 gwei (109 wei), 1 ether (1018 wei) |
| Time units | 1 seconds, 1 minutes, 1 hours, 1 days, 1 weeks |
| Hashing / cryptography | keccak256(bytes), sha256(bytes), ripemd160(bytes), ecrecover(hash, v, r, s) |
| ABI encoding | abi.encode(...), abi.encodePacked(...), abi.decode(...), abi.encodeWithSignature(...) |
| Type information | type(uint8).max, type(MyContract).name, type(IERC20).interfaceId |
| Error handling | require, revert, assert (Chapter 21) |
Example : Using Global Variables
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract GlobalsDemo {
address public owner;
uint public deployedAt;
uint public constant LOCK_TIME = 1 days; // time unit
constructor() {
owner = msg.sender; // who deployed the contract
deployedAt = block.timestamp; // when it was deployed
}
function deposit() external payable {
require(msg.value >= 0.01 ether, "Send at least 0.01 ether");
}
function info() external view returns (address caller, uint blockNo, uint chainId, uint balance) {
return (msg.sender, block.number, block.chainid, address(this).balance);
}
function isUnlocked() external view returns (bool) {
return block.timestamp >= deployedAt + LOCK_TIME;
}
function hashOf(string calldata text) external pure returns (bytes32) {
return keccak256(abi.encodePacked(text));
}
}
block.timestamp, block.prevrandao and block hashes are visible to (and partly influenced by) block producers, so they must not be used for lotteries or games where money depends on randomness. Use a verifiable randomness service (e.g. a VRF oracle) or a commit–reveal scheme (see Project 2).Key Takeaways
msgdescribes the current call,blockthe current block,txthe whole transaction.- Use
msg.sender(nottx.origin) for access control; useblock.timestamp(notnow). - Block values are not a safe source of randomness.
The PDF E-Notes are the earlier printable version of this chapter. The notes on this page are the latest, revised version.
Question / Answer (15 questions)
address(this).balance, which returns the balance in wei.payable(msg.sender).MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- Solidity Team. (n.d.). Units and Globally Available Variables. Solidity Documentation. https://docs.soliditylang.org/en/latest/units-and-global-variables.html
- Solidity Team. (n.d.). Security Considerations. Solidity Documentation. https://docs.soliditylang.org/en/latest/security-considerations.html
- Ethereum Improvement Proposals. (2021). EIP-4399: Supplant DIFFICULTY opcode with PREVRANDAO. https://eips.ethereum.org/EIPS/eip-4399
- ethereum.org. (n.d.). Transactions. Ethereum Developer Documentation. https://ethereum.org/en/developers/docs/transactions/
- Chainlink. (n.d.). Chainlink VRF (Verifiable Random Function) documentation. https://docs.chain.link/vrf
- Modi, R. (2018). Solidity Programming Essentials: A beginner’s guide to build smart contracts for Ethereum and blockchain. Packt Publishing. ISBN 978-1-78883-138-3.