New to blockchain software development? Read my beginners guide here

msg.sender vs tx.origin in Solidity (quick explanation)

Created on October 2022 β€’ Tags: guidessolidityethereum

A short guide to two simple concepts msg.sender vs tx.origin

In the solidity language there are two main ways to get the address of who is making the current call.

These are msg.sender and tx.origin. There is a slight (but very important) difference between then.

tx.origin is the original external account that created the transaction.

But msg.sender is the account (smart contract or external) that made the call the function.

An example of msg.sender vs tx.origin

Let’s say you have contract A, which has a function (FN_A) which itself calls contract B’s function (FN_B).

You (a person / external account) makes a transaction that invokes FN_A. That FN_A then calls FN_B (on contract B)

You call Contract A's FN_A

FN_A then calls Contract B's FN_B

inside FN_A: msg.sender is your address. tx.origin is also your address

inside FN_B: msg.sender is Contract A’s address. but tx.origin is your address

In summary

  • tx.origin is like the root or highest level caller
  • msg.sender is the immediate caller

Further reading

Spotted a typo or have a suggestion to make this crypto dev article better? Please let me know!

See all posts (70+ more)

See all posts (70+ more)

Was this post helpful? πŸ“§

If you liked this content and want to receive emails about future posts like this, enter your email. I'll never spam you.

Or follow me on @CryptoGuide_Dev on twitter

By using this site, you agree that you have read and understand its Privacy Policy and Terms of Use.
Use any information on this site at your own risk, I take no responsibility for the accuracy of safety of the information on this site.