Nice solution @mohkanaan
You have added all of the additional lines of code needed to solve the problem with the withdraw function, and they are also in the correct order to reduce security risk:
- check inputs (require statements)
- effects (update the contract state)
- external interactions
Just as you have done, itās important to modify the contract state for the reduction in the balanceā¦
balance[msg.sender] -= amount;
before actually transferring the funds out of the contract to the external wallet addressā¦
msg.sender.transfer(amount);
⦠just in case there is an attack after the transfer, but before the state is modified to reflect this operation. Youāll learn about the type of attack this prevents, and how it does it, in the courses which follow this one. But itās great youāre already getting into good habits in terms of smart contract security
Your transfer event and corresponding emit statement are also both correctly coded, and the emit statement will log appropriate data when the transfer() function is successfully executed.
But in general, an emit statement is probably better placed after an assert statement.
Donāt forget to either remove or comment out your addBalance function and event. As it is, the addBalance function allows users to artificially inflate their individual balances in the balance
mapping. This would allow some users to withdraw more than their entitlement, whilst others would lose funds.
Just let me know if you have any questions.