There seem to be two different ways to write a withdrawal function to get ETH funds out of a contract you've deployed. I'm wondering which is the better - or "safer" way to do this (in 2022.)
Here are the two versions of the withdrawal methods that I've come across:
// Version 1:
function withdraw() public onlyOwner {
uint balance = address(this).balance;
msg.sender.transfer(balance);
}
// Version 2:
function withdrawMoney() external onlyOwner {
(bool success, ) = msg.sender.call{ value: address(this).balance } ("");
require(success, "Transfer failed.");
}
I'm also noticing that neither version seems to handle or usereentrancy. Is this because both use the onlyOwner modifier, so there's no need to worry about reentrancy?
(I'd obviously be curious about WHY it'd be better to go with one of these methods over the other.)