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 Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay