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

Gas Optimizations #196

Open
code423n4 opened this issue Apr 20, 2022 · 0 comments
Open

Gas Optimizations #196

code423n4 opened this issue Apr 20, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Gas Optimazations

Unnecessarily initialized variable

https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/SupplySchedule.sol#L103

uint256 mintable = 0;

Proposed change:

uint256 mintable;

https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/SupplySchedule.sol#L192

uint256 mintable = 0;

Proposed change:

uint256 mintable;

Minimizing storage reads

https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/SupplySchedule.sol#L178

function getMintableDebug(uint256 lastMintTimestamp) external {

    require(
        globalStartTimestamp > 0,
        "SupplySchedule: minting not started"
    );
    require(
        lastMintTimestamp > globalStartTimestamp,
        "SupplySchedule: attempting to mint before start block"
    );
        ...

        emit log_named_uint("globalStartTimestamp", globalStartTimestamp);
        emit log_named_uint("epochLength", epochLength);

        uint256 startingEpoch = (lastMintTimestamp - globalStartTimestamp) 
            epochLength;

        uint256 endingEpoch = (block.timestamp - globalStartTimestamp) /
            epochLength;
    ;

            uint256 epochStartTime = globalStartTimestamp + i * epochLength;
            uint256 epochEndTime = globalStartTimestamp + (i + 1) * epochLength;
            ...
    }

Proposed changes:

function getMintableDebug(uint256 lastMintTimestamp) external {
        uint256 memory _globalStartTimestamp = globalStartTimestamp;
        uint256 memory _epochLength = epochLength;

        require(
            _globalStartTimestamp > 0,
            "SupplySchedule: minting not started"
        );
        require(
            lastMintTimestamp > _globalStartTimestamp,
            "SupplySchedule: attempting to mint before start block"
        );
        ...

        emit log_named_uint("globalStartTimestamp", _globalStartTimestamp);
        emit log_named_uint("epochLength", _epochLength);

        uint256 startingEpoch = (lastMintTimestamp - _globalStartTimestamp) /
            _epochLength;

        uint256 endingEpoch = (block.timestamp - _globalStartTimestamp) /
            _epochLength;
    ;

            uint256 epochStartTime = _globalStartTimestamp + i * _epochLength;
            uint256 epochEndTime = _globalStartTimestamp + (i + 1) * _epochLength;
            ...
    }

https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/StakedCitadelVester.sol#L132

function vest(
        address recipient,
        uint256 _amount,
        uint256 _unlockBegin
    ) external {
        require(msg.sender == vault, "StakedCitadelVester: only xCTDL vault");
        require(_amount > 0, "StakedCitadelVester: cannot vest 0");

        vesting[recipient].lockedAmounts =
            vesting[recipient].lockedAmounts +
            _amount;
        vesting[recipient].unlockBegin = _unlockBegin;
        vesting[recipient].unlockEnd = _unlockBegin + vestingDuration;

        emit Vest(
            recipient,
            vesting[recipient].lockedAmounts,
            _unlockBegin,
            vesting[recipient].unlockEnd
        );
    }

proposed change:

 function vest(
        address recipient,
        uint256 _amount,
        uint256 _unlockBegin
    ) external {
        require(msg.sender == vault, "StakedCitadelVester: only xCTDL vault");
        require(_amount > 0, "StakedCitadelVester: cannot vest 0");
        uint _lockedAmounts = vesting[recipient].lockedAmounts; 

        vesting[recipient].lockedAmounts =
            _lockedAmounts +
            _amount;
        vesting[recipient].unlockBegin = _unlockBegin;
        vesting[recipient].unlockEnd = _unlockBegin + vestingDuration;

        emit Vest(
            recipient,
            _lockedAmounts,
            _unlockBegin,
            vesting[recipient].unlockEnd
        );
    }

Using Calldata instead of memory

https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/StakedCitadel.sol#L319

function deposit(uint256 _amount, bytes32[] memory proof)
        external
        whenNotPaused
    {
        _depositWithAuthorization(_amount, proof);
    }

proposed change:

function deposit(uint256 _amount, bytes32[] calldata proof)
        external
        whenNotPaused
    {
        _depositWithAuthorization(_amount, proof);
    }

https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/StakedCitadel.sol#L329

 function depositAll(bytes32[] memory proof) external whenNotPaused {
        _depositWithAuthorization(token.balanceOf(msg.sender), proof);
    }

proposed change:

function depositAll(bytes32[] calldata proof) external whenNotPaused {
        _depositWithAuthorization(token.balanceOf(msg.sender), proof);
    }

https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/StakedCitadel.sol#L363

function depositFor(
        address _recipient,
        uint256 _amount,
        bytes32[] memory proof
    ) external whenNotPaused {
        _depositForWithAuthorization(_recipient, _amount, proof);
    }

proposed change:

function depositFor(
        address _recipient,
        uint256 _amount,
        bytes32[] calldata proof
    ) external whenNotPaused {
        _depositForWithAuthorization(_recipient, _amount, proof);
    }
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Apr 20, 2022
code423n4 added a commit that referenced this issue Apr 20, 2022
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)
Projects
None yet
Development

No branches or pull requests

1 participant