Hi @madingo,
You seem to have got quite confused here, so letâs have a look at each of the additional lines of code you should be adding to solve the problem with the withdraw function. But before we do that, you need to review your code carefully because you have lots of syntax errors (spelling mistakes, incorrect operators etc.). Accuracy is important, because the slightest error (e.g. capital letter instead of lower case, =
instead of ==
etc.) will mean your code wonât compile. Before you deploy your code in Remix, you should be checking and correcting any compiler errors. Then, once you have a contract without any compiler errors, which deploys successfully and solves the assignment problem, you can just copy and paste your solution code into the forum text editor and format it using the </> icon in the menu at the top of the text editor. This will wrap your code in 2 sets of 3 back ticks, like this:
```
My code needs formatting
```
Then when your post is displayed, your code will be appear formatted, like this:
My code is now formatted
Syntax Errors in your posted code:
==>
uint Amount
-
return(uint)
â in the function header
msg.transfer(amount)
-
requires(mg.sender = owner)
â x2 errors:â(i) requires
â (ii) =
previous senderBalance
- missing semicolons at the end of most statements (
;
)
- getBalance function opening curly bracket is the wrong way round.
Now letâs look at each of the lines of code you should be adding to the withdraw function:
- require statement: If you restrict the withdraw function to
owner
, then only that single address can withdraw funds from their own individual balance within the contract. The owner address is set by the constructor when the contract is deployed, and will be the address which deploys the contract. Instead, the require statement should do what you have stated here:
So you need a condition in your require statement that checks whether the msg.sender has a balance in the mapping which is greater than or equal to the withdrawal amount requested.
-
The line of code transferring amount
to msg.senderâs wallet address will only reduce the contractâs ether balance as a whole, and not the separate account balance mapped to msg.sender in the mapping. It is vital to also update the balance mapping, so that the contract records which user account the withdrawal came from. If we donât do that then there is no way of knowing how much each user address can withdraw from the total funds in the contract, because the balance we would be checking in the require statement wouldnât have been reduced each time a withdrawal was made (only the total contract balance would have been reduced).
-
You also need a return statement
Once youâve had a go at making the above modifications, have a look at this post which explains why the order of the statements is important for security. Then make sure the statements in your solution are in the correct order.
Your assert statement is a good idea, but will only work appropriately if you declare a local variable previousSenderBalance within this function, and assign balance[msg.sender]
to it before it is reduced by the withdrawal amount. At the moment your assert statement will throw a compiler error because previousSenderBalance doesnât exist.
Let us know if you have any questions about how to correct this, or if there is anything that you donât understand 