0
0
Blockchain / Solidityprogramming~30 mins

Transaction context (tx.origin vs msg.sender) in Blockchain / Solidity - Hands-On Comparison

Choose your learning style9 modes available
Understanding Transaction Context: tx.origin vs msg.sender
📖 Scenario: Imagine you are building a simple smart contract system where one contract calls another. You want to understand who is actually initiating the transaction and who is calling the contract functions.This is important because in blockchain, tx.origin and msg.sender can be different, and knowing the difference helps you write safer contracts.
🎯 Goal: You will create two contracts: Caller and Callee. The Callee contract will have a function that returns both tx.origin and msg.sender. The Caller contract will call the Callee contract's function. You will see the difference in the values returned.
📋 What You'll Learn
Create a contract named Callee with a function getOriginAndSender that returns tx.origin and msg.sender.
Create a contract named Caller with a function callCallee that calls getOriginAndSender from Callee.
Deploy both contracts and call callCallee from an externally owned account (your wallet).
Print the addresses returned by getOriginAndSender to observe the difference.
💡 Why This Matters
🌍 Real World
Understanding transaction context is crucial for writing secure smart contracts that interact with other contracts and users.
💼 Career
Blockchain developers must know how to correctly identify who is calling their contracts to prevent security vulnerabilities.
Progress0 / 4 steps
1
Create the Callee contract with getOriginAndSender function
Create a contract named Callee with a public function getOriginAndSender that returns two addresses: tx.origin and msg.sender.
Blockchain / Solidity
Need a hint?

Remember to declare the function as public view and return two addresses.

2
Create the Caller contract with callCallee function
Create a contract named Caller with a function callCallee that takes an address calleeAddress and calls getOriginAndSender on the Callee contract at that address. Store the returned addresses in variables origin and sender.
Blockchain / Solidity
Need a hint?

Use the Callee contract type to call getOriginAndSender on the given address.

3
Call callCallee from an external account
Write a comment explaining that when you call callCallee from your wallet, tx.origin will be your wallet address and msg.sender will be the Caller contract address.
Blockchain / Solidity
Need a hint?

Explain the difference between tx.origin and msg.sender in a comment.

4
Print the returned addresses to observe the difference
Add a public function printOriginAndSender in the Caller contract that takes an address calleeAddress, calls callCallee, and returns a string showing both tx.origin and msg.sender addresses in a readable format using abi.encodePacked and string conversion.
Blockchain / Solidity
Need a hint?

Use a helper function to convert addresses to strings for readable output.