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 #230

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

Gas Optimizations #230

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

Comments

@code423n4
Copy link
Contributor

  1. Using unchecked and prefix increment can save gas

Proof of Concept:
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/CitadelMinter.sol#L152

Recommended Mitigation Steps:
change to prefix increment and unchecked

========================================================================

  1. Using += to increase value on var

Proof of Concept:
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/CitadelMinter.sol#L279
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/KnightingRound.sol#L196-L199

Recommended Mitigation Steps:
Change to:

	_newTotalWeight += _weight;

========================================================================

  1. Using != instead of > is more gas efficient

Proof of Concept:
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/CitadelMinter.sol#L343
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/Funding.sol#L170
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/Funding.sol#L452

Recommended Mitigation Steps:
Change to:

	require(length != 0, "CitadelMinter: no funding pools");

========================================================================

  1. unnecessary value set. the default value of uint is zero.

Proof of Concept:
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/CitadelMinter.sol#L152
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/CitadelMinter.sol#L180-L182
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/SupplySchedule.sol#L192

Recommended Mitigation Steps:
remove 0

========================================================================

  1. using delete statement can save gas

Proof of Concept:
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/CitadelMinter.sol#L366
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/StakedCitadel.sol#L416

Recommended Mitigation Steps:
Change to:

	delete fundingPoolWeights[_pool];

========================================================================

  1. Using unchecked can save gas

Proof of Concept:
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/Funding.sol#L236
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/KnightingRound.sol#L250

Recommended Mitigation Steps:

Unchecked{
limitLeft_ = assetCap - assetCumulativeFunded;
}

========================================================================

  1. Using > is cheaper than >=

Proof of Concept:
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/Funding.sol#L270-L271
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/Funding.sol#L361

Recommended Mitigation Steps:
1 second difference can be ignored to validate
Change from:
>= or <=
to:
> or <

========================================================================

  1. Using calldata to store struct data type can save gas

Proof of Concept:
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/Funding.sol#L244

Recommended Mitigation Steps:
Change memory to calldata

========================================================================

  1. Gas improvement on returning governancePerformanceFee and strategistPerformanceFee value

Proof of Concept:
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/StakedCitadel.sol#L859

Recommended Mitigation Steps:
declare governancePerformanceFee, strategistPerformanceFee in function returns and delete #L874 can save gas

function _calculatePerformanceFee(uint256 _amount)
        internal
        view
        returns (uint256 governancePerformanceFee, uint256 strategistPerformanceFee) //@audit-info return here
    {
        uint256 governancePerformanceFee = _calculateFee(
            _amount,
            performanceFeeGovernance
        );

        uint256 strategistPerformanceFee = _calculateFee(
            _amount,
            performanceFeeStrategist
        );
    }

========================================================================

  1. unnecessary citadelAmountWithoutDiscount MSTORE

Proof of Concept:
https://github.com/code-423n4/2022-04-badger-citadel/blob/main/src/Funding.sol#L207

Recommended Mitigation Steps:
citadelAmountWithoutDiscount only called once. just calculated directly to citadelAmount_ #L211

 if (funding.discount > 0) {
            citadelAmount_ =
                ((_assetAmountIn * citadelPriceInAsset)* MAX_BPS) /
                (MAX_BPS - funding.discount);
        }

========================================================================

@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