Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 47 ยท Language Basics

msg.sender, msg.value and Global Variables

Learn Solidity's built-in globals: msg.sender, msg.value, msg.data, block.timestamp, block.number, chain ID, and why tx.origin is dangerous.

Every time a function runs, Solidity makes information about the current call available through a few built-in objects: msg (this call), block (the block it is in) and tx (the whole transaction). They answer questions a contract constantly asks: who is calling me, how much ether came with the call, what time is it, and which chain am I on? You never declare them, they are simply there.

Almost all access control, payment logic and time-based rules in later lessons rest on these few values, so it pays to know exactly what each one means, and which ones you should not trust.

Three objects you get for free

msg The current call: sender, value, data.
block The block: time, number, chain ID.
tx The whole transaction: origin, gas price.

All of them are read-only.

1 / 2

msg.sender

msg.sender is the address that called the current function. It is guaranteed by the signature check: for a transaction from a wallet it is that wallet's address, and for a call from another contract it is that contract's address. It is the basis of ownership checks:

SOLIDITY
address public owner;

constructor() {
    owner = msg.sender;          // whoever deploys the contract
}

function setPrice(uint256 p) external {
    require(msg.sender == owner, "not the owner");
    price = p;
}

The key point is that msg.sender is the immediate caller, and it changes as calls nest. If you send a transaction to contract A, and A calls contract B, then inside A msg.sender is you, but inside B msg.sender is the address of A. Contract B never sees you, only A. This is how a token contract knows that a decentralized exchange is moving your tokens (it sees the exchange as the caller, and checks that you approved it).

msg.value

msg.value is the amount of wei sent along with the call. It is only available in payable functions (and in internal functions called from them); using it in a non-payable public function is a compile error.

SOLIDITY
function buy() external payable {
    require(msg.value >= price, "not enough ether");
    // msg.value is in wei: 1 ether == 1e18
}

Remember that the value has already been added to the contract's balance by the time your code runs. If you revert, it goes back to the sender. See Sending and Receiving Ether for receive and fallback.

msg.data and msg.sig

msg.data is the complete input (the calldata) of the call as raw bytes, and msg.sig is its first four bytes, the function selector that says which function was requested. You rarely need either in everyday code, but they are important in fallback functions and proxies, covered in ABI Encoding and Low-Level Calls.

block.timestamp and block.number

  • block.timestamp is the time of the current block, in seconds since January 1, 1970 (the Unix epoch).
  • block.number is the height of the current block.

These are handy for deadlines (require(block.timestamp < deadline)), vesting schedules and cooldowns. Keep three rules in mind:

  1. The timestamp is set by the block proposer within tight rules, so it can be off by a few seconds. Use it for hours and days, not for decisions that depend on single seconds.
  2. Inside one transaction the timestamp never changes, even if your code runs for a long time.
  3. Never derive randomness from timestamp, block number or similar values. Validators and attackers can influence or predict them. Oracles, Randomness and Off-Chain Data shows safer designs.

Other block values you may meet: block.chainid, block.basefee, block.coinbase (the block proposer's address) and block.prevrandao, which replaced block.difficulty after the move to proof of stake. Treat prevrandao as unsuitable for anything that needs strong randomness.

Chain ID

block.chainid identifies the network: 1 is Ethereum mainnet, and test networks such as Sepolia have their own numbers (11155111 for Sepolia). Why does a contract care? Signed messages. If a user signs a message "pay Alice 5" on a testnet, the same signature must not be valid on mainnet, so well-designed signatures include the chain ID. This is called replay protection, and the standard approach (EIP-712) builds it in.

tx.origin and why to avoid it

tx.origin is the wallet that started the whole transaction, no matter how many contracts are in between. It looks like a stronger msg.sender, and that is the trap. If your contract checks tx.origin == owner, a malicious contract can trick the owner into calling it (a fake airdrop, for instance). That malicious contract then calls your contract, and tx.origin is still the owner, so the check passes while the attacker is in control. Using msg.sender instead fails the check, because the sender is the attacker's contract.

SOLIDITY
// DANGEROUS: a phishing contract in the middle still passes this check
require(tx.origin == owner);

// SAFE: checks the direct caller
require(msg.sender == owner);

A rare legitimate use is "this call must come directly from a wallet, not a contract" (msg.sender == tx.origin), but even that is fragile, and you should not rely on it for security. Treat tx.origin in an authorization check as a bug, and see Common Vulnerabilities for more.

Two more everyday globals: address(this) is the current contract's own address, and gasleft() returns the gas remaining.

Stand-ins for the call context

The TryIts are JavaScript stand-ins: a tiny call stack where each frame has its own msg.sender and msg.value, and a shared tx.origin. They are not the EVM.

JS msg.sender changes as calls nest

Press Run (or Ctrl + Enter) to see the console output here.

No output.

When Alice calls the router, the vault sees the router as msg.sender, while tx.origin is still Alice. Now the phishing scenario: the same ownership check written with each global.

JS tx.origin phishing versus msg.sender

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The wallet that checks tx.origin is drained. The one that checks msg.sender refuses, because the direct caller is the attacker's contract.

What is real: the meaning and trust levels of each global, and how msg.sender shifts in nested calls while tx.origin stays fixed. What is the stand-in: a plain JavaScript stack instead of the EVM's call frames, no signatures, no gas and no real block data.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You send a transaction to contract A, and A calls contract B. What is msg.sender inside B?

    msg.sender is always the immediate caller. B sees A. Your wallet is visible to B only as tx.origin.
  2. 2. Why is require(tx.origin == owner) unsafe?

    tx.origin stays the original wallet through all nested calls, so a phishing contract that tricks the owner can call your contract and still pass the check.
  3. 3. Where can msg.value be used?

    A non-payable public function cannot receive ether, so the compiler rejects msg.value there. A payable function can read it.
  4. 4. How should you treat block.timestamp?

    The proposer has a little room to shift the timestamp, so it is fine for hours and days but not for fine-grained or random decisions.

Summary

  • msg.sender is the immediate caller and the basis for access control; it changes in nested calls.
  • msg.value is the wei sent with the call and is available in payable contexts.
  • msg.data and msg.sig expose raw calldata and the 4-byte selector, useful in fallbacks and proxies.
  • block.timestamp and block.number suit deadlines, not randomness or second-level precision.
  • block.chainid identifies the network and helps prevent signature replay across chains.
  • Never authorize with tx.origin; use msg.sender.

Next: Constructors, Constants and Immutables.