Hi @LELUK911,
This is an excellent solution which not only solves the problem with the withdraw() function, but does it by implementing some advanced smart-contract coding techniques
Correctly coded and well implemented modifier with a require() statement to prevent users from withdrawing more than their individual balance.
Correct implementation of the call
method, as an alternative to using the transfer
method, in order to transfer the withdrawal amount from the contract address balance to the withdrawer’s own external address balance.
You are right that it’s not necessary to return the updated balance, and so returns(uint)
 can be removed from both the withdrawalEth() and the depositEth() function headers.
However, do you not think the getBalance() function is a more straightforward way for a user to retrieve their current balance, instead of the automatically created getter for the public mapping? The getBalance() function uses msg.sender
to avoid having to input an address argument.
Your withdraw event and corresponding emit statement are coded well enough to enable appropriate data to be logged when the withdrawalEth() function is successfully executed. The emit statement is also in the correct position in the function body.
However, you haven’t named the parameters in either of the event declarations (for both the depositEth and the withdrawalEth functions). Naming the event data isn’t mandatory, and both data values will still be emitted and logged for both events; but it will make the data clearer and easier to identify, and especially in situations where you have more than one parameter with the same data type.
Yes … your withdrawalEth() function code ensures that each operation is executed in the correct order to reduce security risk:
- Check inputs (require statements)
- Effects (update the contract state)
- External Interactions (e.g.
transfer
or call
method)
Modifying the contract state for the reduction in the individual user’s balance before actually transferring the funds out of the contract to the external address helps to prevent re-entrancy attacks from occuring. In fact, in terms of protection against re-entrancy attacks, your stay_paused
modifier reinforces your contract’s security even further
The automatic getter created for this public state variable will not retrieve the current contract balance, because it is assigned to the state variable on deployment (when the contract balance is zero) and then never updated. So, the balance_Bank
 getter will always return zero. Instead, you should add a getter to your contract which returns the contract balance at the time the getter is called e.g.
function getContractBalance() public view returns(uint) {
return address(this).balance;
}
By the way, don’t forget to do the Events Assignment and post your solution here. This is the assignment from the Events video lecture, which is near the end of the Additional Solidity Concepts section of the course, and it provides useful practice at coding events.
Just let me know if you have any questions about anything