Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

msg.value and msg.sender in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - msg.value and msg.sender
Transaction sent
↓
msg.sender set to sender address
↓
msg.value set to sent ether amount
↓
Function executes using msg.sender and msg.value
↓
Contract updates state or transfers ether
↓
Transaction ends
When a transaction calls a contract, msg.sender holds the caller's address and msg.value holds the amount of ether sent. The contract uses these to process logic.
Execution Sample
Blockchain / Solidity
function buy() public payable {
    require(msg.value == 1 ether, "Send 1 ether");
    buyers[msg.sender] = true;
}
This function requires exactly 1 ether sent and records the sender as a buyer.
Execution Table
Stepmsg.sendermsg.value (ether)ConditionActionState Change
10xABC1231msg.value == 1 etherCheck conditionPass
20xABC1231Condition passedSet buyers[0xABC123] = truebuyers[0xABC123] = true
30xDEF4560.5msg.value == 1 etherCheck conditionFail
40xDEF4560.5Condition failedRevert transactionNo state change
💡 Execution stops when condition fails or function completes successfully.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
msg.senderN/A0xABC1230xABC1230xDEF4560xDEF456N/A
msg.valueN/A1 ether1 ether0.5 ether0.5 etherN/A
buyers[0xABC123]falsefalsetruetruetruetrue
Key Moments - 2 Insights
Why does msg.sender always show the caller's address?
Because msg.sender is automatically set by the blockchain to the address that initiated the transaction or call, as shown in steps 1 and 3 of the execution_table.
What happens if msg.value is less than required?
The require condition fails and the transaction reverts, so no state changes happen. This is shown in step 4 where the condition fails and buyers mapping is not updated.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is msg.sender at step 3?
A0xABC123
B0xDEF456
C0x000000
DNot set
💡 Hint
Check the msg.sender column at step 3 in the execution_table.
At which step does the transaction revert due to msg.value?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look for the step where the condition fails and action is 'Revert transaction' in the execution_table.
If msg.value was exactly 1 ether at step 3, what would happen?
Abuyers[0xDEF456] would be set to true
BTransaction would revert
CNothing changes
Dmsg.sender changes
💡 Hint
Refer to the pattern in steps 1 and 2 where condition passes and buyers mapping updates.
Concept Snapshot
msg.sender is the address calling the contract.
msg.value is the amount of ether sent with the call.
Use msg.value to check payment amount.
Use msg.sender to identify who called.
If msg.value is wrong, revert transaction.
These are set automatically by the blockchain.
Full Transcript
When a blockchain transaction calls a contract, the system sets msg.sender to the caller's address and msg.value to the ether sent. The contract can check msg.value to ensure correct payment and use msg.sender to track who called. If the payment is incorrect, the contract can stop execution and revert changes. This example shows a buy function that requires exactly 1 ether and records the buyer's address. The execution table traces two calls: one with correct payment that updates state, and one with insufficient payment that reverts. Variables msg.sender and msg.value change per call, and the buyers mapping updates only on success. Understanding these helps write secure contracts that handle payments and identify callers correctly.

Practice

(1/5)
1. What does msg.sender represent in a smart contract?
easy
A. The amount of ether sent with the transaction
B. The address of the account that called the contract
C. The balance of the contract
D. The gas price of the transaction

Solution

  1. Step 1: Understand the role of msg.sender

    msg.sender always holds the address of the account or contract that called the current function.
  2. Step 2: Differentiate from msg.value

    msg.value holds the amount of ether sent, not the caller's address.
  3. Final Answer:

    The address of the account that called the contract -> Option B
  4. Quick Check:

    msg.sender = caller address [OK]
Hint: Remember: sender is who calls, value is what is sent [OK]
Common Mistakes:
  • Confusing msg.sender with msg.value
  • Thinking msg.sender is contract balance
  • Assuming msg.sender is always a user (it can be a contract)
2. Which of the following is the correct way to check if the caller sent at least 1 ether in Solidity?
easy
A. if (msg.value >= 1 ether) {}
B. if (msg.value == msg.sender) {}
C. if (msg.sender >= 1 ether) {}
D. if (msg.sender == 1 ether) {}

Solution

  1. Step 1: Identify the variable holding sent ether amount

    msg.value stores the amount of ether sent with the call.
  2. Step 2: Use comparison to check minimum ether sent

    Checking if msg.value >= 1 ether correctly tests if at least 1 ether was sent.
  3. Final Answer:

    if (msg.value >= 1 ether) {} -> Option A
  4. Quick Check:

    Check ether amount with msg.value [OK]
Hint: Use msg.value to check ether sent, not msg.sender [OK]
Common Mistakes:
  • Using msg.sender to compare ether amounts
  • Using equality instead of >= for minimum check
  • Mixing up msg.sender and msg.value in conditions
3. What will be the output of this Solidity function when called by address 0xABC with 2 ether sent?
function pay() public payable returns (address, uint) {
    return (msg.sender, msg.value);
}
medium
A. (0xABC, 0)
B. (msg.sender, msg.value)
C. (address(0), 2 ether)
D. (0xABC, 2 ether)

Solution

  1. Step 1: Understand msg.sender and msg.value in the function

    msg.sender is the caller's address (0xABC), and msg.value is the ether sent (2 ether).
  2. Step 2: Return values match caller and sent ether

    The function returns these two values exactly as (0xABC, 2 ether).
  3. Final Answer:

    (0xABC, 2 ether) -> Option D
  4. Quick Check:

    Caller and sent ether returned correctly [OK]
Hint: Returned values match caller address and sent ether [OK]
Common Mistakes:
  • Assuming msg.value is zero if not explicitly set
  • Confusing msg.sender with zero address
  • Expecting variable names instead of actual values
4. Identify the error in this Solidity function that tries to check if the caller sent ether:
function deposit() public {
    require(msg.value > 0, "Send some ether");
}
medium
A. Missing payable keyword in function declaration
B. Incorrect use of require syntax
C. msg.value cannot be compared with > 0
D. Function should return a value

Solution

  1. Step 1: Check function declaration for payable

    To receive ether and access msg.value, the function must be marked payable.
  2. Step 2: Identify missing payable keyword

    The function lacks payable, so msg.value will always be zero and cause errors.
  3. Final Answer:

    Missing payable keyword in function declaration -> Option A
  4. Quick Check:

    Functions receiving ether must be payable [OK]
Hint: Add payable to receive ether and use msg.value [OK]
Common Mistakes:
  • Forgetting payable keyword
  • Thinking require syntax is wrong
  • Expecting function to return value unnecessarily
5. You want to write a Solidity function that only accepts payments from a specific address and requires at least 0.5 ether sent. Which code snippet correctly enforces both conditions?
hard
A. function pay() public payable { require(msg.sender != 0x123 && msg.value > 0.5 ether); }
B. function pay() public { require(msg.sender == 0x123); require(msg.value >= 0.5 ether); }
C. function pay() public payable { require(msg.sender == 0x123 && msg.value >= 0.5 ether, "Unauthorized or insufficient funds"); }
D. function pay() public payable { if (msg.sender == 0x123 || msg.value >= 0.5 ether) {} }

Solution

  1. Step 1: Ensure function is payable to accept ether

    Only options A, C, and D have payable, needed to receive ether.
  2. Step 2: Check conditions for sender and ether amount

    function pay() public payable { require(msg.sender == 0x123 && msg.value >= 0.5 ether, "Unauthorized or insufficient funds"); } uses require with AND (&&) to enforce both sender address and minimum ether sent.
  3. Step 3: Evaluate other options for correctness

    function pay() public { require(msg.sender == 0x123); require(msg.value >= 0.5 ether); } lacks payable, so msg.value is always zero. function pay() public payable { if (msg.sender == 0x123 || msg.value >= 0.5 ether) {} } uses OR (||), allowing either condition alone. function pay() public payable { require(msg.sender != 0x123 && msg.value > 0.5 ether); } uses != which rejects the correct sender.
  4. Final Answer:

    function pay() public payable { require(msg.sender == 0x123 && msg.value >= 0.5 ether, "Unauthorized or insufficient funds"); } -> Option C
  5. Quick Check:

    Use payable and require with AND for both checks [OK]
Hint: Use payable and require with && to check sender and value [OK]
Common Mistakes:
  • Forgetting payable keyword
  • Using OR instead of AND for conditions
  • Checking msg.sender with != instead of ==
  • Not combining conditions in one require
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard