0

I have this function:

function withdraw() public payable onlyOwner {
    require(payable(msg.sender).send(address(this).balance));
}

I see in others contracts this form of the same function:

function withdraw() public payable onlyOwner {
    (bool success, bytes memory data) = payable(msg.sender).call{value: address(this).balance}("");
    require(success);
}

As I know, if the require fails, value isn't deducted from the contract's balance, isn't true?

If so, what's the need of handling the result? There maybe a security issue using the first one without it?

Thanks

2
  • I do not see return statement in your question. Commented Oct 6, 2021 at 8:28
  • @Itération122442 fixed. Commented Oct 6, 2021 at 11:23

1 Answer 1

2

The two examples use different means of transferring value. Also, they obviously have different return data: one has only one return value, while the other has two return values.

If there are two return values, you can't simply inline the require, since require needs its first parameter to be of type bool. In the case of two return values, the first parameter would be of type (bool, bytes) (a tuple).

So, if there are two return values, you have to first assign the needed value to a variable and after that check its value in a require.

And, yes, if the contract reverts (such as require does if it fails), everything is reverted back to original - it's like the transaction had never even occurred.

2
  • Thanks. If I true understand, both functions do the same action and both work, the only difference is how they invoke the payable function, isn't true? Commented Oct 6, 2021 at 17:06
  • They're not exactly the same, but yes they both transfer assets. The payable function is invoked with both methods. Commented Oct 6, 2021 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.