0
0
Blockchain / Solidityprogramming~30 mins

msg.value and msg.sender in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding msg.value and msg.sender in Solidity
📖 Scenario: You are creating a simple smart contract for a crowdfunding campaign on the Ethereum blockchain. People can send money to support the project, and the contract keeps track of who sent money and how much.
🎯 Goal: Build a smart contract that records the sender's address and the amount of Ether sent using msg.sender and msg.value. Then display the total amount collected and the last sender's address.
📋 What You'll Learn
Create a public variable lastSender to store the address of the last person who sent Ether
Create a public variable totalCollected to store the total amount of Ether collected
Write a receive function to accept Ether and update lastSender and totalCollected
Write a function getSummary that returns lastSender and totalCollected
💡 Why This Matters
🌍 Real World
Smart contracts on Ethereum use <code>msg.sender</code> and <code>msg.value</code> to handle payments and identify users.
💼 Career
Understanding these variables is essential for blockchain developers building decentralized applications that handle money.
Progress0 / 4 steps
1
Set up the contract and variables
Create a contract called CrowdFund. Inside it, declare a public address variable called lastSender and a public unsigned integer variable called totalCollected.
Blockchain / Solidity
Need a hint?

Use address public lastSender; and uint public totalCollected; inside the contract.

2
Add the receive function to accept Ether
Add a receive function that is external and payable. Inside it, set lastSender to msg.sender and add msg.value to totalCollected.
Blockchain / Solidity
Need a hint?

The receive function must be external payable. Use msg.sender and msg.value inside it.

3
Create a function to get the summary
Write a public view function called getSummary that returns the lastSender address and the totalCollected amount.
Blockchain / Solidity
Need a hint?

Use function getSummary() public view returns (address, uint) and return the two variables.

4
Test the contract output
Write a comment showing the expected output of calling getSummary() after someone sends 1 Ether from address 0x123.... The output should show lastSender as 0x123... and totalCollected as 1000000000000000000 (1 Ether in wei).
Blockchain / Solidity
Need a hint?

Remember 1 Ether = 10^18 wei. Show the address and total in a comment.