code-423n4 / 2022-01-timeswap-findings

2 stars 0 forks source link

Caching arithmetic results can avoid redundant storage reads and save gas #164

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

WatchPug

Vulnerability details

Caching the new storage value in the stack and reusing it instead of reading from storage redundantly can save gas.

Instances include:

https://github.com/code-423n4/2022-01-timeswap/blob/bf50d2a8bb93a5571f35f96bd74af54d9c92a210/Timeswap/Timeswap-V1-Core/contracts/TimeswapPair.sol#L185-L189

pool.state.x += xIncrease;
pool.state.y += yIncrease;
pool.state.z += zIncrease;

emit Sync(maturity, pool.state.x, pool.state.y, pool.state.z);

Can be changed to:

uint112 newX = pool.state.x + xIncrease;
uint112 newY = pool.state.y + yIncrease;
uint112 newZ = pool.state.z + zIncrease;
pool.state.x = newX;
pool.state.y = newY;
pool.state.z = newZ;

emit Sync(maturity, newX, newY, newZ);

https://github.com/code-423n4/2022-01-timeswap/blob/bf50d2a8bb93a5571f35f96bd74af54d9c92a210/Timeswap/Timeswap-V1-Core/contracts/TimeswapPair.sol#L256-L260

pool.state.x += xIncrease;
pool.state.y -= yDecrease;
pool.state.z -= zDecrease;

emit Sync(maturity, pool.state.x, pool.state.y, pool.state.z);

Consider caching new value of pool.state.x, pool.state.y and pool.state.z;

https://github.com/code-423n4/2022-01-timeswap/blob/bf50d2a8bb93a5571f35f96bd74af54d9c92a210/Timeswap/Timeswap-V1-Core/contracts/TimeswapPair.sol#L330-L336

pool.state.x -= xDecrease;
pool.state.y += yIncrease;
pool.state.z += zIncrease;

asset.safeTransfer(assetTo, xDecrease);

emit Sync(maturity, pool.state.x, pool.state.y, pool.state.z);

Consider caching new value of pool.state.x, pool.state.y and pool.state.z;

Mathepreneur commented 2 years ago

We have stack too deep error.

0xean commented 2 years ago

closing as invalid based on stack too deep errors.