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.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up