0
0
Blockchain / Solidityprogramming~30 mins

Mappings in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Mappings in Solidity
📖 Scenario: You are creating a simple blockchain contract to store and retrieve user balances. This contract uses a mapping to link each user's address to their balance.
🎯 Goal: Build a Solidity contract that uses a mapping to store balances for addresses, set a balance for one user, and then retrieve and display that balance.
📋 What You'll Learn
Create a mapping named balances that links address to uint
Create a uint variable named initialBalance and set it to 100
Write a function setBalance that sets the initialBalance for the sender's address in balances
Write a function getBalance that returns the balance of the sender's address
Print the balance of the sender after setting it
💡 Why This Matters
🌍 Real World
Mappings are used in blockchain contracts to store data linked to user addresses, like balances, permissions, or votes.
💼 Career
Understanding mappings is essential for blockchain developers to manage user data efficiently and securely in smart contracts.
Progress0 / 4 steps
1
Create the mapping to store balances
Create a mapping named balances that links address to uint inside a contract named BalanceTracker.
Blockchain / Solidity
Need a hint?

Think of mapping as a dictionary where each address has a number (uint) balance.

2
Add an initial balance variable
Inside the BalanceTracker contract, create a uint variable named initialBalance and set it to 100.
Blockchain / Solidity
Need a hint?

This variable will hold the starting balance for users.

3
Write a function to set the sender's balance
Add a public function named setBalance inside BalanceTracker that sets the sender's balance in balances to initialBalance.
Blockchain / Solidity
Need a hint?

Use msg.sender to get the address calling the function.

4
Write a function to get the sender's balance and print it
Add a public view function named getBalance inside BalanceTracker that returns the sender's balance from balances. Then, write a comment showing how to call setBalance and getBalance to print the balance.
Blockchain / Solidity
Need a hint?

The view keyword means the function does not change the contract state.