code-423n4 / 2022-09-quickswap-findings

0 stars 0 forks source link

Gas Optimizations #301

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Using unchecked blocks to save gas

When it is not possible for the arithmetic operation to underflow or overflow. The operation should be unchecked to save gas.

1.Consider unchecking the following arithmetic operations:

Using "x += y" cost more gas than "x = x + y"

1.Consider using this method only for the following state variables

Cache storage values in memory to minimize the gas cost

The code can be optimized by minimizing the number of SLOADs, since SLOADs are more expensive than MLOADs.

1.Consider caching MAX_VOLUME_PER_LIQUIDITY in the following function

Constants can be set as private to save gas

1.Consider declaring the constans UINT16_MODULO and MAX_VOLUME_PER_LIQUIDITY as private:

Splitting require() statements that use && saves gas

Instead of using the && operator in a single require statement to check multiple conditions,using multiple require statements with 1 condition per require statement will save 8 GAS per &&.

Copying state struct in memory is wrong and wastes gas

Using storage pointer instead of memory location can save a decent amount of gas.

1.One function in the contract AlgebraPool, use a memory location to retrieve the struct Cumulatives

2.One function in the contract AlgebraPool, use a memory location to retrieve the struct UpdatePositionCache

3.One function in the contract AlgebraPool, use a memory location to retrieve the struct SwapCalculationCache

4.One function in the contract AlgebraPool, use a memory location to retrieve the struct PriceMovementCache