DEV Community

Mahima Thacker
Mahima Thacker

Posted on

tx.origin vs msg.sender: Which to Use?

When writing Solidity smart contracts, you often come across tx.origin and msg.sender so what’s the difference Between tx.origin and msg.sender?

msg.sender: This is the last caller. It tells you who directly called the contract.

tx.origin: This is the first sender. It tells you who started the transaction, always an external wallet (like MetaMask).

Image description

Example:

If a wallet sends a transaction to Contract A, and Contract A calls Contract B:

In Contract B:

msg.sender is Contract A (the most recent caller).

tx.origin is the wallet(EOA) (the original transaction starter).

Using tx.origin for things like security checks can be dangerous. It can make your contract vulnerable to attacks. For better security, rely on msg.sender.

A small detail, but it can make a big difference in your contract's security.

Top comments (0)