Explain solidity>mappings

Mappings

Players need a way to withdraw their credits. We'll use the withdraw pattern for safety.

Understanding Mappings:

mapping(address => uint256) public credits;

This creates a key-value store. Each address has a uint256 balance.

Access it like: credits[msg.sender]



The Withdraw Pattern:

1. Check there are credits: require(credits[msg.sender] > 0, "No credits");

2. Store the amount: uint256 amount = credits[msg.sender];

3. Zero the balance FIRST: credits[msg.sender] = 0;

4. Then send the ETH (we'll learn how in the next lesson)



Why Zero First?

This prevents reentrancy attacks where someone could call withdraw multiple times before the balance updates.



Create the withdraw() function with these first 3 steps.

Create withdraw function
0%
Requirements:
Declares withdraw function
withdraw is public
Checks credits > 0
Stores amount in variable
Zeros credits[msg.sender]
Zeros credits before any transfer