I have a function which looks more or less like this:

function exchange(uint256 amount, bool specialCase) external returns (uint256) {
    let retVal;
    if (specialCase) {
        retVal = 0;
    else if (amount > address(this).balance) {
        register(amount - address(this).balance);
        retVal = address(this).balance;
    else {
        retVal = amount;
    return retVal;

As you can see, in some cases, it will execute msg.sender.transfer(0).

Of course, I can avoid this by placing msg.sender.transfer(retVal) only where retVal > 0.

But I think that my code looks "cleaner" this way, and I would like to keep it as is.

So I would like to know if there is any gas cost in executing msg.sender.transfer(0).

Thank you!!!


It cost about 1200 gas according to remix.

using this contract:

contract test  {
    address user = 0xca35b7d915458ef540ade6068dfe2f44e8fa733c;

    function send() public {

Produces a transaction cost of 22381 gas, with execution cost of 1173 gas

Changing the value of the transfer to anything different from zero (note that the contract must have a positive balance) make the execution cost about 8000.

Hope this helps

  • Yes, as a matter of fact it does help. Thank you very much for running it on Remix!!! Oct 15 '18 at 11:19
  • To add to this: transfer does a call into the contract with a 2300 gas stipend, so if the transfer is too a contract, it can cost up to 2300 gas.
    – natewelch_
    Oct 15 '18 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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