Nice solution @Nelson1
You have included all of the additional lines of code needed to solve the problem with the withdraw function, and you have them all in an order that optimises security:
- check inputs (require statement)
- effects (update the contract state for reduction in balance)
- external interactions (perform the transfer of funds from the smart contract address to the external wallet address).
Notice that the withdraw function header also includes returns(uint)
. This is not mandatory to include, and the function can still operate effectively without returning a value. But as itâs been included in the function header, you also need to include a return statement in the function body.
The additional event that youâve added is also good. It emits and logs relevant information when a call to the withdraw function has executed successfully. Just remember that the convention is to start event names with a capital letter (e.g. event Withdrawal
), although your code does still work using a lower case letter. What is a problem, though, is writing the mapping name balance
starting with a capital letter: Balance
. If you have also used an upper case letter in your mapping name, then your code will work, but if you havenât then it will throw an error. Remember that itâs standard practice to start variable names (including mappings and arrays) with a lower case letter. Itâs just struct and event names that we start with a capital letter.
Your posted solution will also not execute, because youâve missed off a semi colon at the end of one of your lines. You should format your code before posting it. This should also make it easier to spot any errors like the ones Iâve just mentioned.
Follow the instructions in this FAQ:âHow to post code in the forum