Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updateShareholder in FeeSplitter.sol can be implemented more efficiently #11

Open
code423n4 opened this issue Nov 12, 2021 · 0 comments
Assignees
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

0x0x0x

Vulnerability details

Explanation

updateShareholder in FeeSplitter.sol can be implemented more efficiently. The updated version consumes less gas and also has the second require statement earlier, which reduces the gas cost in case the statement of second require is not fullfilled.

FeeSplitter.sol : L166-174:

    function updateShareholder(uint256 _accountIndex, uint256 _weight) external onlyOwner {
        require(_accountIndex + 1 <= shareholders.length, "FeeSplitter: INVALID_ACCOUNT_INDEX");
        uint256 _totalWeights = totalWeights;
        _totalWeights -= shareholders[_accountIndex].weight;
        shareholders[_accountIndex].weight = _weight;
        _totalWeights += _weight;
        require(_totalWeights > 0, "FeeSplitter: TOTAL_WEIGHTS_ZERO");
        totalWeights = _totalWeights;
    }

can be replaced with:

    function updateShareholder(uint256 _accountIndex, uint256 _weight) external onlyOwner {
        require(_accountIndex + 1 <= shareholders.length, "FeeSplitter: INVALID_ACCOUNT_INDEX");
        totalWeights = totalWeights + _weight - shareholders[_accountIndex].weight;
        require(_totalWeights > 0, "FeeSplitter: TOTAL_WEIGHTS_ZERO");
        shareholders[_accountIndex].weight = _weight;
    }

Tools Used

Manual analysis

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Nov 12, 2021
code423n4 added a commit that referenced this issue Nov 12, 2021
@adrien-supizet adrien-supizet self-assigned this Nov 18, 2021
@adrien-supizet adrien-supizet added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Nov 23, 2021
@adrien-supizet adrien-supizet removed their assignment Nov 23, 2021
@maximebrugel maximebrugel self-assigned this Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants