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

3 stars 0 forks source link

Gas Optimizations #24

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Use custom errors

Solidity ^0.8.4 allow the use of custom errors to optimize gas usage. https://blog.soliditylang.org/2021/04/21/custom-errors/

Consolidate operations

For example: https://github.com/code-423n4/2022-03-timeswap/blob/00317d9a8319715a8e28361901ab14fe50d06172/Timeswap/Core/contracts/TimeswapPair.sol#L189

        assetIn = param.xIncrease + feeStoredIncrease;

https://github.com/code-423n4/2022-03-timeswap/blob/00317d9a8319715a8e28361901ab14fe50d06172/Timeswap/Core/contracts/TimeswapPair.sol#L238

        assetOut = _assetOut + feeOut;

https://github.com/code-423n4/2022-03-timeswap/blob/00317d9a8319715a8e28361901ab14fe50d06172/Timeswap/Core/contracts/TimeswapPair.sol#L314

        assetIn = param.xIncrease + feeStoredIncrease + protocolFeeStoredIncrease;

https://github.com/code-423n4/2022-03-timeswap/blob/00317d9a8319715a8e28361901ab14fe50d06172/Timeswap/Core/contracts/TimeswapPair.sol#L436

        assetOut = param.xDecrease - feeStoredIncrease - protocolFeeStoredIncrease;

There are also a lot of similar case in https://github.com/code-423n4/2022-03-timeswap/blob/00317d9a8319715a8e28361901ab14fe50d06172/Timeswap/Core/contracts/libraries/TimeswapMath.sol

Unnecessary modulo

https://github.com/code-423n4/2022-03-timeswap/blob/00317d9a8319715a8e28361901ab14fe50d06172/Timeswap/Core/contracts/libraries/SafeCast.sol#L6 uint32(x) is same as uint32(x % 0x100000000)

        y = uint32(x % 0x100000000);
amateur-dev commented 2 years ago

We will update for Unnecessary modulo issue

Mathepreneur commented 2 years ago

https://github.com/Timeswap-Labs/Timeswap-V1-Core/commit/962ee056ef60b7528d61aa9cc3c16fbdca179901