code-423n4 / 2022-01-sherlock-findings

0 stars 0 forks source link

Cheaper gas to check `address(this).balance` first then do the transfer #259

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

Tomio

Vulnerability details

Impact

because if there's no check address(this).balance != 0 then doesnt need to call transfer, can save gas

Proof of Concept

https://github.com/code-423n4/2022-01-sherlock/blob/main/contracts/managers/Manager.sol#L51

Tools Used

Remix

Recommended Mitigation Steps

Example: If transfer first:

function sendETH(address _receive) external payable { 
        (bool success, ) = _receive.call{value: address(this).balance}('');
        if(success == false) require(false, "Transfer ETH Failed");
    }
// 22276 gas

If check address(this).balance first

function correctSendEth(address _receive) external payable {
        uint eth_balance = address(this).balance;
        if (eth_balance != 0){
            (bool success, ) = _receive.call{value: eth_balance}('');
            if(success == false) require(false, "Transfer ETH Failed");
        }

    }
//21923 gas
jack-the-pug commented 2 years ago

Dup #211