4

I want to guarantee that user has enough Eth to execute back().

When you set little amount of eth in msg.value, there are no revert. However, if you set almost of all Eth you have, the revert is occurred.

Even if you have enough Eth msg.value + Gas fee, the revert is occurred.

Why?

You can try this in Remix.

  1. Choose Environment
  2. Choose Account which has 100 Eth
  3. Set Value to 90 ether.
  4. Execute back and revert.

Why?

    pragma solidity ^0.4.24;

    contract Back is Ownable {

      using SafeMath for uint;

      modifier affordPay() {
        require(msg.sender.balance > msg.value, "You don't have enough eth!!");
        _;
      }

      function back() external affordPay() payable {
        msg.sender.transfer(msg.value);
      }

    }

1 Answer 1

4

The type of check you are performing is not only useless but it's plain wrong.

When a user issues a transaction with 90 Ethers as value, the 90 Ethers (plus gas) is immediately deducted from the account. The 90 Ethers becomes then visible in address(this).balance. Therefore, also, the sender's balance is also updated and can be seen with address(msg.sender).balance - it will have about 9.9999 Ethers (10 minus tx costs).

So you don't need to check whether msg.value has an "acceptable" value - it will always contain a value which the user already committed into the transaction. Only if the transaction reverts the value is returned to the sender.

1
  • > the 90 Ethers (plus gas) is immediately deducted from the account. I understand what is wrong. I'm completely misunderstood the payable transaction works. Thanks.
    – R. M.
    Nov 4, 2018 at 8:35

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.