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

0 stars 0 forks source link

Gas Optimizations #121

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

2022-03-volt gas optimization

1 use unchecked. Underflow is already checked in the previous require statement, so it will never happen. That’s why you can use unchecked to save gas costs in the following line.

https://github.com/code-423n4/2022-03-volt/blob/main/contracts/utils/RateLimited.sol#L106

Unchecked { bufferStored = newBuffer - usedAmount; }

2 use unchecked. Like the comment describes, block timestamp always >= startTime and it will be also checked with if (!isTimeStarted()) that startTime is already initialized. You can use unchecked in the following line to save gas costs.

https://github.com/code-423n4/2022-03-volt/blob/main/contracts/utils/Timed.sol#L57

uint256 timePassed unchecked { timePassed = block.timestamp - startTime; }

3 use cache and unchecked in the following places. In require statement underflow for amount <= newBuffer is checked, so you can use unchecked for the following calculations. In addition, the calculation must be executed in the other place too. You can use cache for it.

https://github.com/code-423n4/2022-03-volt/blob/main/contracts/utils/MultiRateLimited.sol#L341 https://github.com/code-423n4/2022-03-volt/blob/main/contracts/utils/MultiRateLimited.sol#L351

uint bufferStore;

unchecked { bufferStore = newBuffer - amount; } rateLimitPerAddress[rateLimitedAddress].bufferStored = uint112(bufferStore);

emit IndividualBufferUsed(rateLimitedAddress, amount, bufferStore);

3 use unchecked in mint. amountFeiToTransfer must be less than or equal to amountVoltOut. You can save gas costs by wrapping the following calculation with unchecked.

https://github.com/code-423n4/2022-03-volt/blob/main/contracts/peg/NonCustodialPSM.sol#L290

uint256 amountFeiToMint; unchecked { amountFeiToMint = amountVoltOut - amountFeiToTransfer; }