1

Yesterday I found out about the existence of selfbalance() and that it's cheaper than address(this).balance. I checked the EVM opcodes:

opcode 31 BALANCE - The static cost is 0. If the accessed address is warm, the dynamic cost is 100. Otherwise, the dynamic cost is 2600. See section access sets.

opcode 47 SELFBALANCE - Semantically equivalent of calling BALANCE with ADDRESS as a parameter, but with a reduced gas cost.Minimum gas - 5.

So I wrote 2 similar contracts to check it:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "hardhat/console.sol";


interface ICheckBal{
    function YULF() external view returns (uint256);
}

interface ICheckYUL{
    function YULF() external view returns (uint256);
}

contract CheckYUL {

    function YULF() public view returns (uint256) {
        uint256 self;
        assembly {
            self :=selfbalance()
        }

        
        return self;
    }

    receive() external payable {

    }


}



contract CheckBal {

    function YULF() external view returns (uint256) {
        uint256 callStatus;
        callStatus = address(this).balance;

        
        return callStatus;
    }

    receive() external payable {

    }


}

contract TestBal {
    ICheckBal checkBal = ICheckBal(0xDA0bab807633f07f013f94DD0E6A4F96F8742B53);

    function testing() view public returns(uint256){
        
        return checkBal.YULF();
    }
    
}

contract TestYul {
    ICheckYUL checkYUL = ICheckYUL(0xf8e81D47203A594245E36C48e151709F0C19fBe8);

    function testing() view public returns(uint256){
        
        return checkYUL.YULF();
    }
    
}

In both cases the result was the same:

execution cost  26839 gas (Cost only applies when called by a contract) (Remix)

I tried to ask ChatAI, but seems like it doesn't understand what selfbalance() is. And about balance it answered: If you include address(this).balance in a contract method, you will need to pay gas to execute that method.

I tried it only in Remix and Goerli. So maybe it works only in mainnet? If you have some experience in gas optimization could you tell me how it works? Thanks.

1 Answer 1

3

It's because the solc compiler is smart enough to optimize your solidity code callStatus = address(this).balance; to use SELFBALANCE by default.

Debugged on Remix

You can try to bypass solc optimization by using assembly for that also:

    function YULF() external view returns (uint256) {
        uint256 callStatus;
        //callStatus = address(this).balance;
        assembly {
            callStatus :=balance(address())
        }
        return callStatus;
    }
3
  • 1
    Thanks a lot. So advice like: "use selfbalance() to reduce gas cost" doesn't make sense? Will the compiler do it by default? Jan 8, 2023 at 10:15
  • it's hard to answer because the solc compiler optimization changes very often. It may make sense in some older compiler versions (you can try to test). But at least we know it doesn't make sense in this solc 0.8.17 version.
    – minhhn2910
    Jan 8, 2023 at 12:17
  • It works for me thanks a lot. May 8 at 15:48

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.