Hi @Ouzo69,
First of all, apologies for the long delay in providing you with some feedback on this. Unfortunately, your post was missed
Yes, thatās exactly right
This isnāt needed because you are transferring the contract balance to msg.sender
(which can only be the owner due to the onlyOwner modifier): msg.sender
is always payable by default (unlike address variable definitions, which are unpayable by default). However, you would need to make owner
a payable address if you transferred to owner
, instead of msg.sender
:
function withdrawAll () public onlyOwner {
owner.transfer(getContractBalance()); // owner instead of msg.sender
assert(getContractBalance() == 0);
}
I think Iāve already commented before on how itās very clever how you have made the withdrawAll function more concise by using a function call to getContractBalance to retrieve the balance to transfer. You could also replace the function call withā address(this).balance
:
function withdrawAll () public onlyOwner {
msg.sender.transfer(address(this).balance);
assert(address(this).balance == 0);
}
It would be interesting to see if there was much difference in the gas cost between these two alternativesā¦
This is an excellent use of assert to check for an important invariant. This very much improves your contract security