Skip to content
Back to Curriculum
INTERNALS

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

Example.sol
Loading...

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.

Practical.sol
Loading...

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.

TIMESTAMPNUMBERCALLERCALLVALUECHAINID

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

Getting the caller address (`msg.sender`).
Getting the time (`block.timestamp`) for deadlines.
Getting value sent (`msg.value`) for payments.

Interactive Visualization

See exactly how the EVM handles this code in memory, storage, and the stack.

Interactive Simulation
0 / 0
Loading...
Type Solidity code (v0.8.26) and click Run. Compiler loads on first use (~2MB).

Click Run to execute the simulation

Then use the step controls to navigate