Variables & Globals
Solidity has three kinds of variables, and the difference between them matters far more than in most languages, because it decides what costs gas and what survives after a call.
| Kind | Declared | Lives | Cost |
|---|---|---|---|
| State variable | inside the contract, outside functions | permanently, in contract storage | expensive to write |
| Local variable | inside a function | only during the call | cheap |
| Global variable | built in (msg, block, tx, …) |
provided by the EVM | read-only |
State and local variables
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Tally {
uint256 public total; // state variable: stored on chain
uint256 public calls;
function add(uint256 amount) external {
uint256 doubled = amount * 2; // local variable: gone after the call
total += doubled;
calls += 1;
}
}
contract Main {
function main() external {
Tally t = new Tally();
t.add(5);
t.add(10);
console.log("total:", t.total());
console.log("calls:", t.calls());
}
}total: 30
calls: 2
doubled disappears when add returns; total and calls keep their values between transactions.
State variables can have an initial value (uint256 public fee = 25;). Local variables always start at the type's zero value if you don't assign one.
Scope
Variables are visible from their declaration until the end of the enclosing { } block, as in C or JavaScript's let. Declaring a local variable with the same name as a state variable shadows it; the compiler warns about this because it easily causes bugs.
Global variables
The EVM gives every call information about its context. The most important ones:
| Variable | Type | Meaning |
|---|---|---|
msg.sender |
address |
The immediate caller of the current function |
msg.value |
uint256 |
Wei sent with the call (only in payable functions) |
msg.data |
bytes |
The raw call data |
msg.sig |
bytes4 |
The first four bytes of the call data: the function selector |
block.number |
uint256 |
Current block number |
block.timestamp |
uint256 |
Current block time, seconds since 1970-01-01 UTC |
block.chainid |
uint256 |
The chain's id (1 = Ethereum mainnet) |
tx.origin |
address |
The account that signed the transaction |
gasleft() |
uint256 |
Gas remaining in this call |
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
function main() external view {
console.log("msg.sender :", msg.sender);
console.log("tx.origin :", tx.origin);
console.log("block.number :", block.number);
console.log("block.timestamp:", block.timestamp);
console.log("block.chainid :", block.chainid);
console.logBytes4(msg.sig);
}
}msg.sender : 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
tx.origin : 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
block.number : 1
block.timestamp: 1767225600
block.chainid : 1
0xdffeadd0
The playground always uses block 1 with timestamp 1767225600, which is 2026-01-01 00:00:00 UTC, so results are reproducible. On a real chain these values change with every block. msg.sig is the selector of main(): the first four bytes of keccak256("main()").
msg.sender vs tx.origin
When a contract calls another contract, msg.sender changes at each hop, but tx.origin stays the original signer:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Inner {
function whoCalls() external view {
console.log("Inner sees msg.sender:", msg.sender);
console.log("Inner sees tx.origin :", tx.origin);
}
}
contract Middle {
function forward(Inner inner) external view {
console.log("Middle sees msg.sender:", msg.sender);
inner.whoCalls();
}
}
contract Main {
function main() external {
Inner inner = new Inner();
Middle middle = new Middle();
console.log("Main :", address(this));
console.log("Middle :", address(middle));
middle.forward(inner);
}
}Main : 0x5FbDB2315678afecb367f032d93F642f64180aa3
Middle : 0xB7A5bd0345EF1Cc5E66bf61BdeC17D2461fBd968
Middle sees msg.sender: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Inner sees msg.sender: 0xB7A5bd0345EF1Cc5E66bf61BdeC17D2461fBd968
Inner sees tx.origin : 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Warning: Never use
tx.originfor authorization (require(tx.origin == owner)). A malicious contract that the owner interacts with could call your contract and pass the check. Usemsg.sender. The Access Control lesson shows the attack.
Block time is approximate
block.timestamp is set by the block producer and can only be trusted to within a few seconds. It is fine for deadlines measured in hours or days, but don't use it as a source of randomness or for split-second precision.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
uint256 public deadline;
constructor() {
deadline = block.timestamp + 7 days; // one week from deployment
}
function main() external view {
console.log("now :", block.timestamp);
console.log("deadline:", deadline);
console.log("seconds left:", deadline - block.timestamp);
console.log("open?", block.timestamp < deadline);
}
}now : 1767225600
deadline: 1767830400
seconds left: 604800
open? true
7 days is a time unit literal equal to 604800 seconds. Units get their own lesson later.
Test yourself
Where is a state variable stored?
- In memory, cleared after each call
- In the contract's permanent storage
- On the caller's computer
- In the transaction data
Account A calls contract B, which calls contract C. Inside C, what is msg.sender?
- A
- B
- C
- address(0)
Which global gives the current block's time?
now()block.timestamptx.timemsg.time