Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is function reverted?

so because of reentrancy attacks I'm structuring my function to not get hacked. So first updating mappings and so on and then sending the payment.

My question is what if the payment fails to go through. Will entire function be reverted of just the payment?

Because if only the payment than that would mean my mappings would be updated as if the payment went through.

Is this the case?

Thanks for answers!

like image 809
Kuly14 Avatar asked Oct 25 '25 17:10

Kuly14


1 Answers

function withdraw(uint256 _amount) external {
    balances[msg.sender] -= _amount;
    (bool success, ) = payable(msg.sender).call{value: _amount}("");
}

If the low-level .call() is unsuccessful, the value of success is set to false but that doesn't revert the whole transaction. So the balances value would be reflected even though the payment didn't go through.

You can add a require() condition to check the result of the payment. This will make the parent transaction fail if the internal transaction fails.

(bool success, ) = payable(msg.sender).call{value: _amount}("");
require(success);

Or you can use the .transfer() function (member of address payable, do not confuse with token transfers) that also fails the parent transaction if the internal transaction fals:

function withdraw(uint256 _amount) external {
    balances[msg.sender] -= _amount;
    payable(msg.sender).transfer(_amount);
}
like image 167
Petr Hejda Avatar answered Oct 28 '25 03:10

Petr Hejda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!