Hi @Wyse_Sousa,
You have added the additional lines of code needed to solve the problem with the withdraw function, but youâve left out the following statement which was in the code you were given to start with âŚ
msg.sender.transfer(amount);
If you deploy your contract, make a deposit, call your withdraw function as it is, and then call getBalance() with the same withdrawerâs address, you will notice that the callerâs individual balance in the mapping is reduced by the requested withdrawal amount, but their external address does not receive the Ether. If the function is coded correctly, you should see the withdrawerâs external address balance (showing in the Account field near the top of the Deploy & Run Transactions panel in Remix) increase by this amount.
Instead, each time your withdraw() function is called, the caller is effectively losing ownership of the amount of Ether they have requested to withdraw. Essentially, the smart contract has been coded to steal usersâ funds, so Iâm sure you can see why this is a very serious bug!
The individual user balances in the mapping perform an internal accounting role, and record each userâs share of the contractâs total Ether balance (effectively, each userâs entitlement to withdraw Ether from the contract). However, reducing a userâs individual balance in the mapping for a specific amount doesnât actually transfer an equivalent amount of Ether from the contract address balance to the userâs external address balance: thatâs what the transfer
method does âŚ
<payable address>.transfer(uint amount);
Once youâve included the missing transfer
method, have a look at this post to make sure the order of your statements within the withdraw() function body ensures your smart contract is protected against re-entrancy attacks.
Let me know if anything is unclear, or if you have any questions