Excellent solution @DylanHepworth
You have included all of the additional functionality needed to solve the problem with the withdraw function, and you have the different lines in the correct order to reduce the security risk:
- check inputs (require statement)
- effects (update the contract state for reduction in balance)
- interactions (perform the transfer of funds from smart contract address to wallet address).
Itâs important to modify the contract state:
balance[msg.sender] -= amount;
before actually transferring the funds out of the contract:
msg.sender.transfer(amount);
⊠just in case there is an attack after the transfer, but before the state is modified to reflect this operation.
This is what youâve done, but your comment says the opposite:
The balance stored in the mapping (in the contract state) is adjusted before the withdrawal of ether occurs.
We wouldnât expect you to know this at this early stage, though, so Iâm just telling you for extra information. Youâll learn about the type of attack this prevents, and how it does it, in later courses. So, donât worry about the details for now, although itâs good to be aware of and to start getting into good habits