Solidity – What Happens to msg.value if require Fails in Solidity

ethersolidity

Imagine I have a function that looks something like this. Is the ether refunded to the sender (invoker) if require fails?

function foo(uint _minShares) public {
        uint256 ethReserveBeforeDeposit = address(this).balance - msg.value;
        // compute proportion of deposited ether and total ether balance to determine amount of shares
        uint256 sharesMinted = (msg.value / ethReserveBeforeDeposit) * totalSupply();
        require(sharesMinted >= _minShares);
        // what happens if require fail, is msg.value refunded?
    }

Best Answer

Just tested it out on remix.ethereum

  • first of all the foo function must have the keyword payable to receive ether
  • If require fails the the initial sent ether to foo function will be refunded but gas will still be used till the require statement is reached.
Related Topic