msg.sender vs tx.origin in Solidity (quick explanation)
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 callermsg.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!
Previous post
π Solidity Auditing online quiz
Learn how to audit smart contracts by looking at some example code and trying to find the bugs
β½ Solidity Gas Optimizations Guide
How to optimize and reduce gas usage in your smart contracts in Solidity
π§ͺ Guide to testing with Foundry
Guide to adding testing for your Solidity contracts, using the Foundry and Forge tools
π Guide to UTXO
UTXO and the UTXO set (used by blockchains such as Bitcoin) explained
π Solidity Assembly Guide
Introduction guide to using assembly in your Solidity smart contracts
π¦ Ethereum EOF format explained
Information explaining what the upcoming Ethereum EOF format is all about