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

Mahima Thacker @mahima_thacker

About: 7+ Web3 Hackathon Wins | Full Stack Developer | Web3 Developer | Tech Educator | SheFi Graduate | Member Developer_DAO, H.E.R DAO

Location:
Bhuj, Gujarat, India
Joined:
Dec 23, 2024

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

Publish Date: Jan 26
0 0

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.

Comments 0 total

    Add comment