Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #145

Open
code423n4 opened this issue Apr 20, 2022 · 0 comments
Open

Gas Optimizations #145

code423n4 opened this issue Apr 20, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Gas Optimizations

  • Emitting a storage value for the followings: CitadelMinter.sol#L269, CitadelMinter.sol#L354, Funding.sol#L343, Funding.sol#L434-435, Funding.sol#L461-462, StakedCitadel.sol#L424, StakedCitadelVester.sol#L148,StakedCitadelVester.sol#L150, SupplySchedule.sol#L196-198 The emitted values can all be cached and emitted accordingly to minimize usage of SLOADs

  • At StakedCitadel.sol#L817 : The substraction can be unchecked since the condition is already met by if statement on StakedCitadel.sol#L816 and the substraction can not overflow.

  • At Funding.sol#L236 : The substraction can be unchecked since the condition is already met by if statement on Funding.sol#L235 and the substraction can not overflow.

  • At KnightingRound.sol#L250 : The substraction can be unchecked since the condition is already met by if statement on KnightingRound.sol#L249 and the substraction can not overflow.

  • At StakedCitadelVester.sol#L112 : The substraction can be unchecked since the condition is already met by if statement on StakedCitadelVester.sol#L111 and the substraction can not overflow.

  • At CitadelMinter.sol#L199 : The substraction can be unchecked since the condition is already met since lockingAmount is deposited to the vaul on line 195.

  • At SupplySchedule.sol#L105 : The substraction can be unchecked since the condition is already met on SupplySchedule.sol#L99-100

  • At StakedCitadelVester.sol#L112 and StakedCitadelVester.sol#L117 : The substraction can be unchecked since the condition is already met in claimableBalance() method's if statement.

  • At StakedCitadel.sol#L818 , the function is calling withdraw method which again calls _withdraw method in the same contract (not inheriting other contracts' functions). However, for IStrategy(strategy).withdraw(_toWithdraw); statement, the logic can be inlined to prevent repetetive calls inside the same contract.

  • Use bytes32 instead of string to save gas whenever possible since tring is a dynamic data structure and therefore is more gas consuming then bytes32.
    You could use bytes32 instead of string in the following places: StakedCitadel.sol#L85-L86 , inside StakedCitadel.sol initialize() method.

  • Numerous constant' variables on many locations throughout the contracts can be declared as immutable to save gas.

  • Choosing either named return or explicit instead of specifying both may reduce gas due to unnecessary bytecode introduced. There is an inconsistent use of implicit named return variables across the entire codebase which makes readability and maintainability hard.
    Below methods are only a few examples for this:
    At Funding.sol :

function deposit(uint256 _assetAmountIn, uint256 _minCitadelOut)
        external
        onlyWhenPriceNotFlagged
        gacPausable
        nonReentrant
        returns (uint256 citadelAmount_)
function getAmountOut(uint256 _assetAmountIn)
        public
        view
        returns (uint256 citadelAmount_)
function getStakedCitadelAmountOut(uint256 _assetAmountIn) public view returns (uint256 xCitadelAmount_)
function getRemainingFundable() external view returns (uint256 limitLeft_)
function getFundingParams() external view returns (FundingParams memory)
function getDiscount() external view returns (uint256)
  • Using custom errors instead of returning strings saves gas. But due to the complexity and the unique methods of the project, this may not be the best approach. Then, trying to reduce the error strings less than 32 bytes is also a cheaper gas saving method.
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Apr 20, 2022
code423n4 added a commit that referenced this issue Apr 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

1 participant