Global Variables
Solidity provides special variables that exist in the global namespace. These give you information about the blockchain (Block Context) and the current transaction (Transaction Context).
Syntax Structure
Key Components
- Boilerplate structure required by the Solc compiler.
- Placement of state variables versus functions.
Practical Implementation
Using block.timestamp for a time-locked wallet.
Core Concepts
block.timestamp
Current block timestamp in seconds since Unix epoch. Manipulatable by miners by ~15s.
tx.origin
The original sender of the transaction. Dangerous for authorization (phishing risk).
msg.sender
The immediate caller (can be a person or another contract).
Mental Model
The Environment Stats
Imagine your code running inside a transparent box. `Global Variables` are the displays on the wall of the box telling you: 'What time is it?', 'Who is outside the box poking me?', 'How heavy is the bag of money they threw in?'.
Under the Hood
These map directly to context opcodes. `block.timestamp` is `TIMESTAMP`. `msg.sender` is `CALLER`. Accessing these is generally very cheap.
Gas Analysis
Cheap to read.
- • Don't read `block.timestamp` repeatedly in a loop; store it in a stack variable.
Security Risks
Don't trust miners completely.
- ! Timestamp Dependence: Don't use `block.timestamp` for random number generation (miners can bias it).
- ! tx.origin Phishing: Never use `tx.origin` for auth. An attacker can trick you into calling their contract, which then calls yours.
Common Use Cases
Interactive Visualization
See exactly how the EVM handles this code in memory, storage, and the stack.
Click Run to execute the simulation
Then use the step controls to navigate