Reusing the boolean variable sent in multiple calls

Hello zeppelins

I am changing all the transfer functions in my contracts to adapt to the present recommendations.

In a function in which I have several send to several addresses I have eliminated the transfer function for the call and it occurred to me to reuse the variable bool sent of the first function to be used in the following ones.

Do you consider that this is a good gas saving or it can will give me any problem?

        function commisions(uint256 amountComision) internal                 
    {  
        string memory errorMessage = "e21";
        (bool sent, ) = payable(projectAddress).call{value: (amountComision * (distributionPercentage / 
        2)) / 10**2}("");// 10%
        require(sent, errorMessage); 
        (sent, ) = payable(teamAddressOne).call{value: (amountComision * distributionPercentage) / 
        10**2}("");
        require(sent, errorMessage);         
        (sent, ) = payable(teamAddressTwo).call{value: (amountComision * distributionPercentage) / 
        10**2}("");
        require(sent, errorMessage); 
        (sent, ) = payable(teamAddressThree).call{value: (amountComision * distributionPercentage) / 
        10**2}("");
        require(sent, errorMessage); 
        (sent, ) = payable(teamAddressFour).call{value: (amountComision * distributionPercentage) / 
        10**2}("");
        require(sent, errorMessage); 

    }

The gas improvement achieved by reusing the same local variable would be negligible (if there even IS any gas improvement to begin with).

You may want to consider reusing the code instead, but again - this will not impact the gas consumption in any significant manner, it will just allow for easier maintenance:

function commisions(uint256 amountComision) internal {
    uint256 amount = amountComision * distributionPercentage / 100;
    commision(projectAddress  , amount / 2);
    commision(teamAddressOne  , amount);
    commision(teamAddressTwo  , amount);
    commision(teamAddressThree, amount);
    commision(teamAddressFour , amount);
}

function commision(address recipient, uint256 amount) private {
    (bool sent, ) = payable(recipient).call{value: amount}("");
    require(sent, "e21");
}

Actually, if distributionPercentage is a state (non-constant-non-immutable global) variable, then this actually WILL save quite a bit of gas, which your current implementation spends on reading that state variable several times.

Fantastic refactoring. A much clearer reading. The variable distributionPercentag should be a constant.

Now it is not because its final nature is being decided. But because of its fixed use in all cases I consider it would be appropriate.

Given what you have told me, thinking a little, I understand that the only possible saving is in global variables and the variable sent is temporary.

Thanks for your help.

1 Like

Gas saving can be achieved in many ways, with varying levels of significance.

But in general, the most gas-critical points in your code are:

  1. Storage write (changing the value of a state variable from zero to non-zero)
  2. Storage update (changing the value of a state variable from non-zero to non-zero)
  3. External calls (calling a function in another contract)
  4. Storage read (reading the value of a state variable)
1 Like