msg.sender
A Solidity global variable that returns the address of the account or contract that directly called the current function.
msg.sender is a global variable in Solidity that returns the address of the immediate caller of the current function. It is the standard and recommended way to perform access control checks in smart contracts.
How msg.sender Works
msg.sender changes at each step in a call chain, always reflecting who directly made the current call:
1// Call chain: EOA -> ContractA -> ContractB23// Inside ContractA:4// msg.sender == EOA56// Inside ContractB:7// msg.sender == ContractA (not the EOA)
msg.sender for Access Control
msg.sender is the correct way to verify who is calling a function:
1contract Vault {2 address public owner;34 constructor() {5 owner = msg.sender; // Deployer becomes owner6 }78 modifier onlyOwner() {9 require(msg.sender == owner, "Not owner");10 _;11 }1213 function withdraw(uint256 amount) external onlyOwner {14 payable(msg.sender).transfer(amount);15 }16}
Unlike tx.origin, msg.sender cannot be spoofed through intermediary contract calls. If a malicious contract calls withdraw, msg.sender will be the malicious contract's address — not the original EOA — so the onlyOwner check correctly reverts.
msg.sender vs tx.origin
| Property | msg.sender | tx.origin |
|---|---|---|
| Returns | Immediate caller | Original EOA |
| Changes per call | Yes | No |
| Can be a contract | Yes | No |
| Safe for auth | Yes | No |
Common Patterns Using msg.sender
1// Ownership check2require(msg.sender == owner, "Not owner");34// Role-based access5require(hasRole(ADMIN_ROLE, msg.sender), "Not admin");67// Balance tracking8balances[msg.sender] += msg.value;910// Event logging11emit Transfer(msg.sender, recipient, amount);
Best Practice
Always use msg.sender — not tx.origin — for authorization and identity checks in smart contracts. This is one of the most fundamental Solidity security patterns.
Articles Using This Term
Learn more about msg.sender in these articles:
Related Terms
tx.origin
A Solidity global variable that returns the address of the externally owned account (EOA) that originally initiated the transaction.
EOA (Externally Owned Account)
A blockchain account controlled by a private key held by a person or entity, as opposed to a contract account controlled by code.
Access Control
Security mechanisms that restrict which addresses can call specific functions in a smart contract, preventing unauthorized actions.
Solidity
The primary programming language for writing smart contracts on Ethereum and EVM-compatible blockchains.
Need expert guidance on msg.sender?
Our team at Zealynx has deep expertise in blockchain security and DeFi protocols. Whether you need an audit or consultation, we're here to help.
Get a Quote

