Understanding tx.origin and msg.sender in Solidity (2024)

Developers need to understand the difference between tx.origin and msg.sender in Solidity. These two global variables often need clarification with each other despite their fundamental differences. While they may appear similar at first glance, tx.origin and msg.sender represent distinct addresses in the context of a transaction. In this blog post, we will delve deeper into the meanings of each of these variables.

What is tx.origin?

In Solidity, tx.origin identifies the original sender of a transaction. It points to the external account initiating the transaction and remains constant throughout subsequent smart contract interactions (full call chain).

When a transaction is initiated through the MetaMask wallet, the address of the user's MetaMask wallet is stored in tx.origin. This address remains the same, even if the transaction passes through multiple contracts. The consistency of this address is essential for tracing the initial sender of the transaction.

What is msg.sender?

In smart contract development, msg.sender identifies the sender of the current call. This variable is dynamic and can change throughout the transaction process.

When a transaction moves through several smart contracts, the msg.sender value changes to indicate the most recent contract address in the call chain. For example, if Contract A calls Contract B, then the msg.sender value within Contract B will be recognized as Contract A.

Coding it up

To demonstrate how the tx.origin and msg.sender change between smart contract calls, we will create an EntryContract smart contract that references the UnderlyingContract contract.

Let's add a printTxOriginAndMsgSender function that prints each address.

Here we have the Entry smart contract:

contract EntryContract { IUnderlyingContract private underlyingContract; constructor(IUnderlyingContract _underlyingContract) { underlyingContract = _underlyingContract; } function printTxOriginAndMsgSender() public view { console.log("tx.origin", tx.origin); console.log("msg.sender", msg.sender); } function callUnderlyingContract() external { underlyingContract.printTxOriginAndMsgSender(); }}

Now let's define the UnderlyingContract and it's interface:

interface IUnderlyingContract { function printTxOriginAndMsgSender() external ;}contract UnderlyingContract is IUnderlyingContract { function printTxOriginAndMsgSender() external view { console.log("tx.origin", tx.origin); console.log("msg.sender", msg.sender); }}

To execute the test, we must first deploy the UnderlyingContract and use its address when deploying the EntryContract.

When we call the printTxOriginAndMsgSender function of the EntryContract contract, we see that both addresses are the same.

Understanding tx.origin and msg.sender in Solidity (7)

Let's call the callUnderlyingContract function on the EntryContract contract. We can see that tx.origin and msg.sender are different now. tx.origin is the original caller address and msg.sender is the EntryContract smart contract address.

Understanding tx.origin and msg.sender in Solidity (8)

TL;DR

In Solidity, tx.origin and msg.sender are two variables that serve different but crucial purposes. tx.origin always refers to the address that initially initiated the transaction and remains constant throughout the transaction chain. On the other hand, msg.sender represents the sender of the current message or contract interaction and changes with each call. It is crucial to be cautious when using msg.sender, as it may not always represent the initial caller of the transaction.

Links

Top comments (0)

Subscribe

For further actions, you may consider blocking this person and/or reporting abuse

Understanding tx.origin and msg.sender in Solidity (2024)

FAQs

Understanding tx.origin and msg.sender in Solidity? ›

origin and msg. sender is that tx. origin is the address that initiated the transaction while msg. sender is the immediate address that sent the current function call that's being processed.

What is the difference between TX origin and MSG sender in Solidity? ›

tx. origin always refers to the address that initially initiated the transaction and remains constant throughout the transaction chain. On the other hand, msg. sender represents the sender of the current message or contract interaction and changes with each call.

What does require msg sender == tx origin do? ›

origin is the address of the EOA from which originates the transaction. While msg. sender is the address of the account from which the current call is coming from which could be an EOA or a Contract Account (CA).

What is a MSG sender in Solidity? ›

So, “msg. sender” is a dynamic global variable in Solidity that you can access anywhere in the code without importing anything. This variable is filled automatically when any transaction happens. It is called a dynamic variable because it can change throughout the transaction process.

What is the difference between MSG sender and owner? ›

sender is the address of the person. And if another contract (B) is interacting with the contract, the contract (B)'s address becomes msg. sender . The owner of a contract is the address that deployed the contract to the blockchain, that is, the first msg.

What does MSG value mean in Solidity? ›

msg. value is a member of the msg (message) object when sending (state transitioning) transactions on the Ethereum network. msg. value contains the amount of wei (ether / 1e18) sent in the transaction.

Can a contract be TX Origin? ›

It's also worth mentioning that by using tx. origin you're limiting interoperability between contracts because the contract that uses tx. origin cannot be used by another contract as a contract can't be the tx. origin .

What is the difference between address this and MSG sender? ›

msg. sender is the address of the contract caller. address(this) is the address of the smart contract itself. They are both addresses in Solidity, but there is a big difference between msg.

How do you send ether to a contract that does not have payable functions or a receive or fallback? ›

Answer: To send Ether to a contract that does not have payable functions, or a receive or fallback you would provide its address as a parameter to a selfdestruct call from another contract that has Ether.

How do you check if MSG sender is a contract? ›

Three ways to detect if an address is a smart contract
  1. Check if msg. sender == tx. origin. ...
  2. The second (and recommended way) is to measure the bytecode size of the address using code. length. ...
  3. The third is using codehash and is not recommended because it has the same limitations as code. length with additional complexity.
Apr 5, 2024

Can MSG value be negative in Solidity? ›

1 Answer. It is impossible to send a negative value in EVM. So msg. value is always non-negative.

What does message sender mean? ›

The msg. sender is the address that has called or initiated a function or created a transaction. Now, this address could be of a contract or even a person like you and me.

What is the difference between message sender and message receiver? ›

Communication is sending and receiving information between two or more people. The person sending the message is referred to as the sender, while the person receiving the information is called the receiver.

What is the difference between sender and from? ›

The sender domain is what the receiving email server sees when initiating the session. The from address is what your recipients will see.

What is the difference between a message and a transaction in blockchain? ›

To initiate a blockchain transaction, you need to give a digital signature, which is a cryptographic representation of your intention to carry out the transaction. On the other hand, a wallet confirmation message is used to verify the ownership of a wallet.

What is the origin of Solidity? ›

Solidity was proposed in August 2014 by Gavin Wood The language was later developed by the Ethereum project's Solidity team, led by Christian Reitwiessner.

What is the MSG sender global variable? ›

msg. sender is a global variable that denotes the address of the caller of the current function.

Top Articles
What Happens If I Break My Break My FD Before Its Maturity? | Bajaj Finance
If 5-year post office FD is prematurely withdrawn you will get only savings account interest
Frederick County Craigslist
Nco Leadership Center Of Excellence
Cash4Life Maryland Winning Numbers
Undergraduate Programs | Webster Vienna
Doublelist Paducah Ky
Nikki Catsouras Head Cut In Half
Smokeland West Warwick
Aquatic Pets And Reptiles Photos
Housework 2 Jab
Drago Funeral Home & Cremation Services Obituaries
Craigslist Apartments In Philly
Google Feud Unblocked 6969
Busby, FM - Demu 1-3 - The Demu Trilogy - PDF Free Download
Simpsons Tapped Out Road To Riches
Who called you from +19192464227 (9192464227): 5 reviews
Accident On May River Road Today
Ibukunore
Officialmilarosee
Nhl Tankathon Mock Draft
/Www.usps.com/International/Passports.htm
Wgu Academy Phone Number
Robert Deshawn Swonger Net Worth
Why do rebates take so long to process?
Devotion Showtimes Near Regency Buenaventura 6
Kirk Franklin Mother Debra Jones Age
Smartfind Express Login Broward
Ocala Craigslist Com
By.association.only - Watsonville - Book Online - Prices, Reviews, Photos
Phone number detective
Wow Quest Encroaching Heat
Exploring The Whimsical World Of JellybeansBrains Only
Chs.mywork
That1Iggirl Mega
Temu Y2K
Rhode Island High School Sports News & Headlines| Providence Journal
Discover Things To Do In Lubbock
Collision Masters Fairbanks
Dayton Overdrive
Muni Metro Schedule
Evil Dead Rise - Everything You Need To Know
Zadruga Elita 7 Live - Zadruga Elita 8 Uživo HD Emitirani Sat Putem Interneta
Free Carnival-themed Google Slides & PowerPoint templates
Vcuapi
Comenity/Banter
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 6258

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.