code-423n4 / 2022-03-volt-findings

0 stars 0 forks source link

Gas Optimizations #71

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

RateLimited.sol (function constructor() )

There is unnecessary call to _setBufferCap(_bufferCap); in line 43 (https://github.com/code-423n4/2022-03-volt/blob/f1210bf3151095e4d371c9e9d7682d9031860bbd/contracts/utils/RateLimited.sol#L43).

The call to _setBufferCap(_bufferCap); cause a call to _updateBufferStored();, that have the line bufferStored = buffer();, and buffer do unnecessary calculates because it returns 0 (because this is the start of the contract).

So to save gas we can change lines 41-43: code before:

        lastBufferUsedTime = block.timestamp;

        _setBufferCap(_bufferCap);

code after:

        lastBufferUsedTime = block.timestamp;

        uint256 oldBufferCap = bufferCap;
        bufferCap = _bufferCap;

        emit BufferCapUpdate(oldBufferCap, newBufferCap);

And we save gas of the unnecessary calculates of buffer()!

RateLimited.sol (function _depleteBuffer() )

In line 103 there is require that variable newBuffer is not 0. It can be checked in line 97 (because there is no change of newBuffer from this line. (link - https://github.com/code-423n4/2022-03-volt/blob/f1210bf3151095e4d371c9e9d7682d9031860bbd/contracts/utils/RateLimited.sol#L103)

code before:

        uint256 newBuffer = buffer();

        uint256 usedAmount = amount;
        if (doPartialAction && usedAmount > newBuffer) {
            usedAmount = newBuffer;
        }

        require(newBuffer != 0, "RateLimited: no rate limit buffer");
        require(usedAmount <= newBuffer, "RateLimited: rate limit hit");

code after:

        uint256 newBuffer = buffer();

        require(newBuffer != 0, "RateLimited: no rate limit buffer");

        uint256 usedAmount = amount;
        if (doPartialAction && usedAmount > newBuffer) {
            usedAmount = newBuffer;
        }

        require(usedAmount <= newBuffer, "RateLimited: rate limit hit");